+5 votes
83 views
in Solutions by (242k points)
reopened
Master the Command Line: Delete Files and Folders

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

Why delete files with the command line?
Delete files
Delete directories (aka folders)
Delete everything
A practical application
Delete junk files with one click

When it comes to quickly sorting out daily tasks, the command line can be really powerful and even dangerous. Let's take today's commands as an example: the rm command lets you move (or delete) files. The rmdir command does the same with directories (also known as folders)..

But be careful: Unlike moving files to Trash from Finder, if you use these commands there will be no way to get them back. However, if you want to take advantage of the Terminal, this command should not be overlooked. We will show you how to add a backup to ensure that you delete only the files you want.

image

Why delete files with the command line?

It's not too difficult to delete files with Finder, and you can always delete them from the Trash. So why use the command line? Here are a few reasons:

  • You can delete multiple files quickly and efficiently by using metacharacters;
  • You can move files from the recycle bin when you encounter persistent errors;
  • You can delete hidden files in Finder: these files which may contain settings for certain applications or parts of OS X contain a period (.) In front of their names and Finder does not display them;
  • If you can't access Finder because your Mac is down, you can use the command line to resolve the issue.

Delete files

It is easy to delete files with the rm command. Here is an example: having launched the Terminal (in / Applications / Utilities), type cd ~ / Desktop to access the Desktop directory. If you have a file here named MyFile.rtf that you never want to see again, you can run this command:

rm MyFile.rtf

When you press Return, the file will disappear and you will not be able to recover it..

You can also delete multiple files with a single command. So if you have three files to delete on your Desktop, and you want to delete them all at once, you can do it by typing:

rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf

Again, pressing the Return key will nullify this operation.

If I find it bad enough to discuss the power of the rm command. As I already said a little above, this command deletes the files permanently. You will not be able to recover them. And you can't click trash can icon to recover "accidentally" deleted files..

But there is a safety net: it's an -i sign, or interactive. So if you want to be a little more careful, you can run the above commands with this sign as follows:

rm -i MyFile.rtf

rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf

In any case, pressing Return will not activate the rm command, because the –i sign serves as a pause button. When you run these commands, you will see in Terminal the contents as follows:

image

If you use the –i sign, you will see a confirmation message for each deleted file.

To continue, you must enter yes, or just y. In the case of multiple files, you will find a survey of each file. Granted, it's easy to get into the habit of typing in y, but the purpose of the question is to allow you to take more time to seriously think about whether you really want to delete the file or not.

Delete directories (aka folders)

This is a little different from deleting directories or folders. If you try to use the rm command on a directory, you will see the message as follows:

“You cannot delete a directory using the rm command. "

This is a special command to remove directories: rmdir. So, to delete a directory called "Archives", please run this command:

rmdir archives

You cannot use the –i sign with the rmdir command, so the command is a bit riskier.

Note that this command only removes empty directories. If you want to delete a directory and its included files, read on.

Delete everything

The rm command has a powerful option, -R (or –r), also called the recursive option. When you run the rm -R command on a folder, you tell Terminal to delete the folder and its included files, any subfolders it contains, and any files or folders in those subfolders, up to end.

For example, if you have a directory full of archives, which contains subdirectories and files. It will take a long time to individually remove each item from Finder or from the command line. So run the command like this:

rm -R Archives

Remember that this deletion is final. However, you can use the –i sign to protect it:

rm -R Archives

By this action, you will have to confirm the deletion of each item, which can be a bit annoying, but unless you are really sure you want to delete all of those files, it is probably best to be careful.

A practical application

When can the rm -R command be useful? If you can't empty the Trash on your Mac. A file may be locked or you do not have permission to delete it. This type of problem is annoying, but you can use the command line to resolve the problem.

In Terminal, enter the following command:

Rm –R

Then type a space.

In the Finder, open the trash, then drag the contained items to the Terminal. You will see one or more files with paths such as /Users/.Trash/file.txt.

If there are a lot of files, you will find that the results list (on one line, in the Terminal window) can be very long. If you are absolutely sure you want to delete all of these items, press the Return key. The Terminal will then empty the recycle bin.

Delete junk files with one click

Even though the command line is very convenient, it is also difficult for a few to practice it. With them, third-party software is easier. Here I offer you a powerful tool which can help you remove junk files from your Mac safely and quickly.

1. First of all, please download the FonePaw MacMaster software to your Mac by pressing the download button below. Then start the program.

Free try

2. Choose the “Macster Scan” option on the left of the window, then start the scan.

3. Please wait for a moment and you will see all the unnecessary system files displayed on the screen. Click on “System Cache” and you can choose which ones you want to delete, then click on the “Clean” button.

image

If you have any questions about this article, please leave your comments in the space below!


...