How to Delete Files and Directories in the Linux Terminal

Key Takeaways

  • The rm and rmdir commands are used to delete files and directories on Linux and other Unix-like systems. They are similar to del and delttee commands in Windows and DOS.
  • Files and directories deleted using rm and rmdir are immediately removed from the computer, without being moved to the Trash. To restore accidentally deleted files, a backup must be used.
  • The rm command can delete single or multiple files, with options to use wildcards for selecting groups of files. The rmdir command can only delete empty directories, while the rm -r option is used to delete directories and all their contents. Caution is advised when using the rm -rf command, as it can cause data loss or system malfunction.


The rm and rmdir commands delete files and directories on Linux, macOS, and other Unix-like operating systems. They’re similar to the del and deltree commands in Windows and DOS. These commands are very powerful and have quite a few options.

It is important to note that files and directories deleted using rm and rmdir do not get moved to the Trash. They are immediately removed from your computer. If you accidentally delete files using these commands, the only way you’ll be able to restore them is from a backup.


How to Delete Files on Linux with rm

The simplest case is deleting a single file in the current directory. Type the rm command, a space, and then the name of the file you want to delete.

rm file_1.txt

If the file is not in the current working directory, provide a path to the file’s location.

rm ./path/to/the/file/file_1.txt

You can pass more than one filename to rm. Doing so deletes all of the specified files.

rm file_2.txt file_3.txt

Wildcards can be used to select groups of files to be deleted. The * represents multiple characters and the ? represents a single character. This command would delete all of the png image files in the current working directory.

rm *.png

This command would delete all files that have a single character extension. For example, this would delete File.1 and File.2, but not File.12.

rm *.?

If a file is write-protected you will be prompted before the file is deleted. You must respond with y or n and press “Enter.”

rm command with write-protected file

To reduce the risk of using rm with wildcards use the -i (interactive) option. This requires you to confirm the deletion of each file.

rm -i *.dat

rm command in interactive mode

The -f (force) option is the opposite of interactive. It does not prompt for confirmation even if files are write-protected.

rm -f filename

How to Delete Directories on Linux with rm

To remove an empty directory, use the -d (directory) option. You can use wildcards (* and ?) in directory names just as you can with filenames.

rm -d directory

Providing more than one directory name deletes all of the specified empty directories.

rm -d directory1 directory2 /path/to/directory3

To delete directories that are not empty, use the -r (recursive) option. To be clear, this removes the directories and all files and sub-directories contained within them.

rm -r directory1 directory2 directory3

If a directory or a file is write-protected, you will be prompted to confirm the deletion. To delete directories that are not empty and to suppress these prompts, use the -r (recursive) and -f (force) options together.

rm -rf directory

Care is required here. Making a mistake with the rm -rf command could cause data loss or system malfunction. It’s dangerous, and caution is the best policy. To gain an understanding of the directory structure and the files that will be deleted by the rm -rf command, use the tree command.

Use apt-get to install this package onto your system if you’re using Ubuntu or another Debian-based distribution. On other Linux distributions, use your Linux distribution’s package management tool instead.

sudo apt-get install tree

Running the tree command produces a simple to understand diagram of the directory structure and files beneath the directory from which it is run.

tree

output from tree command

You can also supply a path to the tree command to cause it to start the tree from another directory in the file system.

tree path/to/directory

The rm command also has --one-file-system, --no-preserve-root, --preserve-root options, but those are only recommended for advanced users. If you get something wrong, you could accidentally delete all your system files. Consult the command’s manual page for more information.

How to Delete Directories on Linux with rmdir

There is another command, called rmdir, that you can use to delete directories. The difference between rm and rmdir is that rmdir can only delete directories that are empty. It will never delete files.

The simplest case is deleting a single empty directory. As with rm, you can pass multiple directory names to rmdir , or a path to a directory.

Delete a single directory in the current directory by passing its name to rmdir :

rmdir directory

Delete multiple directories by passing a list of names to rmdir :

rmdir directory1 directory2 directory3

Delete a directory not in the current directory by specifying the full path to that directory:

rmdir /path/to/directory

If you try to delete a folder that is not empty, rmdir will give you an error message. In the following example rmdir successfully, and silently, deletes the clients directory but it refuses to delete the projects directory because it contains files. The projects directory is left exactly as it was and the files in it are untouched.

rmdir command with a non empty folder

When rmdir gives a “Directory not empty” error, it stops processing the directories that were passed to it on the command line. If you’ve asked it to delete four directories and the first one had files in it, rmdir would give you the error message and do nothing more. You can force it to ignore these errors with the --ignore-fail-on-non-empty option so that other directories are processed.

In the following example two folders have been passed to rmdir, these are work/reports and work/quotes . The --ignore-fail-on-non-empty option has been included in the command. The work/reports folder has files in it, so rmdir cannot delete it. The --ignore-fail-on-non-empty option forces rmdir to ignore the error and move on to the next folder it needs to process, which is work/quotes. This is an empty folder, and rmdir deletes it.

This was the command used.

rmdir --ignore-fail-on-non-empty work/reports /work/quotes

rmdir with --ignore-fail-on-non-empty option

You can use the -p (parents) option to delete a directory and to delete its parent directories too. This trick works because rmdir starts with the target directory and then back-steps to the parent. That directory should now be empty, so it can be deleted by rmdir, and the process repeats stepping back up the path that was provided to rmdir.

In the following example the command that is passed to rmdir is:

rmdir -p work/invoices

rmdir command with remove parents option

Both the invoices and the work directories are deleted, as requested.


Whether you’re using Bash or any other shell, Linux provides flexible and powerful commands for you to delete directories and files straight from the terminal command line. Some people prefer to have a workflow that revolves around the terminal. Others may have no choice in the matter. They may be working on servers without a GUI installed or on a remote session onto a headless system such as a Raspberry Pi. These commands are perfect for that group of people.

But whatever type of workflow you prefer, these commands lend themselves very well to being included in shell scripts. If a script is triggered by a cron job, it can help automate routine housekeeping tasks such as purging unwanted log files. If you investigate that use case, remember the power of these commands, test everything carefully, and always maintain a recent backup.

Source

      Guidantech
      Logo
      Shopping cart