Using rm command to delete files and directories in Linux
 

Using rm command to delete files and directories in Linux

 

As already we have discussed how to create a file or directory in Linux or Unix systems. Now if you want to remove them then Linux provides a separate command which is known as rm and it stands for remove.

The rm command is used to remove the files, directories, and links from a Linux system. In this article, we will discuss the usage of the rm command along with some examples.

Syntax of the rm command

The syntax of the rm command is given below –

rm [options] [file_name]

You can use the following options with the rm command.

OptionDescription
-iPrompt before deleting file
-fForcefully delete a file
-IPrompt while removing more than three files
-r or -RRemove directories with their content recursively
-d--dirRemove empty directory
--one-file-systemWhile removing directories recursively it skips a directory that have a different filesystem
-v--verboseIt prints what is being done
--helpDisplay help and exit
--versionDisplay the current version of rm program

How to delete a file in Linux

By default, the rm command deletes a file when used without any options means it doesn’t remove directories. Now use the following command to delete a file –

rm file_name

For example-

rm testfile.txt

The command will remove testfile.txt from the current working directory of the user.

Note: While deleting some files from your system you might need to have root privileges so you should use the sudo with the command

Deleting a file using the option -i with rm command

The option -i enables the interactive deletion of the file. It will prompt you that “rm: remove regular file ‘testfile.txt’? ” you can confirm it by entering yes or no.

Now use the following command if you want to delete the file in interactive mode –

rm -i testfile.txt

Deleting a file forcefully

You can use the -f option to delete a file forcefully from your system. This will ignore the nonexistent files and arguments and never prompt. To delete a file forcefully use the following command in your terminal –

rm -f testfile.txt

Deleting multiple files

You can delete multiple files using a single command. You need to pass all the filenames as the argument. Use the command as given below to remove the files from your system –

rm file1.txt file2.txt file3.txt

How to delete a directory from a Linux system

To delete one or more empty directories you need to use the following command in your terminal –

rm -d dir_name(s)

For example –

rm -d dir1

This command will remove the empty directory dir1.

If you want to delete the directory that contains files or directories then you will have to use the following command in your terminal –

rm -r dir1

Where dir1 contains some files or directories in it.

If you want to see more options and usage of the rm command then see its manual page in your terminal by using –

man rm

Conclusion

Now you know how to delete a file or directory from a Linux or Unix system. If you have any query then leave them in the comments below.

Previous [ubuntu] How to keep digital photos
Next ImportError: libgfortran.so.3: cannot open shared object file: No such file or direct
 

About author

You might also like

Commands 0 Comments

How to copy files & directories in Linux terminal?

Copying files and directories in Linux is one of the most common tasks. If you are using a graphical desktop environment then you can easily copy files or directories using

Commands 0 Comments

Using ping command in Linux

Ping is a software utility that is used to test the network connectivity issue between two systems. This command is available for various platforms including Windows, Linux, Unix, etc. It

Commands 0 Comments

How to rename files & directories in Linux?

There are various ways in which you can rename the files or directories on a Linux system. If you are using a system with a graphical user interface then you

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply