Programming

Delete DSSTORE files in current folder and all subfolders from command line on Mac duplicate

20 September 2026 · 9 min read

Delete DSSTORE files in current folder and all subfolders from command line on Mac duplicate

If you’re a Mac user, you’ve likely encountered the mysterious .DS_Store files. These files, short for “Desktop Services Store,” are automatically created by macOS in every folder you open in Finder. They store information about folder views, icon positions, and other metadata, enhancing the user experience. While they’re generally harmless, these files can become problematic when sharing folders, especially with users on different operating systems or when deploying code to web servers. They can inadvertently expose information about your file structure or cause unexpected behavior. This article will guide you through the process to delete .DS_Store files in the current folder and all subfolders from the command line on Mac, ensuring a cleaner, more professional workflow. We’ll explore the most efficient methods using Terminal, providing step-by-step instructions and best practices for managing these hidden files.

Understanding .DS_Store Files and Why You Should Delete Them

.DS_Store files are a proprietary feature of macOS, designed to remember how you like to view folders. This includes icon size, window position, and even custom background settings. For individual users, these files are generally beneficial. However, when collaborating with others or deploying projects, these files can become a nuisance. Sharing a folder containing .DS_Store files with Windows users, for instance, can result in these files appearing as visible, albeit useless, files in their file explorer. This can be confusing and unprofessional, especially in client-facing scenarios. Furthermore, in software development, these files can sometimes interfere with version control systems like Git, leading to unnecessary commits and potential conflicts. Deleting them ensures a cleaner repository and avoids accidentally deploying them to a live server, where they could expose directory structures.

Moreover, .DS_Store files can sometimes cause problems with network file sharing. If multiple users are accessing a shared folder and modifying its view settings, conflicting .DS_Store files can lead to inconsistent behavior. This can manifest as icons not appearing correctly, folders reverting to default view settings, or even file sharing errors. Therefore, regularly cleaning these files from shared folders can help maintain a consistent and reliable experience for all users. Regularly deleting .DS_Store files from the current folder and all subfolders from the command line helps maintain a clean and professional working environment. This ensures compatibility across platforms and prevents potential conflicts in collaborative projects, aligning with best practices for file management.

According to a study by Atlassian, unnecessary files like .DS_Store can contribute to a significant increase in repository size over time, impacting performance and storage costs. Removing these files can lead to smaller, more efficient repositories, improving build times and reducing bandwidth usage. Atlassian emphasizes the importance of regularly cleaning repositories to maintain optimal performance.

Using the find Command to Delete .DS_Store Files

The most efficient way to delete .DS_Store files in the current folder and all subfolders from the command line on Mac is by using the find command in Terminal. This powerful command allows you to search for files based on various criteria, including name, type, and modification date. When combined with the -delete option, it can automatically remove the found files. This method is particularly useful for cleaning up large directories with numerous subfolders.

To use the find command, open Terminal and navigate to the directory you want to clean. Then, enter the following command:

find . -name ".DS_Store" -type f -delete

Let’s break down this command: find . tells the command to start searching in the current directory (represented by the dot .). -name “.DS_Store” specifies that you’re looking for files named “.DS_Store”. -type f ensures that you’re only targeting files, not directories. Finally, -delete instructs the command to delete the found files. This command is a quick and efficient way to remove all .DS_Store files from the current directory and all its subdirectories. Before running this command, it is always a good idea to test it with -print instead of -delete to make sure it finds the correct files.

This method is considered the standard approach for removing these files due to its simplicity and effectiveness. It’s also scriptable, meaning you can incorporate it into automated cleanup routines. Remember to exercise caution when using the -delete option, as deleted files are not typically recoverable. Consider backing up your data before running this command, especially if you’re working with sensitive files.

Alternative Methods and Considerations

While the find command is the most common method, there are alternative approaches you can use to delete .DS_Store files in the current folder and all subfolders from the command line on Mac. One such method involves using a combination of find and xargs. This can be particularly useful when dealing with a very large number of files, as xargs can help avoid command line length limitations.

Here’s the command using find and xargs:

find . -name ".DS_Store" -type f -print0 | xargs -0 rm -f

In this command, find . -name “.DS_Store” -type f -print0 is similar to the previous command, but instead of deleting the files directly, it prints their names separated by null characters (-print0). This is important because filenames can contain spaces or special characters, which can cause problems with the rm command. xargs -0 rm -f then takes the output of the find command and passes it to the rm -f command, which removes the files. The -0 option tells xargs to expect null-separated filenames, ensuring that spaces and special characters are handled correctly. The rm -f command forces the removal of the files without prompting for confirmation.

Another consideration is the use of third-party tools. Several utilities are available that can automate the process of cleaning up .DS_Store files. These tools often provide a graphical interface, making them more user-friendly for those who are not comfortable with the command line. However, it’s important to choose reputable tools from trusted sources to avoid installing malware or compromising your system’s security. Always research and read reviews before installing any third-party software. Always back up your data before using any new tool. Click here for more information.

Preventing .DS_Store Files from Being Created

While deleting .DS_Store files is a useful skill, preventing them from being created in the first place can save you time and effort. macOS provides a setting that disables the creation of .DS_Store files on network volumes. This setting does not affect local volumes, but it can be helpful if you frequently share files over a network.

To disable the creation of .DS_Store files on network volumes, open Terminal and enter the following command:

defaults write com.apple.desktopservices DSDontWriteNetworkStores true

This command modifies the user defaults for the com.apple.desktopservices domain, setting the DSDontWriteNetworkStores key to true. This tells macOS not to create .DS_Store files on network volumes. To re-enable the creation of .DS_Store files on network volumes, use the following command:

defaults write com.apple.desktopservices DSDontWriteNetworkStores false

Another approach is to add .DS_Store to your global .gitignore file. This prevents these files from being tracked by Git in any of your repositories. This is especially useful for software developers who want to avoid accidentally committing these files to their projects. Here’s how to add .DS_Store to your global .gitignore file:

  1. Open Terminal.
  2. Navigate to your home directory: cd ~.
  3. Create a global .gitignore file if it doesn’t exist: touch .gitignore_global.
  4. Edit the file using a text editor like Nano or Vim: nano .gitignore_global.
  5. Add .DS_Store to the file.
  6. Save the file and close the editor.
  7. Configure Git to use this global ignore file: git config –global core.excludesfile ~/.gitignore_global.

By implementing these preventative measures, you can minimize the occurrence of .DS_Store files and maintain a cleaner file system. These practices are particularly important for collaborative projects and deployments, ensuring consistency and preventing potential issues.

Infographic here
FAQ About Deleting .DS\_Store Files -----------------------------------
What are .DS\_Store files?
.DS\_Store files are hidden files created by macOS to store information about folder views and settings.
Why should I delete them?
They can cause issues when sharing folders with users on other operating systems or when deploying code to web servers.
How do I delete them from the command line?
Use the command: `find . -name ".DS_Store" -type f -delete` in Terminal.
Can I prevent them from being created?
Yes, you can disable their creation on network volumes using the command: `defaults write com.apple.desktopservices DSDontWriteNetworkStores true`.
Are there any risks involved in deleting them?
The risks are minimal, but it's always a good practice to back up your data before running any command that deletes files. Once deleted, they cannot be recovered.
Summary of Key Points ---------------------

Deleting .DS_Store files is a routine maintenance task that contributes to a cleaner, more professional workflow. By understanding what these files are and the potential problems they can cause, you can make informed decisions about how to manage them. The command line provides powerful tools for efficiently removing these files, while preventative measures can help minimize their occurrence. Remember to always exercise caution when using the -delete option and consider backing up your data before running any potentially destructive commands. Embracing these practices ensures compatibility across platforms and prevents potential conflicts in collaborative projects.

  • Use the find command for efficient removal.

  • Consider preventative measures to reduce .DS_Store file creation.

  • Always back up your data before running commands that delete files.

  • .DS_Store files can expose directory structures.

  • Regularly clean shared folders for a consistent experience.

Now that you’re equipped with the knowledge and tools to delete .DS_Store files in the current folder and all subfolders from the command line on Mac, take a moment to clean up your directories and ensure a smoother workflow. Implement the preventative measures discussed to minimize future occurrences. If you found this guide helpful, share it with your colleagues and friends to help them maintain a cleaner, more efficient working environment. For more tips on macOS maintenance and command-line productivity, explore other articles on our blog. You can also find helpful information on Apple’s support website. Apple Support offers a wealth of resources for troubleshooting and optimizing your macOS experience. Additionally, the SS64 website provides a comprehensive reference for macOS command-line tools.

Question & Answer :

I understand I can use `find . -name ".DS_Store"` to [find all the .DS\_Store files](https://stackoverflow.com/questions/5905054/how-can-i-recursively-find-all-files-in-current-and-subfolders-based-on-wildcard) in the current folder and all subfolders. But how could I delete them from command line simultaneously? I found it's really annoying to switch back and forth to all folders and delete it one by one.

find can do that. Just add -delete:

find . -name ".DS_Store" -delete 

Extend it even further to also print their relative paths

find . -name ".DS_Store" -print -delete 

For extra caution, you can exclude directories and filter only for files

find . -name ".DS_Store" -type f -delete