Linux Commands – rm
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed mkdir command in Linux which is used to create a directory in Linux filesystem.
https://cloudaffaire.com/linux-commands-mkdir/
In this blog post, we will discuss rm command in Linux. rm stands for remove and is used to remove files and directory. You can remove single, multiple, or multilevel file/directory using rm command.
Linux Commands – rm:
You can delete single or multiple files using rm command. You need to provide the full or relative path of the file with rm command in order to delete it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
######################### ## Linux Commands | rm ## ######################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##--- ## rm ##--- ## rm [options] [file|dir] touch myfile.{txt,sh,mp3,tar,zip,csv} ## create some files ls ## returns myfile.csv myfile.mp3 myfile.sh myfile.tar myfile.txt myfile.zip rm myfile.csv ## removes myfile.csv ls ## returns myfile.mp3 myfile.sh myfile.tar myfile.txt myfile.zip rm myfile.mp3 myfile.sh ## removes myfile.mp3 myfile.sh ls ## returns myfile.tar myfile.txt myfile.zip rm myfile* ## removes rest of the files with name starting with myfile ls ## empty |
You can delete single or multiple directories using rm command. You need to provide the full or relative path of the directory with rm command in order to delete it. Use rm -d option to remove empty directory and rm -r or -R or –recursive to remove directory recursively along with their content.
1 2 3 4 5 6 7 8 9 10 |
mkdir mydir1 mkdir2 ## create two directories named mydir1 and mydir2 ls ## returns mydir1 and mydir2 rm mydir1 ## rm: cannot remove ‘mydir1’: Is a directory rm -d mydir1 ## removes mydir1 which is currently empty ls ## returns mydir2 echo "hello world" > mydir2/myfile ## creates a file named myfile under mydir2 rm mydir2 ## rm: cannot remove ‘mydir2’: Is a directory rm -d mydir2 ## rm: cannot remove ‘mydir2’: Directory not empty rm -r mydir2 ## removes mydir2 ls ## empty |
You can use rm -i to prompt before every removal.
1 2 3 4 5 |
touch myfile1 myfile2 ## create some files rm -i myfile* ## prompt before every removal ## rm: remove regular empty file ‘myfile1’? y ## rm: remove regular empty file ‘myfile2’? y |
You can also use rm -I option to prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes
1 2 3 4 5 6 7 |
touch myfile{1..10} ## create some files rm -I myfile* ## prompt before removal if more than three files ## rm: remove 10 arguments? y mkdir -p mydir1/mydir2/mydir3/mydir4 ## create some directories rm -rI mydir1 ## prompt before removal ## rm: remove 1 argument recursively? y |
You can use rm -v or –verbose to print what rm command is doing.
1 2 3 4 5 6 |
touch myfile{1..3} ## create some files rm -v myfile* ## remove the files with output ## removed ‘myfile1’ ## removed ‘myfile2’ ## removed ‘myfile3’ |
You can use rm -f or –force option to remove a file/directory forcefully. This option is very useful when dealing with non-existent files or write-protected files. -f or –force option disables the prompt.
1 2 3 4 5 |
touch myfile ## create a file chmod 444 myfile ## only grant read access ls -l ## current permission on myfile -r--r--r-- rm myfile ## rm: remove write-protected regular empty file ‘myfile’? n rm -f myfile ## removes the file without any prompt |
You can use -f and -r command together with rm command to forcefully and recursively delete directory and its contents. Be careful with this command as it can cause massive damage if executed with sufficient permission like root.
1 2 3 4 5 6 7 8 9 10 11 |
mkdir -p mydir1/mydir2/mydir3 ## create some directories touch mydir1/mydir2/mydir3/myfile ## create a file chmod 444 mydir1 ## make write protected rm -r mydir1 ## prompts and without -f fail to delete ## rm: descend into write-protected directory ‘mydir1’? y ## rm: cannot remove ‘mydir1/mydir2’: Permission denied ## rm: remove write-protected directory ‘mydir1’? n rm -rf mydir1 ## rm: cannot remove ‘mydir1/mydir2’: Permission denied sudo rm -rfv mydir1 ## removed mydir1 along with its content |
As mentioned above, rm -rf is the most dangerous command that you can execute as it removes everything in the target directory without any prompt. You can imagine what will happen if you execute this command in the root directory (/) as root user, it will try to delete the entire Linux filesystem and break your system. Linux as a failsafe for rm -rf command has the option –preserve-root[=all] which is also rm default behavior that prevents deletion of root by running rm -rf / as root user. The –no-preserve-root option overrides this behavior. So, if you really want to destroy your system you can execute rm -rf –no-preserve-root / as the root user. I have not tested the last command so don’t know if Linux OS has any fail-safe to override this or not, besides which OS wants to destroy itself.
Hope you have enjoyed this article. In the next blog post, we will discuss mv command in Linux.