How to delete files in Linux?

Are you trying to figure out how to delete a file in your Linux Server?

This guide will help you.

Removing or deleting a file or directory in Linux operating systems can be implemented by using the rm command or unlink command.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Software Installation tasks on their Server.
In this context, you will learn how to delete a given file on a Linux system using the command line option.

How to remove a file using the rm command syntax?

Basically, rm (short for remove) is a Linux command which can be used to delete files from a Server filesystem. Generally, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). The command syntax is as follows to delete the specified files and directories;

rm {file-name}
rm [options] {file-name}
unlink {file-name}
rm -f -r {file-name}


Here;

i. -f: This will forcefully remove file
ii. -r: This will help to remove the contents of directories recursively

When rm command used just with the file names, rm deletes all given files without confirmation by the user.

For instance, to remove or delete a file, lets say file1.txt, execute the command;

rm file1.txt


To delete multiple files in Linux,let's say file1.mp4, file2.doc and file3.txt, then execute;

rm file1.mp4 file2.doc file3.txt
ls

How to delete all files recursively in Linux?

To remove all files and sub-directories from a directory recursively, execute the command;

rm -rf mydir

Where "mydir" is the name of the directory you want to delete.

How to delete a file and prompt before removal in Linux?

To request confirmation before attempting to remove each file, pass the -i option to the rm command as shown below:

rm -i file_name

Where "file_name" is the name of the file.

To get a confirmation of file removal or deletion, you can use the Force rm command "-v" as seen below;

rm -v file1.txt file2.doc
removed 'file1.txt'
removed 'file2.doc'

How to delete empty directories?

To delete or remove empty directory use rmdir command and not the rm command as shown below:

rmdir mydirectory
rmdir dirNameHere
rmdir docs

How to read a list of all files to delete from a text file?

The rm command is often used in conjunction with xargs to supply a list of files to delete. Create a file called file.txt. The execute;

cat file.txt


List of to delete:

file1
/tmp/file2.txt
~/data.txt


Next,  delete all file listed in file.txt by executing the command:

xargs rm

How to delete a file named -file4.txt or a directory named -file4?

To delete a file called -file4.txt, execute:

rm -- -file4.txt

OR

rm -- ./-file4.txt

To delete a directory called -file4, execute:

rm -r -f -- -file4

The two -- dashes tells rm command the end of the options and rest of the part is nothing but a file or directory name begins with a dash.

Warning.

Never run rm -rf / as an administrator or normal Linux user.
[This may crash your computer]
The following command will delete all files on your computer if executed;

rm -rf /
rm -rf *


[Need urgent support to Install Software on your Ubuntu Server? We are available to help you today.]

This article will guide you on how to delete files on Linux operating systems.

Related Posts