How to use the interactive features of the Linux rm command.

Posted: September 8, 2013. At: 1:41 PM. This was 4 years ago. Post ID: 6224

In this example; I am using the -i parameter to the rm command to ask for permission before erasing a file. This is very useful if you want to be sure that you are wiping the correct file.

homer@deep-thought ~/Documents $ rm -i thoughts.txt 
rm: remove regular file ‘thoughts.txt’? y

There is a way to overcome this with the bash shell. In this instance I have created an alias to the rm command that forces it to be interactive.

homer@deep-thought ~/Documents $ alias rm='rm -i'
homer@deep-thought ~/Documents $ alias rm
alias rm='rm -i'

But if I put a backslash before the command, this overcomes this and uses the vanilla command.

homer@deep-thought ~/Documents $ \rm 
Display all 106 possibilities? (y or n)
homer@deep-thought ~/Documents $ \rm Links.txt

A better way is to use the rm -I command in an alias. This will only prompt for confirmation if you are attempting to erase more than 3 files at once or if you are wanting to recursively erase a directory.

By default, the rm command when used as rm -rf will not erase the / directory of Linux. If you have some pressing need to do this; then you need to use this parameter: ~# rm -rf --no-preserve-root this allows the rm command to ignore the special status of the / directory and erase it. But the rm -rf command is something that you should not use lightly. You can type rm -r myfolder to erase a folder and it will ask for confirmation before doing so. This is fine.

Linux users may also use the rmdir command to erase a folder. This will not work if the directory is not empty.

homer@deep-thought ~/Desktop $ rmdir folder2
rmdir: failed to remove ‘folder2’: Directory not empty

Therefore the user needs to pass this parameter to enable a non-empty directory to be erased.

homer@deep-thought ~/Desktop $ rmdir folder2 --ignore-fail-on-non-empty

No comments have been made. Use this form to start the conversation :)

Leave a Reply