How to Safely Remove Files and Directories Using Linux Command Line

How to Safely Remove Files and Directories Using Linux Command Line
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 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 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 rm command.
FILE_NAMES: File name 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 following example demo.txt file will be removed from the system if its available:

rm demo.txt

If 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.

How to Safely Remove Files and Directories Using Linux Command Line - prompt
How to Safely Remove Files and Directories Using Linux Command Line – prompt

2. How to Delete Multiple Files

You can also delete multiple files using 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 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 deleteing a file in Linux system.

In 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:

How to Safely Remove Files and Directories Using Linux Command Line - prompt multiple
How to Safely Remove Files and Directories Using Linux Command Line – prompt multiple

4. How to Delete Write Protected File without Prompt

To delete write protected file without prompt in Linux system you should use -f force option with rm command:

rm -f linuxdistros.pdf

In above command linuxdistros.pdf file will be deleted from 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 regular expression you can do tasks like deleting multiple files at the same time with same extensions.

To delete all files with .jpg extension you can run following command in Linux.

rm *.jpg

Above command will delete all files with .jpg extension in 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 -d option you can delete a empty directory but to delete a non-empty directory you should use -r recursive option.

To remove an empty directory you can use following command:

rm -d directory_name

And to delete 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

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here