3.3 rm rmdir man

We start with two sub-directories play and play2 in the home directory each containing one text file. How can we get rid of them?

$ cd ~
$ rm play/file2.txt
$ rmdir play
$ rmdir play2
rmdir: failed to remove 'play2': Directory not empty

From the home directory we can delete the file in the sub-directory and then remove the empty directory. The directory play2 is not empty and can't be removed with rmdir.

$ rm -r play2
$ cd play2
bash: cd: play2: No such file or directory

rm -r
does it though. The -r stands for recursive which means it will delete all files and sub=directories. Caution - deleting directories by mistake can do a lot of damage! There is a better way

>$ rm -ir play
rm: descend into directory 'play'? y
rm: remove regular empty file 'play/file1.txt'? y
rm: remove directory 'play'? y

The -i flag means interactive so  each deletion is confirmed. Note we can use rm with no flags or several flags such as i or r after the dash to alter the behaviour of the command.

How to remember all these codes? You get help by using the man command.

man rm
Scroll up and down with the arrow keys and the letter q exits.

No comments:

Post a Comment

Introduction to Linux Command Line

SSH is the way you will connect to your Linux server. You will need to understand the basics of the Linux Command Line.