Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Linux and Unix rm command tutorial with examples

What is the rm command?

The rm command is a command line utility for removing files or directories.

How to remove a file

To Remove a file pass the name of a file or files to the rm command. Note that files will be removed immediately.

ls 
foo.txt bar.txt
rm foo.txt
ls
bar.txt

How to remove multiple files

The rm command can remove multiple files at once either by passing it more than one file or by using a pattern.

ls 
foo.txt bar.txt
rm foo.txt bar.txt
ls

In the following example a pattern is used to remove all filenames ending in ‘.txt’.

ls 
foo.txt bar.txt baz.zip
rm *.zip
ls
baz.zip

Be careful that no unwanted files are removed using this method.

How to prompt for confirmation

To prompt for conformation when removing a file use the -i option. This will cause rm to ask for confirmation. If confirmation is given the file is removed. If it is not given the file removal is skipped.

ls 
foo.txt bar.txt
rm -i foo.txt
# type 'y' at the prompt
rm: remove regular file 'foo.txt'? y
ls 
bar.txt

In the following example confirmation is not given and the removal is skipped.

ls 
foo.txt bar.txt
rm -i foo.txt
# type 'n' at the prompt
rm: remove regular file 'foo.txt'? n
ls 
foo.txt bar.txt

Many users like to add an alias to make this the default behaviour so that each time the rm command is run they are prompted for confirmation.

alias rm="rm -i"

How to show more information

To show more output in the rm command pass the -v option. This will cause more information to be written to standard output.

ls 
foo.txt bar.txt
rm -v foo.txt
removed 'foo.txt'
ls 
bar.txt

How to remove directories

By default the rm command does not remove directories. To remove directories use the -R option to make removal recursive.

ls -F
foo.txt bar/
rm: cannot remove 'bar': Is a directory
rm -R bar/
ls -F 
foo.txt

The -i option may also be combined with the -R option to prompt for confirmation.

tree foo
foo
├── bar
│   └── baz.txt
└── foo.txt

1 directory, 2 files
rm -iR foo
rm: descend into directory 'foo'? y
rm: remove regular empty file 'foo/foo.txt'? n
rm: descend into directory 'foo/bar'? y
rm: remove regular empty file 'foo/bar/baz.txt'? y
rm: remove directory 'foo/bar'? y
rm: remove directory 'foo'? n
tree foo                                                                (master)
foo
└── foo.txt

0 directories, 1 file

How to handle permission denied errors

Sometimes when running the rm a permissions error will be thrown indicating that you do not have enough permissions to remove a file.

rm /etc/shadow
rm: remove write-protected regular file 'shadow'? y
rm: cannot remove 'shadow': Permission denied

In this case you will either need to ask a systems administrator to give you permissions on the file or use sudo to delete the file.

sudo rm /etc/shadow

How to remove multiple files based on a pattern

The rm command can be combined with find to find and remove files based on things like modification time, file name or permissions. The following command will find all files in the folder foo that have been modified in the last day and remove them interactively.

find ./foo -type f -mtime -1 -exec rm -i {} \;

How to force a file or folder to be removed

If a user tries to remove a write-protected file with rm by default they will be prompted for confirmation.

rm foo
rm: remove write-protected regular empty file 'foo'? y

To force removal and not receive a prompt pass the -f option.

rm -f foo

rm -Rf /

One of the worst commands you can run in Linux is rm -Rf / especially if you are root. This will remove everything on your machine rendering it useless. The zsh shell now has some protection against this and even prompts if you use an asterisk character to delete all files in a directory.

rm *
zsh: sure you want to delete all the files in /home/george/foo [yn]? y

Does rm delete a file?

The rm command does not delete a file. Instead it unlinks it meaning the data is still on disk but the link to it is removed. Depending on the filesystem you are using it is more or less difficult to recover files removed with rm. If you really want to delete something you can use shred to overwrite a file to hide its contents.

echo 'hello' > foo.txt
cat foo.txt
hello
shred foo.txt
cat foo.txt
    x~XZ"MG     0KA.2Fxţ,b4s3#jOL       ?`xݕ杶hhȜsf.;3 a+M9?n
...
rm foo.txt

The safest option if you really want to delete a file is to take the hard drive out and smash it to smithereens. Wooo destruction!

Further reading

  • rm man page
  • rm Wikipedia page
  • Where to files go when the rm command is issued
  • Linux and Unix find command tutorial with examples
  • Linux and Unix find alias tutorial with examples


This post first appeared on Home | Shape Shed, please read the originial post: here

Share the post

Linux and Unix rm command tutorial with examples

×

Subscribe to Home | Shape Shed

Get updates delivered right to your inbox!

Thank you for your subscription

×