- Linux recursively remove folders
- NAME
- SYNOPSIS
- DESCRIPTION
- OPTIONS
- AUTHOR
- REPORTING BUGS
- COPYRIGHT
- SEE ALSO
- How To Remove Files or Directories Recursively in Linux
- In this article
- Part 1. How To Remove Directory Recursively in Linux Using rm Command
- Examples Of How To Delete A Folder Recursively
- Delete Folders With Unique Name Characters
- Part 2. How To Delete Files Recursively in Linux Using rm Command
- Delete All Files Recursively
- Remove Files by Size Recursively
- Remove Files by Extension Recursively (File type)
- Recursively Deleting All Files Based On Permissions
- Delete Files Recursively Based on Their Modification Or Creation Time
- Part 3. What To Do If You’ve Accidentally Deleted a File or Folder in Linux?
Linux recursively remove folders
NAME
rm - remove files or directories
SYNOPSIS
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted. Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.
OPTIONS
Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred.
AUTHOR
Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.
REPORTING BUGS
Report rm bugs to bug-coreutils@gnu.org GNU coreutils home page: http://www.gnu.org/software/coreutils/> General help using GNU software: http://www.gnu.org/gethelp/> Report rm translation bugs to http://translationproject.org/team/>
COPYRIGHT
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
unlink(1), unlink(2), chattr(1), shred(1) The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command info coreutils 'rm invocation' should give you access to the complete manual.
© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.
How To Remove Files or Directories Recursively in Linux
No worries. Wondershare Recoverit can get back your lost files within 3 steps. The ultimate and professional Linux data recovery software is totally reliable and 100% safe.
A directory can have many subdirectories and files within it. Removing all files and subdirectories in a directory may be necessary when working with the Linux filesystem. It is referred to as recursive deletion. Use the rm command to recursively remove files or directories (also known as folders in Windows) in Linux. The rmdir command only removes empty directories. This guide will present the steps to recursively remove files and directories in Linux.
In this article
Part 1. How To Remove Directory Recursively in Linux Using rm Command
A summary of the rm command syntax for removing directories/folders recursively:
Command Syntax
Description
In Linux and Unix-like systems, everything is considered a file. In other words, «files» include photos, documents, directories/folders, SSD/hard drives, NIC, USB devices, keyboards, printers, and network communications.
Examples Of How To Delete A Folder Recursively
In this example, we will delete the data folder in the current home directory recursively:
Before removing the data directory, the specified /home/vivek/data/ directory will be emptied of all subdirectories, including their subdirectories and files. Unless the -f (force) option is specified on the command line, the user is prompted to remove any write-protected files in the directories:
Alternatively, you can use the command that follows:
To remove a folder whose name begins with a “ — ,” such as “ -dsaatia ,” use one of the following commands:
You can also run the following command:
To see verbose outputs, we can use the -v option. In other words, the rm command on Linux will explain what happens to our files and folders. As an example:
rm -rfv /path/to/dir1
rm -r -f -v /home/vivek/oldpartpics
Delete Folders With Unique Name Characters
Your folders and files may contain while spaces, semicolons, backslashes, and other characters in Linux. As an example:
Assume we have a folder called «Our Sales Data» and » baddir# » or » dir2 ;# » in it. So, how do we get rid of those directories with unusual names? The solution is straightforward. We will enclose the problematic filename or folder name in quotation marks. As an example:
rm ‘Our Sales Data’
rm -rfv ‘/path/to/Dir 1 ;’
rm -r -f -v «baddir#»
rm a\ long \dir1 \name
Sometimes a backslash ( \ ) is required before the meta-character in your filename or folder name:
Part 2. How To Delete Files Recursively in Linux Using rm Command
Delete All Files Recursively
When used with the -r flag, the rm command will remove the contents of all types of files.
But first, using the ls command, let’s look at the home directories. In this example, there are five directories: Desktop, dir2, Documents, Downloads, and removerecurdir.
Assume we want to delete the removecurdir and its contents, including all files and subdirectories. Run the syntax below.
The ls output shows the directory and its contents were successfully removed.
Remove Files by Size Recursively
Here are the steps to recursively delete files (
The find syntax is as follows:
You can use sudo to access the protected files in the example below.
sudo find /var/log -type f -size -10M -exec rm <> +
The minus sign ( — ) must be replaced with a plus sign ( + ).
In the following example, I will use the previous syntax to remove files larger than 1 GB.
find /var/log -type f -size +1G -exec rm <> +
Remove Files by Extension Recursively (File type)
The following section describes how to delete files recursively by extension or file type.
Look for the content of the home directory, which is called testhint with the tree command.
The parent directory testhint has a file (file1.txt) and two subdirectories: testhint2 has file3.txt, and testhint3 has file3 and something.txt.
Assume you want to remove all txt files recursively. The syntax is as follows:
Then, run this command to remove all text files recursively within the parent directory testhint.
find ~/testhint -type f -name ‘*.txt’ -print -delete
All text files were deleted, leaving only file3 without an extension.
You can also delete files by extension by the find together with exec commands.
The syntax for removing files by extension with -exec is as follows:
Run the command below to remove the .log files.
find ~/testhint -type f -name ‘*.log’ -exec rm -f <> \;
The xargs command provides a similar solution. The distinction between xargs and exec is that exec executes the rm function whenever a file matches the condition. The command xargs run the rm command once for each file found that matches the condition.
To remove all files by extension using find and xargs , use the following syntax:
Enter the command shown below to remove all .c files with xargs.
find . -name «*.c» -print0 | xargs -0 rm
Once again, the selected extension files were successfully deleted.
Recursively Deleting All Files Based On Permissions
Let’s take a look at the new content in the testhint directory. Four files have full access (file2, file3.c, file6.c, and file7).
Assume you want to locate and delete all files with full permissions for everyone. The syntax is as follows:
Run the command below to remove all files with full access for all users.
find ~/testhint -perm 777 -print0 | xargs -0 rm
Delete Files Recursively Based on Their Modification Or Creation Time
Execute the syntax that follows:
find ~/testhint -perm 777 -print0 | xargs -0 rm
If you want to delete files created or modified on the last day (last 24 hours), use the following command, where 1 denotes the number of days, and the minus ( — ) symbol denotes files created or modified before the defined number of days.
find -type f -mtime -1 -delete
Replace the minus symbol with a plus symbol to remove files created or modified before a day, before 24 hours.
find -type f -mtime +1 -delete
Part 3. What To Do If You’ve Accidentally Deleted a File or Folder in Linux?
In Linux, unintentionally deleting a file or folder can be a real hassle, but there are actions you can take to improve your chances of successful recovery:
- Stop using the computer: To prevent further data loss, stop using the computer immediately and avoid writing any new data to the disk where the deleted file was located.
- Act quickly: The longer you wait to start the recovery process, the greater the risk of permanent data loss.
- Create backups regularly: Regular backups can ensure that you have a recent copy of your important data in case of accidental deletion.
- Keep your file system in good condition: Regular disk maintenance, such as checking for errors and fixing them, can help keep your file system in good condition and make recovery easier.
The best Linux data recovery tools can also help you recover lost or deleted files. Following these tips and using the correct data recovery tool can increase your chances of successfully recovering your deleted files and folders in Linux.