Written by
Yvonne FengSummary: This article from iBoysoft guides you on how to delete a file in Terminal, how to delete a directory in Terminal, and how to delete multiple files at once in Terminal.
I need to delete some files through Terminal because my MacBook is full and can't start it. I know I need to start with rm /Volumes/..../Users/.. - Reddit
Although Finder offers an intuitive way to delete files, Terminal is essential in some cases. It allows for permanent file deletion and enables file management and troubleshooting when Finder is inaccessible, such as in Mac issues.
This guide covers deleting single files, multiple files, and directories in Terminal, along with troubleshooting the rm command. Terminal deletions are permanent and cannot be undone like moving files to the Trash, so follow each step carefully.
How to delete a file in Terminal?
To avoid the failure of deleting the file at the end, read the following methods carefully if you are not familiar with Terminal or the command.
- Open Terminal by pressing Command + Space, type Terminal, and hit Enter.
- Use the following command to move to the folder where your file is stored. This command moves you into the Documents folder inside your home directory.cd ~/Documents
- To delete a file, you can use the rm (remove) command followed by the file name: rm filename.txt If the file name contains spaces, enclose it in quotes: rm "my file.txt"
If you encounter a permission denied error, try the following command.sudo rm filename.txt
Note: Files deleted using Terminal cannot be recovered, unlike Finder, where they first go to the Trash. Additionally, please check the file name before deleting it to avoid accidentally removing important files.
Share the method of deleting a file in Terminal with your friends if you like it.
How to delete multiple files at once in the Terminal?
If you want to delete multiple files at once, Terminal is a great choice. Instead of manually selecting each file, you can use commands to delete multiple files at once, saving time.
Method 1. Use Spaces to Separate File Names
- Open Terminal.
- Navigate to the directory where the files are stored using the cd command. For example:cd ~/Documents/testYou can use the ls command to list all files in the directory and confirm the filenames.ls
- Suppose you want to delete file1.txt, file2.txt, and file3.txt. You can enter the following command and press Enter to permanently delete them.rm file1.txt file2.txt file3.txt
Method 2. Use the find Command to Delete Multiple Files
The rm command can only delete files in the current directory, whereas the find command allows you to search and delete matching files across multiple subdirectories.
To delete all .log files in the current directory and its subdirectories, run:
find . -name "*.log" -type f -delete
If you want to delete .log files that were modified more than 7 days ago, run:
find . -name "*.log" -type f -mtime +7 -delete
Here, -mtime +7 means the file was last modified more than 7 days ago.
How to delete a directory in Terminal?
Terminal is useful when you need to remove hidden directories or when you want to quickly delete multiple directories. Since the Terminal does not move deleted directories to the Trash, caution is required. Below are the detailed steps for safely deleting a directory.
- Open Terminal.
- Use the cd command to navigate to the path containing the directory you want to delete. For example. cd ~/Documents
- Delete an empty directory (assuming the directory name is test_folder): rmdir test_folder
If the directory contains files or subdirectories, rmdir will not work. In this case, you need to use the rm -r command. The following two commands will recursively delete all files and subdirectories inside the directory:
cd ~/Documents rm -r test_folder
Why “rm” command is not working?
The rm command deletes files and directories, but it may not always work as expected. If the file is protected or owned by another user, you may encounter a "Permission denied" error. In this case, you can use the sudo rm filename to grant administrator privileges and execute the deletion.
Terminal will return a "No such file or directory" error if the filename is misspelled or the file has already been deleted. To avoid this issue, you can use the ls command to check if the file exists and verify the correct path before running rm.
Additionally, attempting to delete a directory without using the -r option will result in an "is a directory" error. To delete a directory, you need to use rm -r directory_name for recursive deletion.
However, proceed with caution, as this command permanently deletes the directory and all its contents, making recovery impossible.
Share this article with your friends if you find it useful!