Normally on this blog I tend to write about more complicated tasks or fancy Linux tricks and completely overlook some of the most basic tasks that a SysAdmin needs to know. Today I have decided that I will make my blog a little more comprehensive and add some posts with some of the basics.
Along with this I will be starting a new category, called Sysadmin Basics and I will try to post an additional article each week that covers some of the more basic concepts and commands used by Linux and Unix Sysadmins.
The rmdir command is used to delete and remove empty directories. I bolded empty as it is important to note that rmdir will only remove a directory if there are no files within that directory. If you want to remove a directory and all files within that directory, skip down to the rm section of this article.
# rmdir somedir/
# rmdir -p somedir/a/b/c/d/e/f/whoa
While rmdir will not remove directories with files in it; rmdir will recursively remove a directory tree that has no files. In the example somedir only has directory a within it, and the a directory only has b which only has c and so on.
The above command will also fail if there are multiple directories in one single directory, to handle that scenario you can list the directories individually and include the
# rmdir --ignore-fail-on-non-empty -p somedir/a/b/c/ somedir/a2/b2/
--ignore-fail-on-non-empty flag the command will still print that somedir is not empty even though it removes somedir. This is due to the fact that both command line arguments ask rmdir to remove somedir and rmdir cannot remove that directory until the last step.
While the rmdir command is solely for directories the rm command can remove both files and directories. With the right combination of flags rm will also remove entire directories, files and all.
# rm a-file rm: remove regular empty file `a-file'? y
On it's own rm will not prompt a user before removing a file; to keep systems safe from accidental file removals some distributions of Linux will ship with an alias for rm with the default
.bashrc file. This alias gives the interactive
-i flag for rm, this tells rm to prompt the user before removing files and directories.
# alias alias rm='rm -i'
While you can simply unalias the rm alias, a simpler and generally used method to remove files without being prompted is to add the force
-f flag to the rm command. It is advisable that you only add the force
-f flag if you really know what you are removing.
# rm -f b-file
If you don't want to be prompted for each file removable but also want to keep an eye on rm in case the command starts removing unexpected files, you can simply add the verbose
# rm -fv c-file removed `c-file'
There are many ways to remove multiple files, one method is to simply list each file you want to remove.
# rm -f a-file b-file
The bash command line supports wildcards and regex statements. A simplier way to remove all files that end in the word file is to simply state
*file. I suggest being cautious with wildcards as it is entirely possible to remove a file without meaning to.
# rm -f *file
Another common method of deleting files is to use regex statement, the below would remove anything that looks like files-0 through files-9 but would not remove files-a or files-list.
# rm -f files-[0-9]
If you want to simply remove an entire directory and all of the contents within, including both files and directories the easiest method is to add the recursive
-R flag to rm. If you are in any way unsure of what you are doing than drop the force
-f and replace it with verbose
-v or interactive
# rm -Rf somedir/