- How do I force delete a directory in Linux?
- How to force delete a directory in Linux
- Removing a directory that contains other files or directories
- How do I remove a full directory in Linux with verbose output?
- Delete a non-empty directory in Linux terminal
- Master the macOS command line: How to delete files and folders using Terminal
- Why bother deleting files with the command line?
- How to delete files
- How to delete empty directories (a.k.a folders)
- How to delete everything in a directory
- Can’t empty Trash in the Finder? Use the Terminal
- Remove directory in Terminal & other ways to delete files and folders on Mac
- Before we start
- How to remove directory with Terminal
- Why delete file command line feature is important
- Tip: Delete unneeded system files on Mac
- How to use delete file command line feature
- How to make command line delete directory
- Delete large and old folders using CleanMyMac X
- Other ways to delete files and folders on Mac
How do I force delete a directory in Linux?
How to force delete a directory in Linux
Here is how to forcefully delete a folder in Linux:
- Open the terminal application on Linux.
- The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
- Type the command rm -rf dirname to delete a directory forcefully.
- Verify it with the help of ls command on Linux.
Removing a directory that contains other files or directories
The syntax is as following for deleting a directory:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/
Use rm command to delete a non-empty directory in Linux terminal
How do I remove a full directory in Linux with verbose output?
Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/
Where,
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -r : Recursive delete
- -f : Force delete a directory
- -v : Verbose output
Delete a non-empty directory in Linux terminal
In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘ — ‘, for example ‘-backups’, use one of these commands:
rm -rfv — -backups/
OR
rm -rfv ./-bacups/
Источник
Master the macOS command line: How to delete files and folders using Terminal
When it comes to quickly taking care of daily tasks, the command line can be both powerful and dangerous. Take the commands in this article as an example: the rm command allows you to remove (or delete) files. The rmdir command does the same to directories (also know as folders). But be careful: Unlike when you move files to the Trash from the Finder, there’s no way to get them back if you use these commands.
Still, if you want to tap into Terminal’s powers, this is a command you can’t overlook. I’ll show you how to add a safeguard to ensure that you only delete files you really want to delete.
Why bother deleting files with the command line?
Deleting files with the Finder isn’t too difficult, plus you can always fish files out of the Trash if you change your mind. So why bother using the command line? Here are some reasons:
- You can use wildcards to delete multiple files quickly and efficiently.
- You can remove files from the Trash when you encounter stubborn errors.
- You can delete files that are hidden in the Finder. These files, which can contain settings for certain apps or parts of macOS, contain a dot (.) before their names and the Finder doesn’t show them.
- If you’ve lost access to the Finder because your Mac is on the blink, you might be able to use the command line to troubleshoot the problem.
How to delete files
It’s dangerously easy to delete files with the rm command. Here’s an example. After you launch Terminal (in your /Applications/Utilities folder) type cd
/Desktop to navigate to the Desktop directory. To delete a file, type rm filename , replacing filename with the actual name of the file you want to delete. (If you have a file name with spaces, you need to put the name in quotes: «For Example.txt» .) If you had a file here named MyFile.rtf that you never, ever wanted to see again, you could run this command:
rm MyFile.rtf
When you press Return, the file will go poof! The Mac doesn’t confirm if you want to delete the file. It will be gone, toast, history. You can’t get it back.
You can even delete multiple files in a single command. If you have three files on your Desktop that you want to delete, and you want to delete them all at once, you can do so like this (if you have a file name with spaces, you need to put the name in quotes: «For Example.txt» .):
rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
Again, pressing the Return key does the dirty work.
It’s worth repeating: this command deletes files. It nukes them. You can’t get them back. You can’t click on the Trash icon and retrieve files you’ve accidentally deleted.
But there is a safety net: it’s the -i (interactive) flag. So if you’re feeling cautious, you could run the above commands with this flag as follows:
Or, in the case of deleting multiple files:
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In each case, pressing Return won’t actually activate the rm command, because the -i flag acts as a pause button. You’ll see the following in Terminal when running these commands:
In order to proceed, you need to type yes , or simply y . In the case of multiple files, you’ll see one query for each file. Granted, it’s easy to get into the habit of quickly typing y , but the question is intended to make you stop and think very carefully about whether you really want to delete that file.
How to delete empty directories (a.k.a folders)
Deleting directories, or folders, is a bit different. If you try to run the rm command on a directory, you’ll see the following message:
You can’t delete a directory using the rm command.
There’s a special command for deleting directories: rmdir . So to delete a directory named Archives, run this command (If you have a directory name with spaces, you need to put the name in quotes: «For Example» .):
rmdir Archives
You can’t use the -i flag with the rmdir command, so the command is a bit riskier.
Note that this command only deletes empty directories. If you want to delete a directory and the files it contains, read on.
How to delete everything in a directory
The rm command has a powerful option, -R (or -r ), otherwise known as the recursive option. When you run the rm -R command on a folder, you’re telling Terminal to delete that folder, any files it contains, any sub-folders it contains, and any files or folders in those sub-folders, all the way down. You enter the command as m -R directoryname , where you substitute directoryname for the name of the directory you want to delete. (If you have a directory name with spaces, you need to put the name in quotes: «For Example» .)
For example, let’s say you have a directory full of archives, containing sub-directories and files. Deleting each item individually from the Finder or the command line can take a long time. So just run the command like this:
rm -R Archives
Remember, this deletion is final. But you can use the -i flag for protection:
rm -iR Archives
This will ask you to confirm the deletion of each item. This can be annoying, but unless you’re really sure you want to delete all those files, it’s probably best to be safe.
Can’t empty Trash in the Finder? Use the Terminal
When can the rm -R command come in handy? Say you can’t empty the Trash on your Mac. A file might be locked or you may not have permission to delete one or more files. This sort of glitch is annoying, but you can use the command line to provide an easy solution.
In Terminal, type the following:
rm -R
Then type a space.
In the Finder, open the Trash, and then drag the items it contains to the Terminal window. You’ll see one or more files with paths such as /Users/.Trash/file.txt.
If there are lots of files, you may find that the resulting list—all on one long line, wrapping in the Terminal window—may be very long. If you’re absolutely sure that you want to delete all these items, press Return. Terminal will empty the Trash. Command line win!
Источник
Remove directory in Terminal & other ways to delete files and folders on Mac
Before we start
Having spent some years coding applications for macOS we’ve created a tool that everybody can use. The all-round problem fixer for Mac.
So here’s a tip for you: Download CleanMyMac to quickly solve some of the issues mentioned in this article. But to help you do it all by yourself, we’ve gathered our best ideas and solutions below.
Features described in this article refer to the MacPaw site version of CleanMyMac X.
One of the most basic computer functions — deleting files and folders — is also one of the most essential. If you never get rid of anything, soon enough all those extra gigabytes will take a toll on your Mac’s processing power, RAM, and hard drive, not to mention your digital life will resemble a dreadful episode of Hoarders.
So deleting files is good and healthy. But how do you do that? Most people right-click on what they need gone and choose Move to Trash from the menu or use the File option in the menu bar. Others employ the ⌘ + Delete shortcut, which works across the system (even within dialog windows). Experts, however, often find themselves defaulting to the command line delete directory feature. Let’s see how and why you should learn it too.
How to remove directory with Terminal
Since macOS is a Unix-based system, nearly all system tasks you do every day with the help of its graphic interface can also be performed via the command line tool called Terminal.
Why delete file command line feature is important
With regards to delete file or delete folder command line functionality, Terminal lets you:
- Effortlessly erase one or multiple files, folders, and apps, bypassing any error messages you can get when you go the traditional route.
- Remove files from Trash, including ones you can’t delete by simply emptying the trash.
- Get rid of files that are invisible to you within Finder (usually system or root files, for example, .htaccess).
- Delete files and folders in cases when Finder is unresponsive.
Note: The Mac command line delete file feature is final and irreversible. While it lets you avoid any error messages, it also removes the files completely, without any possibility of retrieving them later on.
Tip: Delete unneeded system files on Mac
Some files are hard to delete in a normal way because they sit deeply in system folders. These are such files as logs, broken downloads, caches, remains of old apps, etc. Still, deleting them could give your Mac an extra boost — that’s because they are outdated and clutter your macOS.
To dig these files out, try a free version of CleanMyMac X. Among other things, this app has an impressive System Junk detector. When you download CleanMyMac X (this free version is notarized by Apple), you need to click on System Junk.
Just take a look at the sheer amount of files it unearthed. To delete them, click on Clean.
How to use delete file command line feature
Removing files from your Mac forever using Terminal is deceptively simple: just use the rm command followed by the name of the file. Here’s how it works in practice:
- Launch Terminal from your Utilities folder in Applications.
- Check which directory you’re in by typing ls -la
- Then navigate down a directory with cd [directoryname] or up a directory with cd ../
- When you’re in the same directory as the file you want to delete, type rm [filename.extension]
- If you want to delete multiple files at once, list them all, but make sure there’s a space between each one.
- Press Enter to execute the command.
Now all the files you specified after rm are gone for good. Navigate directories in your Mac and repeat the process as many times as you want.
Surprised there was no confirmation before your files were deleted? Luckily there’s a way to add one as a safeguard for not deleting the wrong file by accident. Just use -i after the rm but before the first filename, like this: rm -i [filename.extension]. Terminal will then ask you whether you’re sure you want to delete the file. Reply y or yes followed by Enter and the file will be gone. This also works for multiple files, but you’ll have to confirm the removal of each one separately.
How to make command line delete directory
Surprisingly, you can’t delete a folder using the rm command because it has its own: rmdir.
Otherwise, rmdir works exactly the same as rm:
- Navigate to the appropriate directory using Terminal.
- Type rmdir [directoryname]
- Hit Enter.
Sadly, you can’t use the -i hack when you’re deleting folders, so be extra careful!
Another thing to keep in mind is that rmdir only deletes the directory, but can’t delete any files or folders located within that directory.
To delete a folder with everything in it, you need to use rm -r followed by the folder’s name. Using -i to create a warning here is possible and done like this: rm -ir [foldername].
When it’s too difficult to find a folder or file via Terminal, but you can detect them in Finder, simply drag and drop the file onto the Terminal window to generate its path.
Delete large and old folders using CleanMyMac X
As it happens with folders, they are easy to mess up and lose track of. That’s why many apps provide an alternative interface for searching and deleting files which is more flexible than the Finder. For example, using CleanMyMac X (the app we’ve mentioned above) you can find massive folders that might have been evading you. You also can filter them by file type and other criteria.
The latest released version of this tool looks like this:
This app by developer MacPaw is available for a free download here. Using this app, files are removed for good, and as far as we know, it’s irreversible.
Other ways to delete files and folders on Mac
The shortcut combination
We are sure you know how to drag your files to the Trash. But here is an alternative way that deletes files at once, bypassing the Trash. There’s one less step involved (you don’t have to empty the bin). And you cannot reverse this action.
Select a file, and press [Option + ⌘ + Delete]
Delete hidden folders on Mac
Some folders on your Mac are made invisible to protect them from accidental deletion. They are system and support files you would ignore most of the time. But what if a folder seems empty but still takes up space? You can apply another shortcut combination to highlight these unseen objects. Then, you can delete them at your own risk.
To highlight hidden folders: [Press Shift + ⌘ + . dot key]
This best works if you open some system folder, like Library
Again, delete these folders only if you are sure what they are.
That was how to remove files and folders on your Mac, shown in multiple ways. Want to know more? Explore our blog for a whole universe of Mac tips!
Источник