Contact Us

How to Remove (Delete) Files and Directories in Linux

Send Us A Message

Remove Files and Directories Linux

If you looking on the internet a How to Remove Files and Directories Using Linux Command Line So, you come to the right place now day share with you an amazing method How to Remove (Delete) Directory in Linux In this tutorial, we will show you how to use the rmunlink, and rmdir commands to remove files and directories in Linux.

remove directory linux

You can get a prompt similar to “rmdir:’dir’: Directory not empty’ if you try to remove a directory using a command such as rmdir and can not delete the directory. You should compare “mydir” with the directory name in the illustration above. Running the order will delete all files and subdirectories from the directory reciprocally. you can also check out the How to Rename Files in Linux.

How to Remove Files and Directories In Linux

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.

The unlink the command allows you to remove only a single file, while  rm you can remove multiple files at once.

Be extra careful when removing files or directories because once the file is deleted, it cannot be easily recovered.

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

    If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y and hit Enter. Otherwise, if the file is not write-protected, it will be deleted without prompting.

     
    rm: remove write-protected regular empty file 'filename'?
  • To delete multiple files at once, use the command followed by the file names separated by space.
  • rm filename1 filename2 filename3

    You can also use a wildcard (*) and regular expansions to match multiple files. For example, removing all .pdf Files in the current directory, use the following command:

    rm *.pdf

    When using regular expansions, first list the files with the ls command so that you can see what files will be deleted before running the rm command.

  • Use the rm with the -i Option to confirm each file before deleting it:
    rm -i filename(s)
  • To remove files without prompting, even if the files are write-protected, pass the -f (force) option to the rm command:
    rm -f filename(s)
  • You can also combine rm options. For example, removing 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 can remove/delete directories with the rmdir and rm.

rmdir is a command-line utility for deleting empty directories while with rm you can remove 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
  • 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 a file within the directory is write-protected, you will be prompted 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 -r a command followed by the directory names separated by space.
    rm -r dirname1 dirname2 dirname3

    Same as with files you can also use a wildcard (*) and regular expansions to match multiple directories.

 

How to Remove (Delete) Directory in Linux

Within Linux systems, there are different ways to delete directories. You will delete files and folders using the graphical user interface of the system if you use the Windows file manager such as Gnome’s Files or KDE’s Dolphin. But you can uninstall directories (folders) from the command line if you are running on a headless server or you want to disable several directories at once.

delete directory linux

We should clarify in this article how to uninstall Linux files by using rmdir, rm and finding commands.

Before You Begin

The directory is actually moved to the Trash anytime you delete a directory using a mobile file management method.

Be very cautious when deleting files or folders from the command line, as the directory cannot be entirely restored after it has been deleted using the commands mentioned in this article.

Deleting a directory includes written authorization on the output of the directory on most Linux file systems. If not, the error “Operation not allowed” you will receive.

Directory names with a space in them must be escaped with a backslash (/).

Removing Directories with rmdir

rmdir is a command-line utility for deleting empty directories. It is useful when you want to delete a directory only if it is empty, without needing to check whether the directory is empty or not.

To delete a directory with, type the command followed by the name of the directory you want to remove. For example, to delete a directory named dir1 you would type:

rmdir dir1

If the directory is not empty, you will get the following error:

rmdir: failed to remove 'dir1': No such file or directory

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm

rm is a command-line utility for deleting files and directories. Unlike rmdir the rm a command can delete both empty and non-empty directories.

By default, when used without any option rm does not remove directories. To delete an empty directory, use the -d (--dir) option, and to delete a non-empty directory and all of its contents, use the -r (--recursive Or -R) option.

For example, to delete a directory named dir1 along with all of its contents, you would type:

rm -r dir1

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

rm -rf dir1

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:

rm -r dir1 dir2 dir3

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. If the directory contains a lot of files, this can be a little annoying, so you may consider using the -I option what will prompt you only once before proceeding with the deletion.

rm -rI dir1

To remove the directory type y and hit Enter.

rm: remove 1 argument recursively? y

You can also use regular expansions to match and delete multiple directories. For example, to remove all first-level directories in the current directory that ends with _bak, you would use the following command:

rm -r *_bak

Using regular expansions when removing directories may be risky. It is recommended first to list the directories with the ls command so that you can see what directories will be deleted before running the rm command.

Removing Directories with find

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

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run the following:

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

Let’s analyze the command above:

  • /dir – recursively search in the current working directory (.).
  • -type d – restricts the search to directories.
  • -name '*_cache' – Search only directories that end with _cache
  • -exec – executes an external command with optional arguments, in this case, that is rm -r.
  • {} + – appends the found files to the end of the rm command.

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 of the options used:

  • /dir – recursively search in the /dir directory.
  • -type d – restricts the search to directories.
  • -empty – restricts the search only to empty directories.
  • -delete – deletes all found empty directories in the subtree. -delete Can delete only empty directories.

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

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

/bin/rm: Argument list too long

This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on 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 using a loop to remove sub-directories one by one.

The easiest solution is first to delete all files within the directory with the find command and then delete the directory:

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

Conclusion

By now, you should have a good understanding of how to use the Linux rmrmdir and unlink commands, and you should be able to safely remove files and directories from the command line. With rm and find you can delete directories based on different criteria quickly and efficiently. Deleting directories is a simple and easy process, but you must be cautious not to delete important data.

Send Us A Message

Best Free PDF Reader Apps for Android, iOS, PC & Mobile Devices 2025

Compare the best free PDF reader software and apps for Windows PC, Mac, Android phones, iOS devices, and tablets. Download premium tools for instant digital magazine reading on any mobile device.

Choosing the best PDF reader app for your Android, iOS, or PC device enhances performance, security, and mobile reading experience. Compare free software downloads and premium subscription options.

Windows & Mac Software

Download free apps for PC and laptop

Android & iOS Apps

Best mobile apps for smartphones & tablets

Browser Tools

Chrome, Firefox, Safari PDF viewers

App Settings

Optimize software for mobile devices

Best PDF Reader Software for Windows PC & Mac - Free Download 2025

Adobe Acrobat Reader - Free Download for Windows & Mac

Download Adobe Acrobat Reader free software for Windows 10, Windows 11, and Mac computers. Industry-leading PDF app with premium subscription options for professionals and businesses.

Premium Features
  • Free download for PC and Mac
  • Professional annotation tools
  • Cloud storage integration
  • Mobile app sync (Android/iOS)
Subscription Costs
  • Premium features require paid plan
  • Resource-heavy on older PCs
  • Subscription pricing varies

Top PDF Software Comparison - Free vs Paid Apps 2025

Software/App Platform (PC/Mobile) Price/Subscription Rating
Adobe Acrobat Reader Windows, Mac, Android, iOS Free / Premium $14.99/mo 9.5/10
Foxit PDF Reader Windows PC, Mobile Apps Free / Pro $139 9.0/10
Sumatra PDF Windows 10/11 Free Download 8.5/10
Preview (Built-in App) MacOS, iOS Devices Free with Mac/iPhone 8.0/10

Foxit Reader - Best Free Alternative Software

Download Foxit PDF Reader free for Windows PC and Mac. Lightweight software alternative to Adobe with mobile app versions for Android and iOS devices. No subscription required for basic features.

Best PDF Reader Apps for Android & iOS - Free Mobile Downloads 2025

Adobe Acrobat Mobile App - Android & iOS Download

Download free Adobe Acrobat mobile app for Android phones, iOS devices, tablets, and iPads. Premium subscription unlocks professional features with cloud storage sync across all devices.

Mobile Editing Tools

Annotate PDFs on Android & iOS

Cloud Storage Apps

Sync files across mobile devices

OCR Technology

Convert scans to text on phone

Mobile Sharing

Send files from Android/iOS apps

Xodo PDF - Best Free App for Android & iOS Devices

Download Xodo free PDF reader app for Android smartphones, iOS devices, and tablets. Top-rated mobile software with no subscription fees or in-app purchases required.

Free Mobile App Features
  • Completely free - no subscription
  • Available on Android & iOS
  • Fast mobile performance
  • Works offline on phone/tablet
Premium Upgrades
  • Advanced editing requires premium
  • Cloud storage limited in free app

Mobile App Tip: Download PDF reader apps that support offline reading on Android phones and iOS devices to access magazines without internet connection or data usage.

Best Web Browsers for PDF Reading - Chrome, Firefox, Safari 2025

Google Chrome Browser - Free Download with Built-in PDF Viewer

Download Google Chrome free browser for Windows PC, Mac, Android, and iOS devices. Built-in PDF viewer with instant rendering - no additional software or app downloads required.

Chrome PDF Viewer Features - Free Built-in Tool

  • Free download for PC and mobile
  • No plugin or app installation needed
  • Works on Windows, Mac, Android, iOS
  • Google Drive cloud integration
  • Fast PDF rendering on mobile devices

Mozilla Firefox - Free Browser Download for All Devices

Download Firefox free browser with PDF.js viewer for Windows, Mac, Linux PC, Android phones, and iOS devices. Privacy-focused alternative to Chrome with customizable mobile apps.

Browser (Free Download) Platform/Devices Mobile Performance Best For
Google Chrome Windows, Mac, Android, iOS Excellent on Mobile General users & businesses
Mozilla Firefox All Platforms & Devices Very Good Privacy-focused mobile users
Microsoft Edge Windows PC, Android, iOS Excellent Windows 10/11 users
Safari Browser Mac, iPhone, iPad (iOS) Good on Apple Devices Mac & iOS device owners

Browser Extension Tip: Download free browser extensions and add-ons to enhance PDF viewing on Chrome, Firefox, and Edge browsers for PC and mobile devices.