How to Delete Files and Directories on Linux

To delete files and directories in Linux you can use the ‘rm’ command.

Use of ‘rm’ Command

“rm” is an entry on the Linux command line created for removing files and directories. It is one of the important commands that every Linux user should know. The rm command eliminates entries for a particular file, group of files, or particular set of files from a directory list. When you use the rm command, user confirmation, read permission, and write permission is unnecessary before a file is deleted. However, you must write access to the file’s directory.

How to Remove Linux files using rm

The general syntactic format for the rm (remove) command is:

rm [OPTIONS]... FILE…

There are various types of options, as we would in this article, such as;

Command and OptionFunction
rm -dRemove a void directory
rm -rRemove a non-void directory with the contents
rm -iPrompt before deleting a file
rm -IPrompt an output once when deleting over three files
rm *Remove a particular file with specific characters
rm -fBypass any confirmation to delete a write-protected file
rm -rfBypass any confirmation to delete a write-protected non-empty folder
rmdir -pRemove an empty subdirectory and its parent directory
rm- vOutput the details of the file or directory deleted
Get your Linux hosting server for seamless execution of your Linux commands and unlock the full potential of this tool.

Usually, when a user tries to run the syntax without an option, the rm doesn’t remove any directories. Therefore, the user is not prompted to remove any given file.

Get your Linux VPS Hosting for seamless execution of your Linux commands

To remove a given file, use the rm command with the filename following after as an argument.

rm filename

If a write protection is on the parent file, you will receive a notification “Operation not permitted” error. If not, the specified file will be removed without receiving a notification. The command produces no prompt/output, and the command automatically becomes null, expecting another command.

To remove a file with write protection, the command will prompt you with the following Output;

rm: remove write-protected regular empty file 'filename'?

You type Y and punch the Enter button to delete the file.

Using the -f option can instruct the rm never to produce prompts and also to ignore arguments. The -f argument is as follows;

rm -f filename

To obtain details regarding the deleted files, you can run the -v (verbose) option;

rm -v filename

The Output is;

removed 'filename'

Removing Multiple Files

rm is also capable of deleting more than one file at once. The proper argument is to write the filenames with a space in between them like this;

rm filename1 filename2 filename3

The above commands all function if you are in the same directory as the supplied files. To remove a file that isn’t in your current working directory, you can instead include the file path in the command line parameter:

rm dir/subdir/filename

Regular expressions can also be added as an argument. Using the png format as a specimen, you can remove all png files in a directory by running the expression;

rm *.png

When dealing with multiple files, it is good practice to use the “Ls” command to be prompted on the files that will be removed.

How to Remove directories in Linux using rm

To remove one or more void directories, you can use the -d option:

rm -d dirname

The rm -d command is quite similar to how the rmdir functions.

To remove non-empty directories and all the files recursively, you run the -r (recursive) option:

rm -r dirname

Receiving a prompt

If you want to receive a prompt any time a given file is about to be removed, the -i option is ideal for the task:

rm -i filename1 filename2

Confirm by inputting Y and tapping the Enter key to receive the Output;

rm: remove regular empty file 'filename1'?
rm: remove regular empty file 'filename2'?

When a user is to remove more than three files or make a recursive removal of a directory, the -I option is the gold standard practice:

rm -I filename1 filename2 filename3 filename4

You will get a particular prompt for the entire expression to confirm the removal of all the queried files and directories:

rm: remove 4 arguments?
rm -rf

You will be prompted to confirm the operation when attempting to remove a write-protected file or directory. The rm -rf expression can bypass the prompt, deleting the directory without any confirmation:

rm -rf dirname

Note: This -rf option is highly dangerous and should only be used with extreme measures being set in place.

Removing directory Linux using rmdir

Linux devices do not accommodate recycle bins. Therefore, your files are permanently deleted. The only way to retrieve a lost file is by a backup. That’s why it’s advisable to host your sensitive data files on a VPS. You can try Ultahost’s VPS Hosting to enhance your data backup.

The rmdir command allows you to avoid that mistake by only deleting empty files. It uses the syntax:

rmdir [option] DirectoryName

It will return the Output as an error message if the directory has files:

rmdir: failed to remove ‘Directory’: Directory not empty

To remove a subdirectory within a directory, you can use the -p option. Assuming the directory path is /dir/subdir, use the command; 

rmdir -p /dir/subdir

The Subdirectory folder will first be deleted. If the directory folder is empty after deleting the Subdirectory folder, it will be subsequently deleted.

Conclusion

The “rm” command is essential for Linux users to remove files and directories from their systems. The different options and syntaxes that can be used with the rm command, such as removing a specific file, multiple files, directories, and even using regular expressions. There’s the rmdir command to remove empty directories. Users should exercise caution when using the “-rf” option with the rm command, as it can delete files and directories permanently without any prompt.

Related Post

How To Change The Default RDP Number – A St

Connecting your Windows PC to a host computer remotely ...

Understanding the Function, Types & Roles

What is a DNS Server? The Domain Name System (DNS) h...

Setup a Master-to-Master Replication Between

Master-to-master replication is a highly beneficial fea...

Setting Up a Robots.txt File on Linux Server

The robots.txt file is an important component for manag...

How to Test Disk Speed Using the Linux Comman

In today's fast digital world, how fast your computer's...

Features of Microsoft Management Console (MMC

If you're a Windows Server administrator, you know how ...

Leave a Comment