- How to remove files and directories quickly via terminal (bash shell) [closed]
- 4 Answers 4
- How to Remove Files and Directories using the Linux Command Line
- How to Remove Files in Linux
- Interactive: rm -i
- Example Usage
- Force Remove: rm -f
- Recursive Removal: rm -r
- Verbose Removal: -v
- Example Usage
- Remove Multiple Files
- Remove All Files in The Current Directory
- Remove a File from Another Directory
- Remove Hidden vs. Non-hidden Files
- Dotglob Option in Linux
- How to Remove Directories in Linux
- Delete A Non-empty Directory in Linux
- Combine Options
- Conclusion
How to remove files and directories quickly via terminal (bash shell) [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
From a terminal window: When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders. If I have a directory nested with files and folders within folders with files and so on, is there a way to delete all the files and folders without all the strenuous command typing? If it makes a difference, I am using the Mac Bash shell from a terminal, not Microsoft DOS or Linux.
Just in case you wish to restore the files in future , don’t use «rm» for such cases . Use «rm-trash» : github.com/nateshmbhat/rm-trash
4 Answers 4
-r «recursive» -f «force» (suppress confirmation messages)
+1 and glad you added the «Be careful!» part. definitely a «Sawzall» command that can quickly turn a good day into a bad one.. if wielded carelessly.
@itsmatt: You know what they say. give someone a Sawzall, and suddenly every problem looks like hours of fun!
On a Mac? Do this instead: brew install trash then trash -rf some_dir This will move the unwanted directory into your trashbin instead of just vanishing Prestige-style into the ether. (source)
Would remove everything (folders & files) in the current directory.
But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.
Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .
The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.
And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .
Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.
How to Remove Files and Directories using the Linux Command Line
In this tutorial we will go through how to remove files and directories in Linux using rm, unlink , and rmdir .
These commands also work on macOS or other Unix-like operating systems.
Deleting files using a desktop manager is easy and convenient, and you can usually recover them from the trash if you need them back.
However, with the command line, it’s easier to delete multiple files, based on various patterns that you can set. However, when removing files and directories using the command line, they are not moved into the Trash, and will be more difficult to recover, if at all.
Table of Contents
- How to Remove Files in Linux
- Interactive: rm -i
- Force Remove: rm -f
- Recursive Removal: rm -r
- Verbose Removal: -v
- Remove Multiple Files
- Remove All Files in The Current Directory
- Remove a File from Another Directory
- Dotglob Option in Linux
- Delete A Non-empty Directory in Linux
- Combine Options
How to Remove Files in Linux
To remove files in Linux, you can use either the rm or unlink command.
Both commands delete files, but rm is preferred because it has more options and can handle multiple arguments at the same time.
Important Note
Make sure that you’re ready to delete the file because once it’s deleted, it’s very difficult to recover.Example of how to use rm and unlink commands below:
rm filename unlink filename
If the file you’re removing is write-protected, you will be asked to confirm the deletion. To go ahead with removing the file, press y and then enter. If the file is not write-protected, it will delete without confirmation.
We’ll focus on the rm command, since it offers various options, while unlink avoids that.
The rm command provides multiple options to the users including:
- -i (interactive removal): ask users to confirm before removing each file.
- -f (force removal): removes the confirmation part completely, even for the write-protected files.
- -r (recursive removal): uses traversing to recursively delete all files and subdirectories in a parent directory.
- -v (verbose): explains what is being done
Interactive: rm -i
To confirm each time before deleting a file using the rm command, simply append -i to the command. Doing that will reduce the risk of accidentally removing something important when using rm with wildcards.
Example Usage
I have some files in a folder and I think I want to remove most .txt files, but want to keep some.
afile.txt grocerylist.txt somenotes.txt superduperimportantfile.txt temp.txt thatfile.zip thatotherfile.log
To quickly go through each .txt file and decide which to remove I run:
Then I’m prompted each time to type in Y if I really want to remove the file. If I leave blank or type in N, and then press Enter (⏎), then the file isn’t removed:
rm: remove regular empty file 'afile.txt'? Y⏎ rm: remove regular empty file 'grocerylist.txt'? Y⏎ rm: remove regular empty file 'somenotes.txt'? Y⏎ rm: remove regular empty file 'superduperimportantfile.txt'? N⏎ rm: remove regular empty file 'temp.txt'? Y⏎
Force Remove: rm -f
On the contrary, if you want to delete any file (even write-protected) without confirmation, then append -f and filename to the rm command:
Recursive Removal: rm -r
To remove all files and subdirectories in a given directory, switch the directory and use a wildcard character:
Important
The combination of -r and -f, when resulting in rm -rf /path/to_the_Dir/* is one of the more risky commands on Linux, because it is very easy to harm your system. It’s important to be aware of this, as this does happen, and there are even memes about it.Verbose Removal: -v
Mostly, the rm commands delete files silently and don’t show exactly what it’s deleting. To see each file that is being deleted, use the rm command appended with a -v :
Example Usage
I have the files superduperimportantfile.txt, thatfile.zip, and thatotherfile.log, and I’ll run the command:
removed 'superduperimportantfile.txt' removed 'thatfile.zip' removed 'thatotherfile.log'
Remove Multiple Files
To remove multiple files in the same directory:
Remove All Files in The Current Directory
If you want to delete all files in the current directory, you can use an asterisk ( * ) alongside the rm command. The asterisk stands for any character and the question mark represents a single character.
The following command will delete all files in the current directory:
You can also get creative with the commands, and delete a specific file type, multiple files in a row, and so on.
To remove a specific file type (e.g. PNG), type the rm command with the asterisk followed by the file extension:
To remove all files with a single character extension:
Pattern Matching
You can read more about the special characters used in this type of Pattern Matching at gnu.org > Filename Expansion > Pattern MatchingRemove a File from Another Directory
If the file you want to delete is not in the current directory, then you will need to provide the file path before deleting it.
rm /path/to_the_Dir/filename
Remove Hidden vs. Non-hidden Files
In Linux, the names of the hidden files start with a dot. To see hidden files, type in the ls command and pass -a as an argument. You can also -la argument to list files or directories with extra information alongside the hidden files:
To remove hidden files, just use their full filename, including the dot:
rm .file rm /path/to-Dir/.* rm -rf /path/to_Dir/.* rm .*
To remove all files in a directory except hidden files, you’ll run the commands as usual, and it won’t include hidden files. Examples:
rm /path/to-Dir/* rm -rf /path/to_Dir/* rm *
To delete all files in a directory including the hidden files, we can use curly braces, which are a special character (a wildcard) used for commands to perform actions on more than one file at a time, by matching them to a pattern. In the following example we’re
Dotglob Option in Linux
If you enable the dotglob option in Linux, Bash includes all files with a dot in the pathname expansion results. To put it simply, you can enable dotglob to delete hidden files without mentioning the . at the beginning of the filename explicitly, as we have been doing in the previous example.
To turn on the dotglob option:
To remove all files including the hidden files:
To turn off dotglob , simply append -u the command:
How to Remove Directories in Linux
On most Linux filesystems, deleting a directory requires write privileges on the directory and its contents. Otherwise, you will encounter an Operation Not Permitted error.
In Linux, you can remove a directory using the rm -d or rmdir commands. The rmdir command deletes empty directories whereas the rm command can remove directories and recursively delete all its data.
To delete an empty directory in Linux:
rm -d dirname rmdir dirname
Delete A Non-empty Directory in Linux
To remove a non-empty directory, you can use the rm command with the -r (recursive) option:
To remove multiple non-empty directories, simply list the name of each directory followed by a space after rm -r command:
To remove a directory/directories without the confirmation prompt, use the rm command followed by -r (recursive) and -f (force) options:
Just like files (discussed above), you can also use wildcards and regular expressions to delete multiple directories.
Combine Options
As you may have noticed in other commands and some of the above examples, you can combine options for rm .
For example we can run the following command to force remove all .php files in the current directory, but we also want to be careful, so we’ll remove them interactively:
Or we may want to see all files that were removed after we run rm -rf on all files in the current directory:
Conclusion
We hope this article has given you a better understanding of the various ways you can remove files and directories in Linux via rm , unlink and rmdir .
If you have any questions or have encountered any issues feel free to leave a comment or contact us, and we’ll get back to you as soon as we can.