Skip to content

Creating and deleting files and directories

December 19, 2009

So, next up I think it’s time we learn how to create stuff using the command line.

Creating files
To create a file, the command is touch.
For example,
touch try1
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.
For example,
touch /home/user/try1 /home/user/try2
will create 2 files try1 and try2 in the home folder of user “user”.

Creating directories
To create a directory, the command is mkdir.
For example,
mkdir try
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

Deleting files
To delete a file, the command is rm.
For example,
rm try1
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.

Deleting directories
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.
For example,
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!

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: