Linux: Creating and Deleting Directories and Files

The first principle of file creation is that when you, as a specific user, create a file, it will be owned by you, and your default group will have some default permissions set on that file. Remember the User – Group – Other trio.

Create a directory:

mkdir demo

You can create several at once:

mkdir one two three

Run an ls to see the results:

ls -la

Especially note the user and group permissions the directories have.

Remove directories:

rmdir one two three

…which will work just fine as long as there is nothing inside each directory. But usually there is, so we use a completely different command to deal with directories with files inside them:

rm -r one two three

This will force you to answer “yes” over and over for each file deleted. You can make the process silent by “forcing” it:

rm -rf one two three

Create a file:

If you just want to create an empty file, you can use this method:

touch myfile

Note that touch is actually a command that just updates the last-updated date of the file, so it looks newer. This used to be handy to keep sysadmins from deleting old but critical stuff.

What’s nice about touch is that if a file exists, it just gets its date changed. If the file doesn’t exist, it is created. This is very handy in scripting.

Delete a file:

rm myfile

There is no “recycle bin” – the file is gone for good.