//Tips tagged find
will give you a randomized find. Each file in all subdirs will be displayed with equal probability of 0.5.
Replace toto by foo in all file found by find.
It make a backup $file.bak
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
To find out the number of files of each type in your current directory try the following:
(You may want to add this as an alias rather than type it in each time!)
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!)
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
Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
To display the filename that contained a match, use -print:
Or, use Grep options to print the filename and line number for each match:
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.
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.
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/"'
Selective content replace on files. For example to replace '<?' with '<?php' in all PHP files:
find . -name '*.php' -exec sed -ie 's#<?#<?php#' {} \;Mass-renaming files using find and sed:
(this example will rename all .php3 files to .php)
find -name "*.php3" | sed 's/\(.*\).php3$/mv "&" "\1.php"/' | sh
(this example will rename all .php3 files to .php)
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
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.
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` do echo Deleting $FILE ... >> /root/scripts/deletelog rm -Rf $FILE done ## 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
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 -pTo 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
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
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/pngIf 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 or $ find ./dir/ | cpio -o --format=tar -F test.tar
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 {} \;
find . -mtime +1 -exec rm {} \;
Do a sha256sum of an entire directory name directory and check for integrity.
Modifying the IFS variable is necessary for filename with space.
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
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/*
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/*
Rename a lot of files at once:
Change 'aaa' and 'bbb' to what you want to find and replace in the filename
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
Use this command to list files that have been updated today in the current directory.
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 --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.
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.
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.
find . -name "*windows.template" | while read TEMPLATE ; do echo -e "\nText to Append=" >> $TEMPLATE ; done
Use this to find core files and remove them:
This works well as it finds only core files.
find . | egrep "\/core\.[0-9]+$" | xargs rm -f
This works well as it finds only core files.
.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.
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 {} \;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
Assign random names to all files in a folder (including subfolders!):
Note that this is a somewhat expensive operation, so it might take a few seconds for large numbers of files.
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.

