The Remove command in Unix/Linux ecosystem is very powerful and effective one time operation as it is unrecoverable.
To delete different kinds of content various options can be used with the rm command in linux.
In this section , we will demonstrate how to use the go about rm, unlink, and rmdir commands to remove files and directories in Linux
The Linux system treats the files and Directories by identifying them with Inodes.
Deleting a file is a simple operation but the user has to advise a lot of caution.
Sample Syntax
$ rm filename
Now lets Demonstrate deleting a file
Let’s use the -v
flag to print the verbose information onto the screen.
[vamshi@linuxcent delete-dir]$ rm -v file1 file2 file3 removed ‘file1’ removed ‘file2’ removed ‘file3’
Using -i
option asks the user for interaction and deletes the file upon accepting a “y”
as user input.
[vamshi@linuxcent delete-dir]$ rm -i file3 rm: remove regular file ‘file3’? y
Deleting multiple files from the command line.
Using the wildcard * to delete similar extension filenames
[vamshi@linuxcent delete-dir]$ rm -v ./*.pdf
removed ‘./samplePDFfile1.pdf’
removed ‘./samplePDFfile2.pdf’
removed ‘./samplePDFfile3.pdf’
It’s a good practice to use (-v) verbose mode while running rm command.
Now let’s focus on the best practice
[vamshi@linuxcent ]$ alias rm="rm -iv"
Ensure to export the changes to your profile as demonstrated here(Exporting the changes to user login profile permanently)
[vamshi@linuxcent ]$ rm samplefilename.txt rm: remove regular empty file ‘file10’? y removed ‘samplefilename.txt’
But One has to always ensure to see the file permissions and if they are currently being used by any user or a program.(fuser and what it does)
You can long list the filename to see the permissions(long listing the files on Linux system)
How to Remove the Directory/s from Linux system?
A command-line utility rmdir
exists on Linux to delete the directories. But, it deletes only the Empty directory(ies).
The command rm -d
or --dir
is also preferred to delete an empty directory
The rm
command-line utility also lets you delete the directories and its contents recursively with -r
option.
Sample Command Syntax:
[vamshi@linuxcent delete-dir]$ rmdir sampleDIR1
We can use the -v option to print the verbose information:
[vamshi@linuxcent delete-dir]$ rmdir -v sampleDIR1 rmdir: removing directory, ‘sampleDIR1’
Deleting multiple subdirectories that are empty can be done with -p option, its demonstrated as follows:
[vamshi@linuxcent ]$ rmdir -v -p sampleDIR/subdir/ rmdir: removing directory, ‘sampleDIR/subdir/’ rmdir: removing directory, ‘sampleDIR’
Deleting multiple files from the command line.
[vamshi@linuxcent ]$ rmdir sampleDIR1 sampleDIR2 sampleDIR3
Now let’s shift our focus to rm
command which offers the ability to delete the directories and its contents recursively.
[vamshi@linuxcent ]$ rm -v -r sampleDIR3/ removed directory: ‘sampleDIR3/subdir3’ removed ‘sampleDIR3/samplefile3.txt’ removed directory: ‘sampleDIR3/’
[vamshi@linuxcent delete-dir]$ rm -v -r sampleDIR6 sampleDIR7 sampleDIR8 removed directory: ‘sampleDIR6/subdir6’ removed directory: ‘sampleDIR6’ removed directory: ‘sampleDIR7/subdir7’ removed directory: ‘sampleDIR7’ removed directory: ‘sampleDIR8/subdir8’ removed directory: ‘sampleDIR8’
Please also see our post on How to make a file undeletable on Linux