find / -type f -name *.jpg -exec cp {} . \;
find /dir -type f -size 0 -print
find . -type f -size +10000 -exec ls -al {} \;
find /var/nmon -mtime +30 | xargs -i rm {}
find /var/nmon -mtime +1 -exec gzip -9 {} \;
find . -atime +1 -type f -exec mv {} TMP \; # mv files older then 1 day to dir TMP
find . -name “-F” -exec rm {} \; # a script error created a file called -F
find . -exec grep -i “vds admin” {} \;
find . \! -name “*.Z” -exec compress -f {} \;
find . -type f \! -name “*.Z” \! -name “.comment” -print | tee -a /tmp/list
find . -name *.ini
find . -exec chmod 775 {} \;
find . -user xuser1 -exec chown -R user2 {} \;
find . -user psoft -exec rm -rf {} \;
find . -name ebtcom*
find . -name mkbook
find . -exec grep PW0 {} \;
find . -exec grep -i “pw0” {} \;
find . -atime +6
find . -atime +6 -exec ll | more
find . -atime +6 -exec ll | more \;
find . -atime +6 -exec ll \;
find . -atime +6 -exec ls \;
find . -atime +30 -exec ls \;
find . -atime +30 -exec ls \; | wc -l
find . -name auth*
find . -exec grep -i plotme10 {};
find . -exec grep -i plotme10 {} \;
find . -ls -exec grep ‘PLOT_FORMAT 22’ {} \;
find . -print -exec grep ‘PLOT_FORMAT 22’ {} \;
find . -print -exec grep ‘PLOT_FORMAT’ {} \;
find . -print -exec grep ‘PLOT_FORMAT’ {} \;
find ./machbook -exec chown 184 {} \;
find . \! -name ‘*.Z’ -exec compress {} \;
find . \! -name “*.Z” -exec compress -f {} \;
find /raid/03c/ecn -xdev -type f -print
find /raid/03c/ecn -xdev -path -type f -print
find / -name .ssh* -print | tee -a ssh-stuff
find . -name “*font*”
find . -name hpmcad*
find . -name *fnt*
find . -name hp_mcad* -print
find . -grep Pld {} \;
find . -exec grep Pld {} \;
find . -exec grep Pld {} \;
find . -exec grep PENWIDTH {} \; | more
find . -name config.pro
find . -name config.pro
find /raid -type d “.local_sd_customize” -print
find /raid -type d -name “.local_sd_customize” -print
find /raid -type d -name “.local_sd_customize” -ok cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \;
find /raid -type d -name “.local_sd_customize” -exec cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \;
find . -name xeroxrelease
find . -exec grep xeroxrelease {} \;
find . -name xeroxrelease
find . -name xeroxrelease* -print 2>/dev/null
find . -name “*release*” 2>/dev/null
find / -name “*xerox*” 2>/dev/null
find . -exec grep -i xeroxrelease {} \;
find . -print -exec grep -i xeroxrelease {} \;
find . -print -exec grep -i xeroxrelease {} \; > xeroxrel.lis
find . -exec grep -i xeroxrel {} \;
find . -print -exec grep -i xeroxrel {} \;
find . -print -exec grep -i xeroxrel {} \; | more
find /raid/03c/inwork -xdev -type f -print >> /raid/04d/user_scripts/prt_list.tmp
find . -exec grep ‘31.53’ {} \;
find . -ls -exec grep “31/.53” {} \; > this.lis
find . -print -exec grep “31/.53” {} \; > this.lis
find . -print -exec grep 31.53 {} \; > this.lis
find . -exec grep -i pen {} /;
find . -exec grep -i pen {} \;
find . -print -exec grep -i pen {} \; | more
find . -exec grep -i pen {} \;
find . -atime +6 -exec ll | more \;
find . -atime +6 -exec ll \;
find . -atime +6 -exec ls \;
find . -atime +30 -exec ls \;
find . -atime +30 -exec ls \; | wc -l
find . \! -name ‘*.Z’ -exec compress -f {} \;
find . -name ‘cache*’ -depth -exec rm {} \;
find . -name ‘cache*’ -depth -print | tee -a /tmp/cachefiles
find . -name ‘cache[0-9][0-9]*’ -depth -print | tee -a /tmp/cachefiles
find . -name ‘hp_catfile’ ‘hp_catlock’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_catfile’ -name ‘hp_catlock’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_cat*’ -depth -print | tee -a /tmp/hp.cats
find . -name ‘hp_cat[fl]*’ -depth -print | tee -a /tmp/hp.cats
find /raid -name ‘hp_cat[fl]*’ -depth -print
find . \! -name ‘*.Z’ -exec compress -f {} \;
find . -name ‘*’ -exec compress -f {} \;
find . -xdev -name “wshp1*” -print
find . -xdev -name “wagoneer*” -print
find . -name “xcmd” -depth -print
find /usr/contrib/src -name “xcmd” -depth -print
find /raid -type d -name “.local_sd_customize” -exec ls {} \;
find /raid -type d -name “.local_sd_customize” \
-exec cp /raid/04d/MCAD-apps/I_Custom/SD_custom/site_sd_customize/user_filer_project_dirs {} \;
find . -name “rc.conf” -print
find . -name “rc.conf ” -exec chmod o+r ‘{}’ \; -print
find . -not (\ -name “*.v” -o -name “*,v” \) ‘{}’ \; -print
=================================================================
Basic find command examples
This first Linux find example searches through the root filesystem (“/”) for the file named “Chapter1”. If it finds the file, it prints the location to the screen.
find / -name Chapter1 -type f -print
On Linux systems and modern Unix system you no longer need the -print option at the end of the find command, so you can issue it like this:
find / -name Chapter1 -type f
The “-f” option here tells the find command to return only files. If you don’t use it, the find command will returns files, directories, and other things like named pipes and device files that match the name pattern you specify. If you don’t care about that, just leave the “-type f” option off your command.
This next find command searches through only the /usr and /home directories for any file named “Chapter1.txt”:
find /usr /home -name Chapter1.txt -type f
To search in the current directory — and all subdirectories — just use the . character to reference the current directory in your find commands, like this:
find . -name Chapter1 -type f
This next example searches through the /usr directory for all files that begin with the letters Chapter, followed by anything else. The filename can end with any other combination of characters. It will match filenames such as Chapter, Chapter1,Chapter1.bad, Chapter-in-life, etc.:
find /usr -name “Chapter*” -type f
This next command searches through the /usr/local directory for files that end with the extension .html. These file locations are then printed to the screen.
find /usr/local -name “*.html” -type f
Find directories with the Unix find command
Every option you just saw for finding files can also be used on directories. Just replace the -f option with a -d option. For instance, to find all directories named build under the current directory, use this command:
find . -type d -name build
Find files that don’t match a pattern
To find all files that don’t match a filename pattern, use the “-not” argument of the find command, like this:
find . -type f -not -name “*.html”
That generates a list of all files beneath the current directory whose filename DOES NOT end in “.html”, so it matches files like *.txt, *.jpg, and so on.
Finding files that contain text (find + grep)
You can combine the Linux find and grep commands to powerfully search for text strings in many files.
This next command shows how to find all files beneath the current directory that end with the extension .java, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:
find . -type f -name “*.java” -exec grep -l StringBuffer {} \;
(Those last few characters are required any time you want to exec a command on the files that are found. I find it helpful to think of them as a placeholder for each file that is found.)
This next example is similar, but here I use the -i argument to the grep command, telling it to ignore the case of the characters string, so it will find files that contain string, String, STRING, etc.:
find . -type f -name “*.java” -exec grep -il string {} \;
Acting on files you find (find + exec)
This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r–r–).
find /usr/local -name “*.html” -type f -exec chmod 644 {} \;
This find command searches through the htdocs and cgi-bin directories for files that end with the extension .cgi. When these files are found, their permission is changed to mode 755 (rwxr-xr-x). This example shows that the find command can easily search through multiple sub-directories (htdocs, cgi-bin) at one time.
find htdocs cgi-bin -name “*.cgi” -type f -exec chmod 755 {} \;
Running the ls command on files you find
From time to time I run the find command with the ls command so I can get detailed information about files the find command locates. To get started, this find command will find all the “*.pl” files (Perl files) beneath the current directory:
find . -name “*.pl”
In my current directory, the output of this command looks like this:
./news/newsbot/old/3filter.pl
./news/newsbot/tokenParser.pl
./news/robonews/makeListOfNewsURLs.pl
That’s nice, but what if I want to see the last modification time of these files, or their filesize? No problem, I just add the “ls -ld” command to my find command, like this:
find . -name “*.pl” -exec ls -ld {} \;
This results in this very different output:
-rwxrwxr-x 1 root root 2907 Jun 15 2002 ./news/newsbot/old/3filter.pl
-rwxrwxr-x 1 root root 336 Jun 17 2002 ./news/newsbot/tokenParser.pl
-rwxr-xr-x 1 root root 2371 Jun 17 2002 ./news/robonews/makeListOfNewsURLs.pl
The “-l” flag of the ls command tells ls to give me a “long listing” of each file, while the -d flag is extremely useful in this case; it tells ls to give me the same output for a directory. Normally if you use the ls command on a directory, ls will list the contents of the directory, but if you use the -d option, you’ll get one line of information, as shown above.
Find and delete
Be very careful with these next two commands. If you type them in wrong, or make the wrong assumptions about what you’re searching for, you can delete a lot of files very fast. Make sure you have backups and all that, you have been warned.
Here’s how to find all files beneath the current directory that begin with the letters ‘Foo’ and delete them.
find . -type f -name “Foo*” -exec rm {} \;
This one is even more dangerous. It finds all directories named CVS, and deletes them and their contents. Just like the previous command, be very careful with this command, it is dangerous(!), and not recommended for newbies, or if you don’t have a backup.
find . -type d -name CVS -exec rm -r {} \;
Find files with different file extensions
The syntax to find multiple filename extensions with one command looks like this:
find . -type f \( -name “*.c” -o -name “*.sh” \)
Just keep adding more “-o” (or) options for each filename extension. Here’s a link to
Case-insensitive file searching
To perform a case-insensitive search with the Unix/Linux find command, use the -iname option instead of -name. So, to search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters beneath the current directory, use this command:
find . -iname foo
If you’re just interested in directories, search like this:
find . -iname foo -type d
And if you’re just looking for files, search like this:
find . -iname foo -type f
Find files by modification time
To find all files and directories that have been modified in the last seven days, use this find command:
find . -mtime -7
To limit the output to just files, add the “-type f” option as shown earlier:
find . -mtime -7 -type f
and to show just directories:
find . -mtime -7 -type d
Recent Comments