By now you should have gathered basic knowledge on your filesystem. You know how to find help, list contents on your directories, copying and moving stuff around and also some theory behind the Linux Filesystem. Now let’s see how can you create and remove directories and files, one by one ore a few at a time!
To create a directory, you first have to make sure that you have permissions on the current working directory. Normally your ~home will have this permission, so feel free to test around here. It is also possible to use the /tmp directory, which is world writable by everyone, but don’t expect to find your stuff here in the future… the name is self explanatory (read it temp, as in temporary). For this tutorial I’ll be using the /example directory, that I have created before.
We use, for directories the mkdir command for creation, and the rmdir for deletion. Issuing a mkdir test1 on your home, makes a new subdirectory called, go figure it out, test1!!!! If you want to remove it, simply do a rmdir test1 (remember, if an error occurs, to use the full path). This only works if the directory is empty, but if it’s not, is just a matter of adding the -R (capital R) flag to rmdir. This recursively deletes everything inside the target directory.
Now, forget the last part I said about removing directories with rmdir. Thats right, I just told you that, because the command exists, but let’s learn a more general command, a kind that does-it-all.
I’m talking about the rm command. In the man page for rm we find that its used to remove files or directories. This means you only need to know a few flags, and you have at your hands one single command to remove both files and directories.
If you want to remove a file, just make a rm <target>, and it will be removed. It’s possible to use wild cards, to remove files by name, or extension. If you have a bunch of *.txt files sitting around in a directory which also contains several other file types, and you want to remove all those *.txt, just type rm *.txt inside the directory, or if your are not inside, use the full path.
For directories, we must explicitly tell rm that we want to do a remove in a directory, so just add the -d flag to your command to accomplish this. If the directory in questions it’s not empty, just add the -r flag which removes the entire directory tree below the specified directory, (there are no limitations on the depth of director trees that can be removed by rm -r).
If you want to see what is being removed had the -v (verbose) to see each entry being removed. If you need confirmation, use the -i for interactive mode (you will be asked for each target, if that’s what you want). If you intend to remove everything without confirmation, and forcing in the way, had the -f flag to your rm.
One last quick command. To create a empty file, use touch <target> . If the <target> does not exist, a new file is created, else, the file’s timestamps will be updated. (More on timestamps later…)
That’s it, no more boring command juggling. Train yourself inside a safe directory, so you can master this without thinking (i call this, Zen commandlinning). It’s very useful to know different flags, for different situations, and also using regular expressions to accomplish the job done. Have phun!

LINKS: - man rm
-funny man rm
   Share This


0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment