Useful Linux find examples

The Linux/Unix find command can be used to do a lot more than find strings. Here’s a few very handy commands I’ve put together over the years:

Recursively chmod files and folders to something sensible:
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

Find files over a certain size:
find . -type f -size +100000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

Finds all files named global.php with string $new_include_dirs:
find ./ -name global.php -exec grep -li "\$new_include_dirs" {} \;

Rename all .php files to .php5
find . -name “*.php” -exec mv {} {}5 \;

Rename all .info files to .txt:
find . -name "*.info" -exec rename .info .txt {} \;

Find php files modified in last 7 days:
find ./ -mtime -7 -name "*.php" -ls

Find files containing string in name:
find ./ -name "*RPC*"

Find files owned by user:
find ./ -user username

find 777 directories:
find ./ -type d -perm 0777

Delete files ending in m3u:
find ./ -name *.m3u -exec rm -f {} \;

User xargs to delete large amounts of files:
find ./ -name "*.log" -print | xargs rm -f

Or to delete strings from large amounts of files:
find . -type f -exec grep -l "Email spam string" {} \; | xargs -P3 -n64 rm -f

Find owner of httpdocs and chown all php files to that owner. Not actually the find command but I think it’s relevant!:
for domains in $(cat domlist); do set `stat --printf=%U $domains/httpdocs`;chown -R $1:psacln $domains/httpdocs/*.php; done

You can leave a response, or trackback from your own site.

Leave a Reply