If you are learning to use the terminal application or becoming a frequent user of it, you are probably finding out how powerful it is and how many different things you can do with it. One of the things I like to use it for is to clean up my file system by removing unwanted files.
You can delete files and folders on Mac using Terminal with the rm command. It is a simple, straightforward command that you can use to get rid of any items on your computer, but you should always use caution when employing it. Misuse of it can be dangerous to your Mac.
My name is Eric, and as a software engineer, I use command line interfaces such as Terminal daily. Deleting files and folders is often necessary, but it is wise to ensure you know what you are doing before using a command like this one.
If you want to learn more about using the rm command to delete files and folders, keep reading below, and I will show you how to use it. We will also take caution, so you don’t end up accidentally removing things you don’t want to.
Think Before you Delete
Deleting files and folders using the Terminal application on your Mac is very convenient and provides an easy way to eliminate unwanted items. That being said, the rm command that we will use below is very powerful and has the possibility of doing significant damage to your file system.
While it does have some built-in safety precautions, given enough permissions, being in the wrong place and specifying the wrong wildcard can possibly result in permanently deleting important files and even an entire file system.
I have made this mistake before. Once you make a mistake, you learn to always think about and check what you are doing before you use the rm command. If you follow the three simple rules below, it can prevent that terrible sinking feeling of permanently deleting the wrong files.
Know who you are
This may sound corny, but in this instance, I am talking about your login id. You should always know who you have logged in as. Does the user you are logged in as have administrative or root privileges? If so, the account may be able to delete anything on the file system.
In most cases with Mac Terminal, you need to use the sudo command with your password to delete items owned by root or other users, but you still need to be careful. Once you have entered the password, you don’t always need to enter it again for the commands that come after the first one.
You can check to see who you are logged in as by using the id command. Just type the command as shown below.
id
Your sudo privileges will determine what admin or root commands you can run. You can see what sudo privileges you have by running the following command.
sudo -l
You may have to enter your password after this command. If you see rm or all in the list of commands, it means you have the power to delete anything on the file system, so you need to be careful.
The example above shows that I can run all commands (which includes rm), so using sudo and my password (sudo rm), I can remove any files or folders on the system.
Know where you are
When running commands like rm it is very important to know where you are currently located in the file system. When working on the command line, we often move around to different directories, and it is easy to forget where you are at.
Running an rm command from the root directory (/) can easily remove things we don’t want to remove and possibly delete the entire file system. This can also be a problem if you run it from the top level of your home directory.
The important thing here is to ensure you are in the proper directory where the files or folders that you want to delete are located. You can check this with the pwd command. Simply type it as follows.
pwd
After running the pwd command, I can see that I am in my home directory.
Know what you are deleting
Of course, you will need to know what you are deleting. Make sure that the files and or folders you are deleting are not critical system files that your macOS or one of your applications needs to run.
When in doubt, stop what you are doing and research to find out what it is you are getting ready to remove. It never hurts to do a quick internet search with the file name or information to see if you can find out what it is.
If you are unsure, you can also make a backup copy of that file or folder and leave it in a temporary location until it has been removed for some time and you know that it does not cause any problems with your system.
Be extra careful when using wildcards to specify what you are going to delete. Doing an rm * command will delete everything under your current directory. It is always best to test your wildcard specification before using it with the rm command.
Use it with a command such as ls first to see what the results are. This command only displays information about the files and cannot do any damage to them. With this, you will see what files the wildcard specification actually includes.
For example, if I were getting ready to delete everything in a folder that starts with the name test, I might use the wildcard test*. To see what the wildcard test* will include, I can run the following command.
ls -la test*
The results show all of the files and folders included by test*, so when you use this wildcard with the rm command, it will delete all of these files and folders. Now I can see beforehand what the command is going to do.
If you are careful and use the rm command with the three checks that I have shown above, then you should have no problems, and you will begin to be very comfortable with using it when needed.
How to use the rm Command to Delete Files and Folders
After going over the safety precautions I have outlined above, you probably figured out that the rm command is what you will use to delete files and folders. It’s a relatively simple command with a few optional parameters.
Below I will show you how to use the rm command for both files and folders.
Files
You will first need to navigate to the location of the file or folders that you wish to delete. Once there, you can delete them using the rm command. In its simplest form, you can type it as shown below.
rm <file name>
For my example below, I have removed a file called test.txt.
The file gets removed without any messages or issues. If the file happens to have read-only permissions, then you will be asked to override the permissions. Answering yes will remove the file.
If you want to skip the confirmation step shown above, you can add the -f parameter, which tells it to force the removal of the file without asking you to confirm.
rm -f test.txt
This will remove the file without any confirmation required,
Suppose another user or the root user owns the file. In that case, it will not let you delete it unless it has full read/write permissions. You will need to use the sudo command, which will allow you to run commands (in this case, the rm command) as the root user.
The command would look like the following.
sudo rm test.txt.
Once you enter this command, you will be prompted to enter your password the first time you do this. Use the same password you use to log in to your Mac.
If you are logged in as a user with administrative permission, this is a very powerful command as it will let you delete almost any file on the system, so be careful when using it.
You can specify more than one file to delete at a time, as shown below.
rm file1 file2 file3 …
You can also specify wildcards to delete multiple files. If I wanted to delete all files that start with the word test, I would enter the command below.
rm test*
And if I specify the -r parameter (r stands for recurse), it will also remove any files in any of the subdirectories.
rm -r test*
This will remove all files that start with test in my current directory and all subdirectories below my current directory.
If you know the path of a file, you can remove it without navigating to the path first. Simply add the file’s path to the beginning of the file name in the rm command as shown below.
rm /Users/ericwinkler/test.txt
This will remove the file from wherever I am located. Just be very careful if you combine paths and wildcards, as it can have unwanted results if you type something wrong.
There are, of course, many combinations and other parameters you can specify when using the command, and you can explore those when you get a chance.
Folders
Removing folders or directories works much the same way as removing files, but there is one caveat. You will need to use the -r parameter so that anything within the folder is also deleted. Even if you don’t see files in the folder, there is information stored there that you can’t see.
Use the command below to remove a folder or directory.
rm -rf <folder name>
Here I have also specified the -f parameter. Without this, you may need to confirm each file, and if the folder contains hundreds of files, it could take a while.
It is also possible to remove directories using the -d parameter, but this only works if the folder is empty.
rm -d <directory name>
I usually just use the rm -rf command instead since it works with directories, whether they are empty or not.
FAQ
Below are a few of the questions commonly asked when discussing the rm command and how to delete files and folders on Mac using Terminal.
Do the files go into the trash?
No, they do not. When you delete files and folders from the command line using the rm command, they are permanently deleted; they do not get moved to the trash, so the only way you can recover them is from a backup such as Time Machine.
How can I see the available parameters for the rm command?
If you are curious to see all the available parameters and options that are available for the rm command, you can type the following command in Terminal.
man rm
This will display all the information on the command right in your Terminal window.
Why do I have to be so cautious with the rm command?
The main reason is that you can do a lot of damage with the rm command. Using a combination of sudo, rm, -rf, and *, you can delete anything and everything on your system, which can be a real headache.
Conclusion
As we have seen above, you can use the rm command with different parameters and options to delete files and folders using Mac Terminal. This is a convenient way to clean up your file system, but you need to be extremely careful and ensure you know what you are doing.
If you are just learning, it may be wise to create a temporary directory where you can practice running rm commands to get a good feel of how it works. You will soon learn how to safely use the command and be on your way to being an expert user of the Terminal application.
As usual, let me know how your experiences go with using the rm command to delete files and folders. I would love to hear from you!