Centos: How To List All / Delete Hidden Files Recursively ?

by admin on August 29, 2014

I need to find and list all hidden files including directories on a Linux server (CentOS). How can I recursively list all hidden files and directories? How do I save result in a text file? And How do I delete all hidden files/directories ?

Using the find command to list all hidden files recursively

# find  -name ".*" -print

Example

# find /etc/ -name ".*" -print
/etc/skel/.bashrc
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/.pwd.lock

Or,

# find  -name ".*" -ls

Example

# find /etc/ -name ".*" -ls
1310797    4 -rw-r--r--   1 root     root          124 May 11  2012 /etc/skel/.bashrc
1310794    4 -rw-r--r--   1 root     root           18 May 11  2012 /etc/skel/.bash_logout
1310796    4 -rw-r--r--   1 root     root          176 May 11  2012 /etc/skel/.bash_profile
1310854    0 -rw-------   1 root     root            0 Dec 12  2012 /etc/.pwd.lock

If you only want to search only hidden files:

# find  -type f -iname ".*" -ls

Example

# find /usr -type f -iname ".*" -ls
8915163    4 -rw-r--r--   1 root     root          760 Sep 19  2012 /usr/share/man/man5/.k5login.5.gz
8651596    4 -rw-r--r--   1 root     root           40 May 11  2012 /usr/share/man/man1/..1.gz
8654188    4 -rw-r--r--   1 root     root         2438 Aug 25 14:29 /usr/local/lib/php/.depdb
8654187    0 -rw-r--r--   1 root     root            0 Aug 25 14:29 /usr/local/lib/php/.depdblock
8654186    0 -rw-r--r--   1 root     root            0 Aug 25 14:29 /usr/local/lib/php/.lock
...

Or, If you only want to search only hidden directories:

find  -type d -iname ".*" -ls

Example

# find /usr -type d -iname ".*" -ls
8654180    4 drwxr-xr-x   3 root     root         4096 Dec 12  2012 /usr/local/lib/php/.channels
8785283    4 drwxr-xr-x   2 root     root         4096 Dec 12  2012 /usr/local/lib/php/.channels/.alias
8654179    4 drwxr-xr-x   5 root     root         4096 Dec 12  2012 /usr/local/lib/php/.registry
...

To remove all hidden files recursively

# find  -type f -iname ".*" -print0 | xargs -0 rm -f

Example

# Delete all hidden files of apache server
# find /var/www/html -type f -iname ".*" -print0 | xargs -0 rm -f

Related Posts:

Leave a Comment

Previous post:

Next post: