How to Remove (Delete) Files and Directories in Linux? Easy Methods

How to Remove (Delete) Files and Directories in Linux?

How to Remove (Delete) Files and Directories in Linux

If you are searching the Internet ‘How to Remove (Delete) Files and Directories in Linux‘. You are now in the right place and share an incredible method with you. How to delete (delete) a directory in Linux In this tutorial, we will show you how to use the rm, unlink and rmdir commands to delete files and directories in Linux.

You may get a message similar to “rmdir: ‘dir”: directory is not empty “if you try to delete a directory using a command like rmdir and you cannot delete the directory. You must compare” mydir “with the directory name in the illustration in Above: Running the command will mutually delete all files and subdirectories in the directory.

How to Remove Files and Directories In Linux?

If you want to remove (or delete) any file in Linux from the command line, use either the rm (remove) or unlink command.

The unlinkthe command will make you able to remove only a single file, while with rm you will easily remove multiple files at once.

Note: Be extremely careful while removing files or directories, because once you have deleted the file, it cannot be recovered easily.

  • To delete a single file, use the rm or unlinka command followed by the file name:
    unlink filename
    rm filename

    If the file is already write-protected, you will see a prompted confirmation, as shown below. To remove or delete the file type y and hit Enter. And in case, if the file is not write-protected, it will be deleted without any prompting notification or warning.


  • rm: remove write-protected regular empty file 'filename'?
  • In order to delete multiple files at the same time, use the rma command followed by the file names separated by space.
  • rm filename1 filename2 filename3

    There is also a use of wildcard (*) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:

    rm *.pdf

    When using regular extensions, first list the files with the ls command to see which files will be deleted before running the rm command.

  • Use rm with the -i option to validate each file before deleting it:
    rm -i filename(s)
  • If you want to remove the files without prompting even if the files are write-protected then pass the -f (force) option to the rm command:
    rm -f filename(s)
  • You can also combine rm options. For example, to remove all .txt files in the current directory without a prompt in verbose mode, use the following command:

    rm -fv *.txt

How to Remove Directories (Folders)?

In Linux, you will easily remove/delete directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories, while with rm you can delete directories and their contents recursively.

  • To remove an empty directory, use either rmdir or rm -d followed by the directory name:
    rm -d dirname
    rmdir dirname
  • If you want to remove non-empty directories and all the files within them, use the rm command with the-r (recursive) option:
    rm -r dirname

    If a directory or file in the directory is write-protected, you will be asked to confirm the deletion.

  • To remove non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:
    rm -rf dirname
  • To remove multiple directories at once, use the rm -ra command followed by the directory names separated by space.
    rm -r dirname1 dirname2 dirname3

    As with files, you can also use a wildcard (*) and regular extensions to match multiple directories.

How to Remove (Delete) Directory in Linux?

On Linux systems, there are different ways to delete directories. It will delete files and folders using the system graphical user interface if you use Windows file managers like Gnome Files or KDE Dolphin. But you can uninstall directories (folders) from the command line if you are running on a headless server or if you want to disable multiple directories at once.

An option that will ask you once before deleting.

rm -rI dir1

To remove the directory type y and hit Enter.

rm: remove 1 argument recursively? y

You can also use regular extensions to join and remove multiple directories. For example, to remove all top-level directories from the current directory ending with _bak, you would use the following command:

rm -r *_bak

Using regular extensions when deleting directories can be risky. It is recommended that you first list the directories with the ls command so that you can see which directories will be removed before running the rm command.

Removing Directories with “find

find is a command-line utility that allows you to search for files and directories according to a given expression and perform an action on each corresponding file or directory.

The most common scenario is to use the “search” command to remove model-based directories. For example, to remove all directories ending with _cache in the current working directory, you should run:

find . -type d -name '*_cache' -exec rm -r {} +

Let’s analyze the command above:

  • / dir: recherche récursivement dans le répertoire de travail courant (.).
  • -type d: restreindre la recherche aux répertoires.
  • -name ‘* _cache’ – recherche uniquement les répertoires se terminant par _cache
  • -exec: exécute une commande externe avec des arguments facultatifs, dans ce cas, qui est rm -r.
  • {} +: Ajoutez les fichiers trouvés à la fin de la commande rm.

Removing All Empty Directories:

To remove all empty directories in a directory tree you would run:

find /dir -type d -empty -delete

Here is an explanation for the options used:

  • / dir: searches recursively in the / dir directory.
  • -type d: restrict the search to directories.
  • -empty: restrict the search to empty directories only.
  • -delete: delete all empty directories found in the sub-tree. -delete can only delete empty directories.

Use the -delete option with extreme caution. The search command line is evaluated as an expression, and if you add the -delete option first, the command will remove everything below the start points you specified.

Always try the command first without the -delete option and use -delete as the last option.

/bin/rm: Argument list too long:

This error message appears when you use the rm command to delete a directory that contains a large number of files. This happens because the number of files is greater than the system limit for the size of the command line argument.

There are several different solutions to this problem. For example, you can cd to the directory and manually or through a loop to remove subdirectories one by one.

The simplest solution is to first delete all the files in the directory using the find command, then delete the directory:

find /dir -type f -delete && rm -r /dir

Conclusion

At this point, you should have a good understanding of the use of the Linux rm, rmdir, and unlink commands, and you should be able to safely remove files and directories from the command line. With rm and findy you can delete directories based on different criteria quickly and efficiently. Deleting directories is a simple and easy process, but you must be careful not to delete important data.

Summary
Review Date
Reviewed Item
How to Remove (Delete) Files and Directories in Linux?
Author Rating
51star1star1star1star1star
Software Name
How to Remove (Delete) Files and Directories in Linux
Software Name
Windows
Software Category
Linux

Leave a Reply

Your email address will not be published. Required fields are marked *