How to Delete Files and Folders in Linux

How to Delete Files and Folders in Linux

How to Remove a Directory in Linux

To remove a directory in Linux, use the rm command with the -r option for recursive removal. For example, to remove a directory named mydir, you would use the following command:

rm -r mydir

This will remove the directory and any files or subdirectories it contains. Be careful with this command, as it cannot be undone. If you want to remove a directory but keep its contents, you can use the mv command to move it to a different location or rename it.

Note: If the directory is not empty, you may need to use the sudo command to permit yourself to delete it.

For example:

sudo rm -r mydir

The rm command has several options you can use it with; here are the most common ones:

OptionDescription
-fForce removal of files without prompting for confirmation
-iPrompt for confirmation before removing each file
-IPrompt for confirmation, but only once, before removing more than three files, or when removing recursively
-rRecursively remove directories and their contents
-dRemove empty directories
-vVerbose output, showing each file as it is removed
–helpShow a help message and exit
–preserve-rootDo not remove / (root) directory, even if it is empty
–no-preserve-rootRemove / (root) directory, even if it is empty (default)
–dirRemove directories, even if they are non-empty (use with -f)
–one-file-systemWhen removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument

Note that these options can be combined, so for example, you could use rm -rf to force removal of files and directories without prompting for confirmation. However, this is generally not recommended, as it can lead to accidental data loss. It’s always a good idea to use the -i option to confirm each file before removing it.

And lastly, these options may vary depending on the version of Linux you are using. You can always use the --help option to see a complete list of options for your version of the rm command.

How to Remove an Empty Directory

When you delete a file or directory in Linux, it is permanently deleted and cannot be recovered unless you have a backup. The rmdir command allows you to remove empty directories in a safe and controlled manner, without the risk of accidentally deleting important files or directories.

Using the rmdir command is particularly useful if you want to clean up your file system by removing empty directories that are no longer needed.

rmdir mydirectory

If the directory is not empty, you will get an error message telling you that the directory is not empty and that the rmdir command can only be used on empty directories.

The rmdir command has a few options that you can use to modify its behavior.

Some of the most common options include:

  • -p: This option allows you to remove a directory and any of its empty parent directories. This can be useful if you have a series of empty directories that you want to remove all at once.
  • -v: This option makes the rmdir command more verbose, meaning that it will print additional information to the command prompt as it runs. This can be useful if you want to see what the command is doing as it executes.
  • -ignore-fail-on-non-empty: This option tells the rmdir command to ignore any errors that occur if the directory is not empty. This can be useful if you want to try to remove a directory and its contents even if it is not empty.

How to Delete Files

The rm command is universal regarding removals, so it works for directories and individual files. Always use the -i option so that you get prompted before a file gets deleted, and ideally – when doing large-scale removals, make a backup, so you don’t end up without any files at all!

To delete a file with the rm command, you would use the following syntax:

# remove a file without a prompt

rm myfile.txt

# remove a file and have the system ask you to confirm

rm -i myfile.txt

You can also do this for specific file extensions:

# delete all .txt files in the current directory

rm *.txt

# delete all .jpg files in the current directory

rm *.pdf

# delete all files that start with a word

rm prefix* # this would delete all files starting with the word 'prefix'

Summary

The Linux command line offers a variety of tools for managing files and directories. One such tool is the rm command, which can be used to delete both files and non-empty directories. To delete empty directories, you can use the rmdir command.

It’s important to note that, unlike in some other operating systems, there is no recycle bin or trash folder in Linux.

Once you delete a file or directory using these commands, it is permanently deleted, so it’s important to use them carefully or to create backups of any important files.