find examples:

find . -type f -exec ls -al {} \;

find . -type f -exec chmod 644 {} \;

find /tmp -type f -user john -exec ls -al {} \; 
find /tmp -type f -user john -exec rm -f {} \; 

find . -type f -name engineering-flowchart.jpg -exec cp /home/luser/source/engineering-flowchart.jpg {} \;

using find to copy

mkdir a mkdir CD3 mount /dev/cdrom a cd a # # [Pp] upper and lower case P, d, f... # find . -name *.[Pp][Dd][Ff] -exec cp {} ../CD3/ \;

using find to update and replace README.html files on a server

--> more update-README.sh #!/bin/bash # script copies a fresh version of README throughout the entire website # update README on server - 10 Dec 2017 # create a list of files into an ARCHIVE in case of error - optional step ######################### find /srv/www/htdocs/ -type f -name README.html -exec ls -al {} \; >> /home/luser/bin/ARCHIVE/current-readmes-10Dec2017.txt ######################### # copy existing README.html master to archive with date - note date format: # --> date +'%Y-%m-%b-%d-%H%M' --> produces: 2017-12-Dec-28-2206 ######################### cp /srv/www/htdocs/README.html /home/luser/bin/ARCHIVE/README.html-`date +'%Y-%m-%b-%d-%H%M'` ####################### # find all README.html in /srv/www/htdocs (default web server - YMMV of course) - copy master over existing find /srv/www/htdocs/ -type f -name README.html -exec cp /home/luser/bin/README.html {} \; #######################
Linux example - find and grep, and perl replacement to fix a spelling error. ------------------------------------------------------- locate the files using FIND find ./ -type f -name '*.htm*' -exec grep Galations {} \; find ./ -type f -name '*.txt' -exec grep Galations {} \;
locate and EDIT the files using find and Perl find ./ -type f -name '*.htm*' -exec perl -pi -e 's/Galations/Galatians/g' {} \; find ./ -type f -name '*.txt' -exec perl -pi -e 's/Galations/Galatians/g' {} \;
find . -atime +3 -type f -exec ll {} \; # find files accessed over 3 days in this directory and list find /tmp -atime +4 -type f -exec rm {} \; # find files accessed over 4 days in the /tmp directory and rm (delete) # always good to LIST first... find . -type f -name "*.pdf" -exec cp /home/john/mopar-pdfs/ {} \;

"find" - Linux commands, scripts, tools and systems administration


find "README.html" in the LinuxMeister directory, then execute a long listing...

--> find LinuxMeister -name README.html -exec ls -al {} \;

output:

-rwxr-xr-x 1 luser luser 7346 Jul 23 14:40 LinuxMeister/RegularExpressions/README.html
-rw-r--r-- 1 luser luser 7346 Jul 23 14:41 LinuxMeister/UNIX/README.html
-rwxr-xr-x 1 luser luser 7346 Jul 23 14:41 LinuxMeister/Scripts/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:11 LinuxMeister/Scripts/Lab-exercise-create-text-man-pages-for-vi-practice/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:11 LinuxMeister/Linux/Intro-to-Linux/.2011/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:11 LinuxMeister/Linux/Intro-to-Linux/README.html
-rwxr-xr-x 1 luser luser 7346 Jul 23 14:40 LinuxMeister/Linux/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:11 LinuxMeister/Linux/FileSystems/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:33 LinuxMeister/Linux/Commands-and-Notes/README.html
-rwxr-xr-x 1 luser luser 5922 May  5 16:33 LinuxMeister/Linux/Datasheets-and-PDFs/README.html
-rw-r--r-- 1 luser luser 7346 Jul 23 14:32 LinuxMeister/README.html
-rwxr-xr-x 1 luser luser 7346 Jul 23 14:41 LinuxMeister/SSH/README.html


find files in /users 100,000 bytes or larger not accessed for 7 days

--> find /tmp -size +100000c -atime +7

output:   /tmp/lintUpdaze/QZdP3t

find files in current directory not accessed for more than 3 days, provide long listing

--> find . -atime +3 -type f -exec ls -al {} \;

output:
-rwxr-xr-x 1 luser luser 1672 Dec  3  2013 ./RPM-info.txt
-rw-r--r-- 1 luser luser 444 Jun 16  2013 ./ssh-setup.txt
-rwxr-xr-x 1 luser luser 1226 Dec  1  2013 ./picasa.txt
-rw-r--r-- 1 luser luser 3187 Apr 24 14:51 ./tree-help.txt
-rw-r--r-- 1 luser luser 593 May  6 12:44 ./grep-awk-uniq.txt
-rwxr-xr-x 1 luser luser 1098 May  4  2013 ./bashrc-user.txt
-rw-r--r-- 1 luser luser 987 Dec  1  2013 ./basic-commands-and-learning-linux.txt
-rwxr-xr-x 1 luser luser 930 May  4  2013 ./bashrc-root.txt
-rwxr-xr-x 1 luser luser 1281 May  4  2013 ./fix-ftp.sh.txt
-rw-r--r-- 1 luser luser 2485 May  6 16:02 ./awk-example.txt
-rw-r--r-- 1 luser luser 3382 Dec  1  2013 ./cut-and-sed-example.txt
-rwxr-xr-x 1 luser luser 3627 May  4  2013 ./vnc-setup.txt
-rwxr-xr-x 1 luser luser 4214 May  4  2013 ./sample-httpd-conf.txt
-rwxr-xr-x 1 luser luser 199 May  4  2013 ./samba.txt

find a particular file in subdirectories then determine file size using du -sh and xargs

first find the "Sent" files in a subdirectory of Thunderbird Mail Folders (trying to clear disk space, do you really care what you sent? if you do, edit the files... --> find . -name Sent -print ./wheezy.com-gmail/Inbox.sbd/Sent ./wheezy.com-gmail/Sent ./freeemail-gmail/Sent ./unixluser/Inbox.sbd/Sent ./unixluser/Sent ./downsouth/Inbox.sbd/Sent ./downsouth/Sent ./favoriteforum/Sent ./insane/Inbox.sbd/Sent ./insane/Sent ./linuxluser/Inbox.sbd/Sent ./linuxluser/Sent NOTE: the really nice thing about Thunderbird is that it is portable. Of course it's harder to move OUT of Windows, so I start with the setup in Linux or Mac, then move the directories over. using rsync --rp directory/ remote:/home/luser/directory/ will synchronize from the source to the remote system, there are other options with rsync such as --delete (but be careful, it removes files on the remote system that are not on the source... if you have any doubts, create a tar file of the remote directory first.) This allows me to move files saved OFF of an IMAP synchronized email account between systems. One does not always have a network connection, or the space on line to save files. I don't like trusting the cloud or free services, both for security and reliabilty. Free services are wonderful, but their low cost operation basically says... you get what you pay for. If facebook fell off the internet tomorrow people would be upset, but one must ask the question... how much did YOU pay for that service? I got way more than I paid for, because I haven't (and won't) pay one cent for that service... well... I'm hooked, hooked up with long lost friends and relatives, so I'd pay something... because it is a value-added service. How much is it worth? I don't pay for premium service on any web services. (except ebay and paypal, but that's built in). So... you can trust gmail and flicker and snap-whatever with your pictures and email... but... there is no contractual obligation, at least not directly. The fact that they've earned money from your use via ads might imply an exchange of value making their service contractual, but you'd have a hard time convincing a judge of that, or a jury. But crazier things have happened in court... but I digress... back to find. :)

find | xargs du -sh

------------------------------------------------ luser@linuxbox [/home/luser/.thunderbird/Thunderbird_Mail_Folders] ------------------------------------------------ --> find . -name Sent -print | xargs du -sh 0 ./wheezy.com-gmail/Inbox.sbd/Sent 2.2M ./wheezy.com-gmail/Sent 6.3M ./freeemail-gmail/Sent 0 ./unixluser/Inbox.sbd/Sent 1.4M ./unixluser/Sent 0 ./downsouth/Inbox.sbd/Sent 60M ./downsouth/Sent 183M ./favoriteforum/Sent 0 ./insane/Inbox.sbd/Sent 644K ./insane/Sent 0 ./linuxluser/Inbox.sbd/Sent 23M ./linuxluser/Sent So... for the next step I want to reduce the size of the largest file, in this case it is "favoriteforum" at 183M using find, xargs and perl I can eliminate "commented" lines, blank lines and other known strings that are redundant... process: --> find . -name Sent -size +100M | xargs du -sh 183M ./favoriteforum/Sent --> find . -name Sent -size +100M | xargs du -s 186612 ./favoriteforum/Sent --> find . -name Sent -size +100M | xargs perl -pi -e 's/#####/##/g' --> find . -name Sent -size +100M | xargs du -s 186608 ./favoriteforum/Sent
found a directory with files at 777... not good. BEFORE: -rwxrwxrwx 1 luser lusers 5279872 Oct 5 01:55 a_file find . -type f -exec chmod 644 {} \; AFTER: -rw-r--r-- 1 lusers lusers 5279872 Oct 5 01:55 a_file

Change permissions on all regular files in a directory subtree to mode 444, and permissions on all directories to 555: find -type f -print | xargs chmod 644 find -type d -print | xargs chmod 755 Note that output from find was piped to xargs(1) instead of using the -exec primary. This is because when a large number of files or directories is to be processed by a single command, the -exec primary spawns a separate process for each file or directory, whereas xargs collects file names or directory names into multiple arguments to a single chmod command, resulting in fewer processes and greater system efficiency. Hewlett-Packard Company - 7 - HP-UX Release 10.20: July 1996
there were files created by a script that had no content... an "ls -al" showed the 0 byte size, but before executing a powerful command with complex syntax, it's always best to test first: find . -type f -size 0 -exec ls {} \; ./MAN/man.edquota.txt ./MAN/man.type.txt ./MAN/man.fg.txt ./MAN/man.yum.txt ./MAN/man.bootloader.txt ./MAN/man.quotaon.txt ./MAN/man.quota.txt ./MAN/man.set.txt ./MAN/man.jobs.txt ./MAN/man.repquota.txt ./MAN/man.export.txt ./MAN/man.rpm.txt ./MAN/man.unset.txt ./MAN/man.yumdownloader.txt ./MAN/man.bg.txt ----------------------------------- the test displayed the empty byte files, now modify the command to remove the files above... ----------------------------------- find . -type f -size 0 -exec rm {} \; and then they were no more...
tricks with find, grep and perl Linux example - find and grep, and perl replacement to fix a spelling error. ------------------------------------------------------- find ./ -type f -name '*.htm*' -exec grep Galations {} \; find ./ -type f -name '*.txt' -exec grep Galations {} \; find ./ -type f -name '*.htm*' -exec perl -pi -e 's/Galations/Galatians/g' {} \; find ./ -type f -name '*.txt' -exec perl -pi -e 's/Galations/Galatians/g' {} \; find . -type f -name README -exec perl -pi -e 's$UNIX/LINUX$LINUX$g' {} \; # find README files and replace UNIX/LINUX with LINUX (path to a link) find . -type f -name "*.html" -exec perl -pi -e 's$pages/Living_By_Faith/Science-and-Bible.html$BIBLE/Science-and-Bible.html$g' {} \; find and perl example find ./ -type f -name '*.htm*' -exec perl -pi -e 's/billygraham.org\/steps.htm/billygraham.org\/spiritualhelp\/steps.asp/g' {} \; find . -type f -name "-fan-yan-hung-kb.html" -exec mv {} fan-yan-hung-kb-3.html \; note: most of these commands are for HP-UX using find to clean /tmp of files older than 1 day find . -atime +1 -name '*' -exec rm -f {} \; find /tmp -atime +1 -name '*' -exec rm -f {} \;
also: find /usr/tmp -atime +1 -name '*' -exec rm -f {} \; remove cores: find / -name core -exec rm -f {} \; using find to compress overladen user directories find . -type f -exec compress -f {} \; (and to undo it should they complain:) find . -type f -exec uncompress {} \; ------------------------------------------------------------------ Fri Jan 17 10:27:04 2003 At 02:58 PM 1/17/2003 +0000, Kevin wrote: I am trying to move files over 700 days old into another folder called oldiges I am using: /usr/bin/find /cadusr/bottle/iges/*.igs -atime +700 -exec /usr/bin/mv -f {} /; /oldiges It wont work any suggestions? ---------------------------- I got this to work (HP-UX 11i on a D350/2): find . -atime +1 -type f - exec mv {} TMP \; so for your application: find /cadusr/bottle/iges/*.igs -atime +700 -exec mv {} /oldiges \; (it looks like you had your slashes wrong... :) john ------------------------------------------------------------------ find examples - man pages HP-UX (need to test in Linux) EXAMPLES Search the two directories /example and /new/example for files containing the string Where are you and print the names of the files: find /example /new/example -exec grep -l 'Where are you' {} \; Remove all files named a.out or *.o that have not been accessed for a week: find / \( -name a.out -o -name '*.o' \) -atime +7 -exec rm {} \; Note that the spaces delimiting the escaped parentheses are required. Print the names of all files on this machine. Avoid walking nfs directories while still printing the nfs mount points: find / -fsonly hfs -print Copy the entire file system to a disk mounted on /Disk, avoiding the recursive copy problem. Both commands are equivalent (note the use of -path instead of -name): cd /; find . ! -path ./Disk -only -print | cpio -pdxm /Disk cd /; find . -path ./Disk -prune -o -print | cpio -pdxm /Disk Copy the root disk to a disk mounted on /Disk, skipping all mounted file systems below /. Note that -xdev does not cause / to be skipped, even though it is a mount point. This is because / is the starting point and -xdev only affects entries below starting points. cd /; find . -xdev -print | cpio -pdm /Disk Change permissions on all regular files in a directory subtree to mode 444, and permissions on all directories to 555: find -type f -print | xargs chmod 444 find -type d -print | xargs chmod 555 Note that output from find was piped to xargs(1) instead of using the -exec primary. This is because when a large number of files or directories is to be processed by a single command, the -exec primary spawns a separate process for each file or directory, whereas xargs collects file names or directory names into multiple arguments to a single chmod command, resulting in fewer processes and greater system efficiency.

examples from the HP-UX man pages

EXAMPLES Search the two directories /example and /new/example for files containing the string Where are you and print the names of the files: find /example /new/example -exec grep -l 'Where are you' {} \;
Remove all files named a.out or *.o that have not been accessed for a week: find / \( -name a.out -o -name '*.o' \) -atime +7 -exec rm {} \; Note that the spaces delimiting the escaped parentheses are required.
Print the names of all files on this machine. Avoid walking nfs directories while still printing the nfs mount points: find / -fsonly hfs -print
Copy the entire file system to a disk mounted on /Disk, avoiding the recursive copy problem. Both commands are equivalent (note the use of -path instead of -name): cd /; find . ! -path ./Disk -only -print | cpio -pdxm /Disk cd /; find . -path ./Disk -prune -o -print | cpio -pdxm /Disk
Copy the root disk to a disk mounted on /Disk, skipping all mounted file systems below /. Note that -xdev does not cause / to be skipped, even though it is a mount point. This is because / is the starting point and -xdev only affects entries below starting points. cd /; find . -xdev -print | cpio -pdm /Disk

remove old core files

find core files with an access time over 4 days, then remove
find . -atime +4 -name core -exec rm -f {} \;

find / -atime +4 -name core -exec rm -f {} \;

note: find commands from / are often frowned upon by system owners
use the . command or specify the path. IN addition, if not run as root
you will run into permission problem errors.
find core files, then remove
find . -name core -exec rm -f {} \;

find / -name core -exec rm -f {} \;

note: find commands from / are often frowned upon by system owners
use the . command or specify the path. IN addition, if not run as root
you will run into permission problem errors.
find old files, then remove
find . -atime +60 -exec rm -f {} \;

find large file, say 100,000 bytes, not accessed for 7 days
find /users -size +100000c -atime +7


real world FIND usage examples

 sudo find /somedirectory -type f -name *.jpg  -exec cp {} . \;

find . -type f -size +10000 -exec ls -al {} \;
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 . -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 {} \;



JohnMeister.com Today's Date:

Simply Linux: Basics Using BASH on Windows 10
Practical Suggestions for Microsoft Windows
Linux Tackles Microsoft  Full Size Jeep Buyer's Guide
Video Course:
The Art of Linux System Administration, and a
Study Guide for the LPIC-2 Certification Exams.

-- O'Reilly Media author info
FULL SIZE JEEP

Buyer's Guide

SJ Jeeps

"Jeep is America's
only real sports car."
-Enzo Ferrari


Mercedes, VW, and other Diesels
Nikon cameras
general tech info
AMSOIL product guide,
or, AMSOIL web, or 1-800-956-5695,
use customer #283461

Amsoil dealer since 1983

purchase AMSOIL and have it
installed locally in WA at:

- Northland Diesel 360.676.1970 - Bellingham
- Midway Auto 360.668.7111 - Clearview/Snohomish
- Fleet Services 425.355.4440 - Everett