Linux Remove or Delete Files and Directories using Terminal Commands


⌛ Read time: 5 minutes | Topic Complexity: Easy

Delete files and directories in Linux

There are multiple commands and options that you can make use of depending upon your use case to delete a file, multiple files, a directory, a group of directories, or nested directories with files in them.

List of commands

  1. rm - remove files or directories
  2. unlink - call the unlink function to remove the specified file
  3. rmdir - remove empty directories

Let's take a look at each of them,

How to Remove a File?

  • Make use of the rm command for deleting a file in Linux,

    Example:
    # ls -l
    -rw-r--r-- 1 root root 0 Nov  5 06:50 file1.txt
    -rw-r--r-- 1 root root 0 Nov  5 06:50 file2.csv
    -rw-r--r-- 1 root root 0 Nov  5 06:51 image.png

    We have three files, lets remove file1.txt,

    # rm file1.txt
    #
    1 Delete a file using rm command

    As you can see, after running the rm command followed by the filename and hitting enter, the prompt came back without any error or message, so the file was deleted, you can confirm using the ls command again,

    # ls -l
    
    -rw-r--r-- 1 root root 0 Nov  5 06:50 file2.csv
    -rw-r--r-- 1 root root 0 Nov  5 06:51 image.png

How to Delete Multiple Files at once?

  • In Linux to delete files at once, you can then use the rm command followed by all the files names you want to remove separated by a space.

    Example:
    # rm file1.txt file2.csv image.png
    #
Delete Multiple files at once using rm command

How to Delete Multiple Files using Regular Expression (Regex)?

  • # ls -l
    
    -rw-r--r-- 1 root root 0 Nov  5 07:15 myfile1.txt
    -rw-r--r-- 1 root root 0 Nov  5 07:15 file2.txt
    -rw-r--r-- 1 root root 0 Nov  5 07:15 file3.csv
    -rw-r--r-- 1 root root 0 Nov  5 07:15 file4.csv

    We have 4 files, 2 of them are of type .txt and two of type .csv, lets's try .csv files using a regular expression,

    # rm *.csv
    #
    
    # ls
    file1.txt  file2.txt
    Remove files using regex

    Now let us delete files that start with letter m,

    # rm m*
    #
    
    # ls
    file2.txt
Remove file using regular expression Linux

Permission Denied Error while deleting a file

  • Now let us switch to another user and try to delete file2.csv

    # su c2c
    
    [c2c@034410af48d7 myFiles]$ ls -l
    
    -rw-r--r-- 1 root root 0 Nov  5 06:50 file2.csv
    -rw-r--r-- 1 root root 0 Nov  5 06:51 image.png
    
    [c2c@034410af48d7 myFiles]$ rm file2.csv
    rm: remove write-protected regular empty file 'file2.csv'? y
    rm: cannot remove 'file2.csv': Permission denied

    We get a "Permission denied" error when you try to delete the file because this file does not belong to the current user. It belongs to root as we can see.

    Solution: Either use SUDO Command to assume root role and delete the file, or switch to root user using su command.


How to force remove file(s)?

  • At times you may get warning messages and asked to say y/n before deleting the file, such as "file is write protected", if you want to skip such messages and go ahead with removing the file(s), use the -f or --force option.

    Examples:
    # rm --force file1.txt
    # rm -f file2.txt

How to get prompted/asked before deleting a file?

  • At times we want to be double sure before deleting file or files, especially when using regular expressions, in such cases, it's always better to add a -i or --interactive option with the rm command,

    Example:
    # rm -i f*
    rm: remove regular empty file 'file1.txt'? y
    rm: remove regular empty file 'file2.txt'? y
    rm: remove regular empty file 'file3.csv'? n
    rm: remove regular empty file 'file4.csv'? n

    As you can see in the above example, I was prompted for all the files that were to be deleted, I typed in y to remove a file and n to not remove it


Removing Directories

You can make use of the rm command to remove directories (folders) as well, but if you use it without any option you will get an error - rm: cannot remove, is a directory

Example:
# # rm myDirectory 
rm: cannot remove 'myDirectory': Is a directory
Remove Directory using rm command

How to Remove/Delete an Empty Directory?

  • To remove a directory make use of the rm command with -d or --dir option followed by the directory name,

    Example:
    # rm -d myDirectory
    #

    Again, just the removing a file, if the prompt comes back without any error, then the directory is successfully deleted.


How to Remove Multiple Empty directories at once?

  • Just like removing files at once, you can pass in multiple directory names followed by space with rm -d command,

    Example:
    # rm -r dir1 dir2 dir3
    #
    Remove multiple empty directories usin rm command

Directory not empty error!

  • What happens when you try to delete a directory that has files in them? You get an error even when you use the -d option,

    # rm -d dir1/
    rm: cannot remove 'dir1/': Directory not empty

How to Delete a non-empty Directory with files and sub-directories?

  • If a directory contains files and sub-directories in them, you will need to make use of the -r or --recursive option along with the rm option,

    Example:
    # tree -L 3 myDir/
    myDir/
    |-- file1.txt
    |-- file2.csv
    `-- myDir1
        `-- file3.txt

    As you can see, I have used the tree command just to show that the nestedness of the directory myDir, lets try to delete this directory,

    # rm -r myDir
    #

How to get prompted when deleting directories?

  • rm -r can get very risky if you are not mindful when you run a command, imagine being a root user using this command and by mistakenly removing the directories that belong to the Linux FHS, so it could be wise to check what are you deleting and if in doubt make use of the -i option,

    # rm -r -i myDir/
    rm: descend into directory 'myDir/'? y
    rm: descend into directory 'myDir/myDir2'? y
    rm: remove regular empty file 'myDir/myDir2/file3.csv'? y
    rm: remove directory 'myDir/myDir2'? y
    rm: remove regular empty file 'myDir/file2.txt'? y
    rm: remove regular empty file 'myDir/file1.txt'? y
    rm: remove directory 'myDir/'? y

Using the unlink command to delete files

Though the rm command is what you will use the most to delete files and directories in Linux using Command Line, there is one more command unlink that you can make use of

Example:
# unlink file1 
#

Note: Like rm command, you cannot delete multiple files at a time using unlink, you will get an error,

# unlink file1 file2
unlink: extra operand 'file2'
Try 'unlink --help' for more information.

You cannot delete a directory either using unlink,

# unlink myDir
unlink: cannot unlink 'myDir': Is a directory

Aslo there are no options that you can use with unlink, check the help page,

unlink --help
Usage: unlink FILE
  or:  unlink OPTION
Call the unlink function to remove the specified FILE.

      --help     display this help and exit
      --version output version information and exit

Using rmdir to remove directories

rmdir is a command you can make use of just to delete an empty directory or multiple empty directories in Linux,

# rmdir myDir
#
# rmdir myDir1 myDir2
#
# rmdir myDir/
rmdir: failed to remove 'myDir/': Directory not empty


tl;dr Summary

  • So to conclude, rm is the command that you should use to remove files and directories in Linux,

    rm -i : to get a prompt before deleting files/directories.
    rm -d : to remove an empty directory.
    rm -r : to delete directories that are not empty and nested, recursively.
    rm -f : to force delete files or directories.
    


✏️ P.S: rm is a General User Commands you will find binary under /usr/bin/rm

rm command can be used for all distributions of Linux (Debian) without the need to install any package,
  • Fedora KDE
  • Ubuntu
  • Amazon Linux
  • Alpine Linux
  • Linux Mint Cinnamon
  • Kali Linux

Work on macOS as well as the base for both Linux and macOS is Unix.

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap