Creating and deleting files and directories
So, next up I think it’s time we learn how to create stuff using the command line.
To create a file, the command is touch.
creates an empty file try1 in the present working directory.
Multiple arguments can be passed to touch like touch try1 try2 try3 and so on. This will create 3 empty files try1, try2 and try3.
Also, touch can create files if you pass the path too.
touch /home/user/try1 /home/user/try2
will create 2 files try1 and try2 in the home folder of user “user”.
To create a directory, the command is mkdir.
creates an empty directory try in the present working directory.
Just like the touch command, the mkdir command also can accept multiple arguments like mkdir dir1 dir2 dir3 and so on. This will create 3 empty directories in the present directory dir1, dir2 and dir3.
Similar to the touch command, one can also pass the path in which the directory should be created to mkdir. Go ahead and try it: it’s really simple
To delete a file, the command is rm.
will remove the file try1 if such a file exists in the present directory.
Just like the other commands stated above, rm also accepts multiple arguments.
The command to delete directories is also rm. But, to delete a directory you need to use the -r option(stands for recursive delete).
This is where it differs from DOS; DOS requires that a directory be empty before it can be deleted. But in UNIX, just the -r option needs to be used – it’ll delete the directory and it’s contents, if any.
rm -r dir1
removes the dir1 and it’s contents, if any.
Multiple arguments, paths to directories to be deleted all can be used just like in deleting files.
Hope you understood how to create and delete files and folders. Till next time!