• Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'rm' Command In Linux

2023-04-29 - By Robert Elder

     I can use the 'rm' command to delete a file like this:

rm foo.txt

     and now the file is gone forever.

     I can delete multiple files at once by specifying more than one file after the 'rm' command.  If I start in a directory that looks like this:

ls -l
total 8
drwxrwxr-x 2 robert robert 4096 Apr 30 12:02 files
-rw-rw-r-- 1 robert robert    0 Apr 30 12:02 groceries.txt
-r--rw-r-- 1 robert robert    0 Apr 30 12:02 names1.txt
-rw-rw-r-- 1 robert robert    0 Apr 30 12:02 names2.txt
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 other-files

     and use the 'rm' command like this:

rm names2.txt groceries.txt

     You can see that the two files 'names2.txt' and 'groceries.txt' are now gone:

ls -l
total 8
drwxrwxr-x 2 robert robert 4096 Apr 30 12:02 files
-r--rw-r-- 1 robert robert    0 Apr 30 12:02 names1.txt
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 other-files

'rm' Command With Directories

     By default, deleting a directory won't work:

rm files

     The output from running the above command is the following:

rm: cannot remove 'files/': Is a directory

     And the file was not deleted.  If you run:

ls -l

     you can see that the output is still the same:

total 8
drwxrwxr-x 2 robert robert 4096 Apr 30 12:02 files
-r--rw-r-- 1 robert robert    0 Apr 30 12:02 names1.txt
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 other-files

     I need to include the '-r' flag to make it recursive:

rm -r files
ls -l

     now outputs the following:

total 4
-r--rw-r-- 1 robert robert    0 Apr 30 12:02 names1.txt
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 other-files

Using The '-f' Flag With 'rm' Command

     Sometimes, the 'rm' command may prompt you before deleting a write protected file:

rm names1.txt

     If you type 'y' at this prompt the file will be deleted:

rm: remove write-protected regular empty file 'names1.txt'?

     If we now run the 'ls' command again:

ls -l

     You can see that the file was indeed deleted:

total 4
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 other-files

     You can sometimes ignore this and other prompts by including the '-f' flag:

rm -f names1.txt

     However, you can only delete a file if you have write and execute permissions on the directory that contains the file.  For example, here is a listing of files in the 'other-files' directory:

ls -la other-files

     Note the fact that the 'w' or 'write' permission is missing on the folder itself (the '.' entry):

total 8
dr-xr-xr-x 2 robert robert 4096 Apr 30 12:02 .
drwxrwxr-x 3 robert robert 4096 Apr 30 12:38 ..
-r--r--r-- 1 robert robert    0 Apr 30 12:02 README.txt

     If we now try to delete the 'README.txt' file using the '-f' flag:

rm -f other-files/README.txt

     we get the following error, and the file is not deleted:

rm: cannot remove 'other-files/README.txt': Permission denied

     By adding the write permission to the directory using the 'chmod' command like this:

chmod u+w other-files
ls -la other-files

     we can see that the 'w' permission is now present on the directory:

total 8
drwxr-xr-x 2 robert robert 4096 Apr 30 12:02 .
drwxrwxr-x 3 robert robert 4096 Apr 30 12:38 ..
-r--r--r-- 1 robert robert    0 Apr 30 12:02 README.txt

     Now we can delete the 'README.txt' file using the '-f' flag:

rm -f other-files/README.txt

     Using the 'ls' command we can verify that the file has been deleted:

ls -la other-files
total 8
drwxr-xr-x 2 robert robert 4096 Apr 30 12:44 .
drwxrwxr-x 3 robert robert 4096 Apr 30 12:38 ..

     And that's why the 'rm' command is my favorite Linux command.

Setup Commands

     For reference, here are the setup commands I used when writing this article:

touch foo.txt
touch names1.txt
touch names2.txt
touch groceries.txt
chmod u-w names1.txt
mkdir files
mkdir other-files
touch other-files/README.txt
chmod ugo-w other-files
chmod ugo-w other-files/README.txt
Intro To 'stty' Command In Linux
Published 2023-10-04
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List

Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
Privacy Policy      Store Policies      Terms of Use