16.8 C
New York
Tuesday, September 19, 2023
HomeLinuxHow to Delete Files and Directories in Linux

How to Delete Files and Directories in Linux

Deleting files and directories is an essential skill you must have as a Linux user. While it’s not hard, you can get more out of the needed Linux commands once you learn how to use them properly.

In this tutorial, we’ll show you the different ways you can delete files and directories in Linux.

Let’s get started!

Delete Files and Directories Using the Command Line

First, let’s learn how you can do this with the help of Linux commands. It may seem difficult to you at first compared to using the GUI. But once you see the details, you’ll see how powerful they are in deleting files and directories.

Delete Files using the Command Line

The simplest way to delete a file is using the rm command. If you want to delete a file in the current directory, you only pass the file name to the command like this:

$ rm myfile
delete file linux
Delete a file on Linux

If the target file is not in the current directory, you must pass the full file path as an argument. See the below command:

$ rm ./full//file/location/filename

Suppose you have a file in the Documents directory inside another directory called MyFiles. And you’re in the home directory. The way you need to write the command is like this:

$ rm ./MyFiles/Documents/myfile

You can also delete multiple files using the rm command. To do that, you need to pass each filename to the command.

$ rm myfile1 myfile2 myfile2
delete multiple files on linux
Delete multiple files on Linux

What if you want to delete dozens of files, and they are of the same file type? It’s time-consuming to write down the name of each file, right? To delete all files having the same file extensions, you can take advantage of wildcards.

By using the * wildcard, you can delete all files of the same type in this way:

$ rm *.txt
delete files using wildcards
Delete files using wildcards on Linux

This command will delete all text files present in the current directory. Another useful wildcard is to add ? after the * wildcard. See the below command:

$ rm *.?

If a file contains a single character extension, the above command will delete it. So, for example, if you have files like “text.a”, “image.b”, audio.c”, all such files will be deleted.

However, using wildcards could be risky. You may not notice an important file that gets deleted. To tackle that, you can pass the -i flag. This will ask for your confirmation when deleting each file. Use the command like this:

$ rm -i *.txt
delete files interactively in linux
Delete files interactively in Linux

If you are trying to delete write-protected files, you will get this confirmation option automatically. By pressing y or n, you can decide whether to delete them or not. You can bypass this by adding the -f flag, as shown below:

$ rm -f myfile

The -f flag forces any file to be deleted, even if it’s write-protected.

Another popular command for deleting files is unlink. It doesn’t have the -f flag like rm. So that makes it safer to use. You can delete a file using the unlink command like this:

$ unlink myfile
unlink command to delete file
unlink command to delete a file

This command can only be used for deleting a single file at a time.

The last command we’d like to show you is the shred command. Unlike the previous two commands, this command overwrites the file content before deleting it. This prevents someone from recovering the file later on. To delete the file afterward, you need to pass the -u flag. So the command looks like this:

$ shred -u myfile
$ shred -u myfile1 myfile2 myfile3
shred command to delete file
shred command to delete files

Delete Directories using the Command Line

You can delete directories with the rm and rmdir commands.

To delete an empty directory using the rm command, you have to pass the -d flag, as shown in this command:

$ rm -d mydirectory
delete directory on Linux
Delete directories on Linux

In the same way, you can delete multiple empty directories by passing each directory name to the command:

$ rm -d mydirectory1 mydirectory2 mydirectory3
delete multiple directories on Linux
delete multiple directories on Linux

If the directory you want to delete is not in the current one, you need to specify the full path, like this:

$ rm -d /path/to/the/directory

However, you can’t delete non-empty directories like this. To delete directories containing files and other directories, you use the -r flag, like this:

$ rm -r mydirectory
delete directories recursively on Linux
delete directories recursively on Linux

Similar to files, if a directory is write-protected, you will be asked for confirmation. To proceed, enter y. To suppress this prompt, you can again pass the -f flag:

$ rm -f directory 

You can also combine flags to achieve more results. So if you want to delete write-protected directories that contain other files or directories, use this command:

$ rm -rf directory 

However, use this command sparingly, as this can be of significant risk.

The second most used command to delete directories is rmdir. To delete an empty directory, run this command:

$ rmdir directory
rmdir command to delete directories
rmdir command to delete directories

To delete multiple directories using the command, pass all the directory names like this:

$ rmdir first_dir second_dir third_dir
delete multiple directories with rmdir
delete multiple directories with rmdir

If the targeted directory is not in the current directory, then you need to specify the path to the directory:

$ rmdir /path/to/the/target/directory

A useful way to use rmdir is when you want to delete a whole path of empty directories. For that, you use the -p flag. This will remove the target directory, then its parent directories. The command looks like this:

$ rmdir -p parent_dir/intermediate_dir/target_dir
delete parent directories on Linux
delete parent directories on Linux

This will remove three directories, starting from target_dir to parent_dir. But if there is any file or non-empty directory, this command will fail. 

In a situation where you want to delete multiple directories with their parent directories, if even one directory isn’t empty, the command will show an error. To solve this, you can use the command like this:

$ rmdir -p --ignore-fail-on-non-empty path/to/dir1 path/to/dir2

Now if a directory is not empty, rmdir will ignore that and go to the next directory and delete it.

Delete Files and Directories Using a Utility Tool

So far, we’ve shown you all the ways to delete files and directories in Linux using various commands. Note that these methods remove them permanently from your system. What if you want to move them to the Trash first before you want to delete them?

You can do that with a tool named trash-cli. Firstly, install the tool with this command: 

$ sudo apt-get install trash-cli

To move a file or directory to the trash, use these commands:

$ trash-put myfile.txt
$ trash-put mydirectory

Afterward, you can delete the files and directories from the trash. To empty the whole trashcan, use this command:

$ trash-empty

If you want to remove only an individual file from the trashcan, use this command:

$ trash-rm myfile.txt

This tool allows more flexibility when deleting your files and directories. 

Final Thoughts

And that’s how you can delete files and directories in Linux. You can either use the built-in commands or use the mentioned tool in case you want to trash them instead. For files you don’t need anymore, you can permanently delete them using the commands we’ve shown you.

If you have any questions, feel free to let us know in the comments below.

Zunaid Ali
Zunaid Ali
Meet Zunaid Ali, a passionate Linux enthusiast with an extraordinary skill set. From seamless server management to adept network troubleshooting, Zunaid is your go-to source for comprehensive how-to guides.

You May Like It!

Trending Now