find command linux with examples and options

| April 30, 2011

Linux find command with bunch of options and examples

Find a file or directory

# find . -name TEMP –print (or)
# find . -name TEMP -exec echo {} \;

Find core files in this directory tree and remove them

# find . -name “core” -exec rm -f {} \;

Find junk directories and remove their contents recursively

# find . -name “junk” -exec rm -rf {} \;

Find files not starting with “junk” OR not ending with “log”

# find . ! \( -name “junk*” -o -name “*log” \) -print

Find a pattern in a file using the recursive grep (ignore case)

# find . -type f | xargs grep -i MYPATTERN
# find . -name ‘*.sh’ -exec grep -i MYPATTERN {} \;

Find files modified in the past 7 days

# find . -mtime -7 -type f

Find files owned by a particular user

# find . -user esofthub

Find all your writable directories and list them

# find . -perm -0777 -type d –ls (or)
# find . -type d -perm 777 -print

Find all your writable files and list them

# find . -perm -0777 -type f -ls
or
#find . -type f -perm 777 -print

Find large file sizes and list them or output to file

# find . -type f -size +1000 –ls (or)
# find . -type f -size +1000 -print
# find . -type f -size +2000K | tee filename

Find how many directories are in a path (counts current directory)
# find . -type d -exec basename {} \; | wc -l
53

Find how many files are in a path

# find . -type f -exec basename {} \; | wc -l
120
Find all my pipe files and change their permissions to all writable

# find . -name “pipe*” -exec chmod 666 {} \;

Find files that were modified 7 days ago and archive

# find . -type f -mtime 7 | xargs tar -cvf `date ‘+%d%m%Y’_archive.tar`

Find files that were modified more than 7 days ago and archive

# find . -type f -mtime +7 | xargs tar -cvf `date ‘+%d%m%Y’_archive.tar`

Find files that were modified less than 7 days ago and archive

# find . -type f -mtime -7 | xargs tar -cvf `date ‘+%d%m%Y’_archive.tar`

Find files that were modified more than 7 days ago but less than 14 days ago and archive

# find . -type f -mtime +7 -mtime -14 | xargs tar -cvf `date ‘+%d%m%Y’_archive.tar`

Find files in two different directories having the “test” string and list them

# find esofthub esoft -name “*test*” -type f -ls

Find files in two different directories having the “test” string and list them

# find esofthub esoft -name “*test*” -type f -ls

Find files in two different directories having the “test” string and count them

# find esofthub esoft -name “*test*” -type f -ls | wc -l
12

Find files and directories newer than CompareFile

# find . -newer CompareFile -print

Find files and directories older than CompareFile

# find . ! -newer CompareFile -print

Find files and directories but don’t traverse a particular directory

# find . -name RAID -prune -o -print

Find all the files in the current directory

# find * -type f -print -o -type d -prune

Find files associated with an inode

# find . -inum 968746 –print

# find . -inum 968746 -exec ls -l {} \;

Find an inode and remove

# find . -inum 968746 -exec rm -i {} \;

This command will find all the .gz files under /home/jomlu and move to /home/bobbin

# find /home/jomlu -iname “*.gz” -exec mv {} /home/bobbin \;

#find all files under /dir older than 7 days, and delete

find /dir -type f -mtime +7 | xargs rm -f

#find all files newer than FILE, and delete

find /dir -type f -newer /path/to/FILE -exec rm \{\} \;

#find all files older than 10 minutes, and print names(print is default)

find /dir -type f -mmin +10 -print

#find all files newer than 1 day, and tar them to file.tar

find /dir -type f -mtime -1 | tar -c -T – -f file.tar

#delete the files older than 60 days

find /…/exp -name \*.log -mtime +60 -exec rm -f {} \;

#Remove all files (log files) created 7 days before current date

find / -name *.log -ctime +7 -exec /bin/rm -f {} \;

Tags: ,

Category: Linux Administration

Comments are closed.