//Tips tagged find
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
To delete a file who's file name is a pain to define (eg. ^H^H^H) find it's inode number with the command "ls -il". Use the line below to find and delete the file.
find . -inum 12345 | xargs rm

View Comments »

To find out the number of files of each type in your current directory try the following:

find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr  

5 PHP script text 2 data 2 Zip archive data 2 GIF image data 1 PNG image data

(You may want to add this as an alias rather than type it in each time!)

View Comments »

To find the last modified files in a directory you can use ls -ltr. To find the last modified file on a file system it will not work, but the following command will work:

find /etc -type f -printf "%T@ %T+ %p" | sort -n

View Comments »

To remove empty directories (even if filenames or dirnames contain spaces or weird characters) from a tree you can do:

find . -type d -empty -print0 | xargs -0 rmdir

View Comments »

Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
find . -name  -exec grep "phrase" {} \;

To display the filename that contained a match, use -print:
find . -name  -exec grep "phrase" {} \; -print

Or, use Grep options to print the filename and line number for each match:
find . -name  -exec grep -Hn "phrase" {} \;

The string `{}` is replaced by the current filename being processed everywhere it occurs in the arguments to the command. See the `find` man page for more information.

View Comments »

The following alias will print the directory structure from the current directory in tree format.

alias dirf='find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"'

View Comments »

Selective content replace on files. For example to replace '<?' with '<?php' in all PHP files:
find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;

View Comments »

dos2unix requires the name of an input and output file so it can be hard to run on a list of files. The following gets around this and will run dos2unix on all files in a directory:

for f in `find * -type f`; do echo "dos2unix $f $f"; done | sh

View Comments »

When specifying time with find options such as -mmin (minutes) or -mtime (24 hour periods, starting from now), you can specify a number n to mean exactly n, -n to mean less than n, and +n to mean more than n. 2 For example:

find . -mtime -1  # find files modified within the past 24 hours
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modifed between 5 and 10 minutes ago

View Comments »

Mass-renaming files using find and sed:

find -name "*.php3" | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh

(this example will rename all .php3 files to .php)

View Comments »

The following command creates in the /usr/project directory, a copy of the current working directory structure:

find . -type d -print|sed 's@^\.\{0,1\}@/usr/project@' | sed 's/ /\\ /' | xargs mkdir -p

View Comments »

This is a script that I recently wrote to find the logs of my IDP that were over 90 days old and delete them.

The script is pretty much self explanatory.

## Lets find the log directories that are over 90 Days old.
find /var/netscreen/DevSvr/logs -mtime +90 -type d -prune -print > /root/scripts/files-deleted

## Now let's log the directories we are going to delete.
touch /root/scripts/deletelog

## We want to know when the files were deleted so we will append that to the log file.
date >> /root/scripts/deletelog

## Now we go through and delete the directories that are over 90 days old
## and log the directories that we delete to the log file.
for FILE in `cat /root/scripts/files-deleted`
echo Deleting $FILE ... >> /root/scripts/deletelog
rm -Rf $FILE

## Finally we will email a copy of the log file to the admin for his/(or her) review.
cat /root/scripts/deletelog | mail -s "90 Day IDP log cleanup" youremailaddress@domian.com

View Comments »

Use find by time to delete files more than x days old. For example the command below will delete files more than one day old:

find . -mtime +1 -exec rm {} \;

View Comments »

Use this command to find files of a given mime-type. For example to find all PNG images in a directory or below:

find . -exec file -i {} \; | grep image/png

View Comments »

which directories and trees take up all the diskspace?
du -sm $(find /start/dir/* -type d -maxdepth 1 -xdev) | sort -g

If you want more human readable output try:
du -ha /var | sort -n -r | head -n 10

you want to see ALL directories in the tree
find $1 -type d | xargs du -sm | sort -g

To show all directories size including sub directories, type

du -h

To calculate the current directory size you are in (-s stand for summary)

du -sh

To show all the 1 level sub directories size (which you are not interested at sub sub directories.)

du -sh *

To show the size of specific directory

du -sh /home

To show the size of all sub directories of a specific directory

du -sh /home/*

View Comments »

If you want to select specifically the files to add to an archive you can pipe the output from find (or any command that gives a list of files) to cpio:

$ find ./dir/ | cpio -o --format=tar > archive.tar
$ find ./dir/ | cpio -o --format=tar -F test.tar

View Comments »

Rename a lot of files at once:
find . | perl -ne'chomp; next unless -e; $oldname = $_; s/aaa/bbb/; next if -e; rename $oldname, $_'

Change 'aaa' and 'bbb' to what you want to find and replace in the filename

View Comments »

Do a sha256sum of an entire directory name directory and check for integrity.
Modifying the IFS variable is necessary for filename with space.

$ IFS='
$ for i in $(find directory -type f -print);do sha256sum "$i";done > directory.SHA256SUM
$ sha256sum -c directory.SHA256SUM

View Comments »

alias findrandom="find -type f -exec bash -c 'test \`echo \$((RANDOM%2))\` -eq 0' \; -print"

will give you a randomized find. Each file in all subdirs will be displayed with equal probability of 0.5.

View Comments »

find . -name *whatyouwant* -exec perl -pi.bak -e 's/toto/foo/g' {} \;

Replace toto by foo in all file found by find.
It make a backup $file.bak

View Comments »

find . -name "*windows.template" | while read TEMPLATE ; do echo -e "\nText to Append=" >> $TEMPLATE ; done

View Comments »

Use this command to list files that have been updated today in the current directory.

ls -l|awk '/'$(date +%Y-%m-%d)'/{print $NF}'

This method is an alternative to using find with the mtime option (see tip 199) and can be a more intuitive way of locating files modified on a specific date, for example:

ls -l|awk '/2009-04-28/{print $NF}'

'ls --full-time' can also be used and the matching criteria modified to find files modified in a particular month, hour, day, between 09:00 and 17:00 each day or anything else as required.

View Comments »

Assign random names to all files in a folder (including subfolders!):

find . -type f -exec bash -c 'mv "$1" "./$RANDOM"' - {} \;

Note that this is a somewhat expensive operation, so it might take a few seconds for large numbers of files.

View Comments »

Use this to find core files and remove them:

find . | egrep "\/core\.[0-9]+$" | xargs rm -f

This works well as it finds only core files.

View Comments »

.DS_Store files are a hidden file created by OS X to store custom attributes of a folder such as the position of icons or the choice of a background image. By default, they are created in every folder accessed, even folders on remote systems (for example, folders shared over an SMB or AFP connection). This can soon leave your shared folders littered with these files.

The command below will remove all DS_Store files from the current directory and any sub directories.

find -type f -name .DS_Store -exec rm {} \;

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit