X

Terminal fun: Deleting repetitive files in OS X

If you find a number of similarly named files that you would like to delete from multiple folders, the Find command in the Terminal can be used to do the trick.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
4 min read

To manage various settings, both OS X and Windows will often place small files within folders that help the system manage their contents. A common file of this type in OS X is the .DS_Store file, and a common type in Windows is the Thumbs.db file. While these files are usually hidden, in some cases (especially those where multiple operating systems share the same storage spaces), you may see these files show up and contribute to window clutter.

If you want to remove them and tidy up your filesystem, there are several utilities that can do this for you, but another option is to use the OS X Terminal's Find command, which with a simple setup can be used scour the entire system for a specific file name pattern, and then optionally delete the file.

To use this command, open the Terminal utility (in the Applications/Utilities/ folder) and then perform the following steps:

  1. Type "sudo find" followed by a single space.
  2. Drag your starting folder to the Terminal Window (or use a forward slash to indicate the system root for the whole system).
  3. Type "-name" followed by a space and then the file name pattern you would like to search for in quotes.
  4. To have the command delete files, finish off the command with the "-delete" flag.

When you are done, the command should look something like the following:

sudo find / -name ".DS_Store" -delete

Terminal showing results of the Find command.
Running the command in the Terminal without the "-delete" flag will list all found files, so you can scroll through them to see if the desired ones are being targeted before rerunning the command with the "-delete" flag included (click for larger view). Screenshot by Topher Kessler/CNET

In this case, the command will start in the system root and locate all files named ".DS_Store," and then delete them if found. The "sudo" component will run it in root mode, so it will be able to access all aspects of the system without running into any errors.

While this command is convenient, as with any automatic deletion command (especially those that use "sudo" to run in root mode) be sure you have properly specified your file names to delete. In this case the ".DS_Store" name specification is unique and is the complete name of the files being targeted, but in other cases the file names might be more ambiguous and need further specification.

For example, Apple included a lockfile option in OS X Lion's preferences writing routines, so users may see a number of .lockfile files associated with the preference files in their home folders. If you want to remove these files, simply specifying "lockfile" as the name in the command will not work because these files contain more than only "lockfile" in their names. Instead, you would need to use an asterisk "wild card" character to specify the rest of these files' names, in a manner similar to the following:

sudo find / -name "*.lockfile" -delete

In addition to making sure to specify the name properly, do keep in mind that the Terminal is case-sensitive, so if you were to use ".ds_store" or "*.Lockfile" in the name specifications then the command would not properly locate these file names.

Some ways to target repeated files with wild cards include the following:

  1. The filename contains an appended identifier string, such as what commonly happens with preference files in OS X:

    com.apple.iTunes.plist.ABE3AC
    com.apple.iTunes.plist.2D4E65
    com.apple.iTunes.plist.A2E3D4


    In this case, the use of the following wild card will target these files:

    com.apple.iTunes.plist.*

  2. The files all have the same file extension:

    com.apple.iTunes.plist.lockfile
    com.apple.iPhoto.plist.lockfile
    com.apple.TextEdit.plist.lockfile


    In this case, the following wild card will target these files:

    *.lockfile

  3. The filename itself is unique:

    .DS_Store
    Thumbs.db


    In these cases, there is no need for a wild card. You can use a wild card such as "*.DS_Store" but there's no need because .DS_Store files do not contain any additional components to their names.

Lastly, you can further isolate groups of the files you are trying to delete by specifying subdirectories that contain them. In the command examples above, the single slash tells them to search the entire filesystem; however, this can cause the command to take a while and perhaps include files you do not wish to include. Therefore, you can tell it to only search specific folders by specifying them instead of using the single slash.

In the case of the ".lockfile" files, they are only generated in the preferences folders on the system, and in the case of the .DS_Store files, you might only want to delete them from shared folders that Windows or Linux systems might connect to, instead of remove them from your entire system. To specify these folders, instead of typing a single slash to specify the entire filesystem to search, in step 2 above simply drag the desired folder to the Terminal window when typing the command.

As a final note, the "-delete" component of the command will tell it to remove the files it finds; however, you can also have the command show you a preview of these files first by running it without the "-delete" flag. When this happens the system will output file paths to the located files, which you can browse through and check to make sure the proper files are being targeted, before running the command again with "-delete" at the end of it.



Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.