How to Safely Remove Files and Directories Using Linux Command Line

Safely Remove Files and Directories Using Linux Command Line
To remove files and directories using the Linux command line rm
and rmdir
commands are used. To remove files unlink
command also used in Linux. In this tutorial, you are going to learn how to safely remove files and directories using the Linux command line.
Basic Syntax for rm
Command
Following is the basic syntax for rm command in Linux:
rm [OPTIONS] [FILE_NAME/S]
where,
OPTIONS: Specify the options to use with the rm
command listed here.
FILE_NAMES: Filename or names to be deleted.
How to Delete Files
Following are the examples to delete files using rm command:
1. How to Remove a File
To remove a single file from your system you can use rm
command without any options. In the following example the demo.txt
file will be removed from the system if it is available:
rm demo.txt
If the file to be deleted is write protected then you will be prompted for confirmation at such time you should enter y
to delete the file, Otherwise, if file is not write-protected it will be deleted without any prompt.

2. How to Delete Multiple Files
You can also delete multiple files using the rm
command without using any options. You should just specify file names separated by spaces one by one.
Before you delete any files just list the files in the targeted directory using ls
command:
ls -a
To delete multiple file using rm command run following command in the terminal:
rm demo.txt demo2.txt demo3.txt
In the above example demo.txt
, demo2.txt
and demo3.txt
will be deleted from the current directory.
3. How to Prompt Before Deleting File
You can use -i
option with rm command to show prompt before deleting a file in the Linux system.
In the following example rm
command will prompt you before deleting each file specified. Enter ‘y’ to delete files otherwise enter ‘n’:
rm -i demo.txt demo2.txt demo3.txt
The output should be:

4. How to Delete Write Protected File without Prompt
To delete the write-protected file without prompt in Linux system you should use -f
force option with rm
command:
rm -f linuxdistros.pdf
In the above command linuxdistros.pdf
file will be deleted from the current directory without prompting you even if file is write-protected.
5. How to Remove Multiple File with Regular Expression
The rm
command can also be used with a regular expression. By using a regular expression you can do tasks like deleting multiple files at the same time with the same extensions.
To delete all files with the .jpg
extension you can run following command in Linux.
rm *.jpg
Above command will delete all files with .jpg
extension in the current directory.
How to Delete Directories
Following are the examples to delete files using rm command:
1. How to Remove a Directory or Folder
You can also remove an empty directory using rm command with -d
option. By using the -d
option you can delete an empty directory but to delete a non-empty directory you should use a -r
recursive option.
To remove an empty directory you can use the following command:
rm -d directory_name
And to delete the non-empty directory you can use below command:
rm -r directory_name
If the directory is write-protected, it will prompt you before deletion.
You can also use rmdir
command delete a directory:
rmdir directory_name
2. How to Remove Multiple Directories or Folders
To delete multiple directories you should use -r
option with rm
command with space-separated file names.
rm -r directory_name1 directory_name2 directory_name3
The above command will delete directory_name1
, directory_name2
and directory_name3
directories.
3. How to Delete Write Protected Directory with Prompt
You can delete a directory with prompt each time by combining -i
option with -r
recursive option.
In below example it will delete a directory by prompting user:
rm -ri directory_name
or you can also use below command:
rm -r -i directory_name
Conclusion
You have successfully learned How to Safely Remove Files and Directories Using Linux Command-Line. If you have any of the queries regarding this tutorial then please don’t forget to comment below.
LATEST POSTS
-
Bash Case Statement with Examples
-
How to Install MySQL on Linux Mint 19
-
11 Wget Command Examples in Linux (Linux File Downloader)
-
Concatenate Strings in Bash Script
-
How to Install DataGrip on Linux Mint 19
-
How to Install Docker Compose on Debian 10
-
How to Create New User and Grant Permissions in MySQL
-
How to Install Papper Flash on Linux Mint 19
-
How to install Nginx on CentOS 7
-
How to Set or Change User Password in Linux
-
How to Install VirtualBox on Debian 10
-
How to Install Kodi on Fedora 29/30