Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Deleting Hidden Files

Submitted by on August 7, 2018 – 4:14 pm

This simplest of problems just never fails to find a new victim. For whatever reason you want to delete all hidden files in the current directory and subfolders. Without much thinking you just type rm -rf .* Here’s a classic example of one developer giving another developer a terrile advise.

The problem here, of course, is that .* wildcard expands to include .., which is the name-inode map for the parent directory. Thus, if your run rm -rf .* as root from any subfolder, you will wipe out your entire server, including the data on any network mounted shares.

Whenever deleting anything using wildcards, try to limit your destructive abilities as much as possible. For example, the command below will delete hidden files in the current directory without descenting into any subfolders:

find "$(pwd)/" -maxdepth 1 -mindepth 1 -type f -name "\.*" -delete

The following command will delete hidden folders at the current directory level:

find "$(pwd)/" -maxdepth 1 -mindepth 1 -type d -name "\.*" -exec /bin/rm -rf {} \;

You can control the depth of search by adjusting the maxdepth parameter.

Print Friendly, PDF & Email

Leave a Reply