...

Master the rm Command in Linux/Unix: Quickly Delete Files and Folders in Just 2 Steps

Rate this post

Introduction: What is the rm command?

rm Command in Linux (or Unix – macOS) deletes files and folders. This is a serious command because once files/folders are deleted through its execution, they can’t be recovered so use it with caution.

rm stands for remove. We should always learn the basics of a command by issuing man <command> or <command> --help. This helps us to know the details of the command, e.g.- what is the use of this command, its synopsis, available options flags for this command, and examples related to its use case. See the below screenshots for the same.

rm command in Linux: man page
rm command in Linux: help page

Prerequisites

Synopsis: rm [OPTION]… [FILE]…

Precaution before using the rm command in Linux

Please check file permissions and ownership before issuing this command. Based on the current permissions and ownership of the file/directory, sudo privilege escalation might be needed.

Use cases

  • Delete a file
  • Delete multiple files
  • Delete a directory
  • Delete multiple directories

Assumption

for this post, I will assume an ubuntu system user. Please switch this user if you need it. Also we use ~ (symbol of home directory = /home/ubuntu)

Delete a file using the rm command

Synopsis

rm <filename>
  • Step 1 – Go inside the directory where the file is located (this step is optional)
  • Step 2 – Check if the file exists and you have write access to delete the file
  • Step 3 – Delete the file

Suppose you want to delete a file named my_file_1.txt which is located inside ~/linux_commands/rm_command directory.

# Step 1 -  Go inside the directory where file is located (this step is optional)
cd ~/linux_commands/rm_command

# Step 2 -  Check if the file exists and you have write access to delete the file
ls -lht my_file_1.txt

# Step 3 - Delete the file
rm my_file_1.txt

##### if you don't want to follow step 1, simply provide full path in step 2 and step 3 as below
# Step 2 -  Check if the file exists and you have write access to delete the file
ls -lht ~/linux_commands/rm_command/my_file_1.txt

# Step 3 - Delete the file
rm ~/linux_commands/rm_command/my_file_1.txt

if the file owner is root (as shown in the output of ls -lht) use the sudo privilege below

sudo rm my_file_1.txt
rm command in linux
Deleting a file using the rm command

Delete multiple files using the rm command

Synopsis

rm <filename> <filename> <filename> <filename> .. <filename>

Steps 1 and 2 will remain the same as described in the previous section, just change step 3 as follows

# Step 1 -  Go inside the directory where file is located (this step is optional)
cd ~/linux_commands/rm_command

# Step 2 -  Check if the file exists and you have write access to delete the file
ls -lht my_file_1.txt my_file_2.txt

# Step 3 - Delete the file
rm my_file_1.txt my_file_2.txt

##### if you don't want to follow step 1, simply provide full path in step 2 and step 3 as below
# Step 2 -  Check if the file exists and you have write access to delete the file
ls -lht ~/linux_commands/rm_command/my_file_1.txt ~/linux_commands/rm_command/my_file_2.txt

# Step 3 - Delete the files
rm ~/linux_commands/rm_command/my_file_1.txt ~/linux_commands/rm_command/my_file_2.txt
Deleting multiple files using the rm command

Delete a directory using the rm command

Synopsis

rm -r <dir_name>
rm -rf <dir_name>
# -r option is mandatory to remove directory, -f is optional
# flags meaning
-r, -R, --recursive   remove directories and their contents recursively
-f, --force           ignore nonexistent files and arguments, never prompt, using this option will not throw error if the file/directory does not exist


Please follow the below steps (assuming you want to delete my_dir_1)

  • Step 1 – Go inside the directory where the given directory(my_dir_1) is located (this step is optional)
  • Step 2 – Check if the directory exists and if you have write access to delete the directory(my_dir_1)
  • Step 3 – Delete the directory(my_dir_1)
# Step 1 - Go inside the directory where the given directory(my_dir_1) is located (this step is optional)
cd ~/linux_commands/rm_command

# Step 2 -  Check if the directory exists and if you have write access to delete the directory(my_dir_1)
ls -lht my_dir_1

# Step 3 - Delete the directory
rm -r my_dir_1
Delete a directory using the rm command

Delete multiple directories using the rm command

Synopsis

rm -r <dir_name_1> <dir_name_2> .. <dir_name_n>
rm -rf <dir_name_1> <dir_name_2> .. <dir_name_n>
# -r option is mandatory to remove directory, -f is optional
# flags meaning
-r, -R, --recursive   remove directories and their contents recursively
-f, --force           ignore nonexistent files and arguments, never prompt, using this option will not throw error if the file/directory does not exist

Note: It is better to check and delete one directory at a time, also check the directory content one at a time.


Please follow the below steps (assuming you want to delete ~/path1/to/my_dir_1, ~/path2/to/my_dir_2)

  • Step 1 – Check if the directory exists and if you have write access to delete the directory(path1/to/my_dir_1, path2/to/my_dir_2)
  • Step 2 – Delete the directory(path1/to/my_dir_1, path2/to/my_dir_2)
# Step 1 -  Check if the directory exists and if you have write access to delete the directory(my_dir_1)
ls -lht ~/path1/to/my_dir_1 ~/path2/to/my_dir_2

# Step 2 - Delete the directory
rm -r ~/path1/to/my_dir_1 ~/path2/to/my_dir_2
Delete multiple directories using the rm command

Additional Tips

  • If file is write protected or you don’t have write permission, then first grant this permission (sudo chmod 0664 <filename>)
  • if only root user has write access then use sudo (sudo rm <file_name>)

Conclusion

In this simple post, you learnt about deleting files and folders.
Enjoy the post!!

Spread the love

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.