User Tools

Site Tools


find

If you want to send us your comments, please do so. Thanks
More on comments


Find

find searches the current directories and all directories below it

Alternatives

An alternative for find: locate or mlocate
Fuzzy finder fzf might also be useful

find options

OptionExplanationRemark
!Logical NOTExclude a result. Examples: \( ! -name hello.txt \). Also -not but not POSIX compliant
-aLogical ANDDefault, if omitted it is assumed. Also -and but not POSIX compliant
-daystartMeasure times from the beginning of today rather than from 24 hours ago
-execExecute a commandExample: -exec ls -l '{}' \; # '{}' is a result of the find command. \; makes of every execution a new process. + concatenate the commands. The result between ; and + may differ (\ is used to escape the ;)
-group
-oLogical ORAlso -or but not POSIX compliant
-pathDo not decend in this pathExample: find . ! -path /home/user/somedirectory . All files in /home/user and (hidden)subdirectories are found execpt those in /home/user/somedirectory
-permSearch for certain permissionsExample: find / -perm -4000 . Search for files with setuid permission like -rwsr-xr-x 1 root root 51280 jan 10 2019 /bin/mount
-pruneDo not descend into directoryExample: -name “unimportant_dir” -prune
-user

find examples

CommandFunction / Remark
find /tmp -iname "*.txt" 2> /dev/null | lessSearch caseinsensitive in /tmp for files ending with .txt
find . -name “*%*” -or -name “*:*”Search for files with a “%” in it or a “:” in it
find . -iname “*string1*” -a -iname “*string2*” -exec mpg123 '{}' \;Play found mp3 files
find . -type d -empty -deletedelete empty directories in the current directory and below
find . -type d -empty -name "2013*" -deletedelete empty directories with the name starting with 2013 in the current directory and below
find . -type f -exec mv '{}' ../ \;find files in the current directory and below and move them to one level up
find . -type f -size -1000000cfind files smaller than 1MBytes in the current directory and below. Remark: the c after the size is needed. If omitted find conts blocks of 512 or 1024 bytes. See also this page about find
find /home/user/ -type f -cmin -`bc <<< 60*72+1`| wc -lcount the files that have been created in the last 73 hours = 3 days and one hour
alias fnp='find $1 -iname "*$2*" 'A alias to make it easy to do a quick search. This does not work. A test to show what happens: alias echo3='echo -e “$1\n$2” ' . When running this with echo3 H J the result is: empty line, H J. So the inputs are not separated. To be solved
find . -size -10000c -exec grep -l sometext '{}' \;Display the filenames of the files which are less than 10.000 characters and contain the text “sometext
find . -size -10000c -exec sleep 0.2 \; -printf ”%h/%f\r“Search for files less then 10.000 characters. Display the full path and file name for at least 0.2 seconds. To be solved: Clear the line before the next write
find . -name “*.txt” -exec grep -il searchword '{}' \;Search for searchword (case insensitive) in files with extension .txt and display the full path and file name
find / -size -500000c -name “*.sh” -printf ”%h/%f\r“ -exec grep “text here” '{}' \; 2> /dev/nullSearch in files less than 50000 bytes in size. Print path and filename on the same line so you can see when a file is found. Look for “text here” in the found files
find /* -name *.png -exec feh --geometry 400×400 --slideshow-delay=0.2 --cycle-once '{}' \;Search for png picures and display them quickly
find . -type f -exec grep -l texthere '{}' \;
find /etc/ -mtime -10Find all files modified in the last 10 days in the /etc directory
find . -name hallo.txt -printf ”%Ts\n“Search for all files named hallo.txt in the current directory and below and print the file's last modification time in Linux epoch format. Exampele: 1416148114. The %As, %Cs and %Ts options are not documented in the man page
find ~ -name “*something*.extension” -exec cp '{}' /mnt/somewhere/ \;
find . -mtime -1 -printf ”%s\t%Ty%Tm%Td.%TH:%TM\t%h/%f\n“ -exec cp '{}' /home/user/Find the files, show filesize, date and time and copy the found file to an ohter location
find . -mtime -1 -printf '%s \t %Ty%Tm%Td.%TH:%TM \t %h/%f \n' -exec cp '{}' /home/user/Find the files, show filesize, date and time and copy the found file to an ohter location
find ~ -name “*something” -printf ”%s\t%Td-%Tb-%TY\t%p\n“ -exec md5sum '{}' \;Find files with something in the name and print the size and filename and filelocation and the md5sum
find / -type f ! -path ”/tmp/*“ -name filenameSearch for file “filename”. Exclude the files and directory's in the /tmp directory
find / -path /mnt/directory -iname “*.extension” 2> /dev/nullSearch for files with extension “extension”. Exclude the files and directory's in the /mnt/directory directory
find /Music/ -type f -iname “*What you are looking for*” -exec mpg123 '{}' \;Search for music and play the found files with mpg123
find /home/user -name "*.txt" -o -name "*.ods" | \
tar --create --file \
/home/user/textfilesarchive.tar --files-from=-
find /home/user -path /home/user/somedir -prune -or -printPrints all entries in /home/user except for the directory somedir
cd /home/user; find . -path ./somedir -prune -or -printPrints all entries in /home/user except for the directory somedir
cd /home/user; find . -path ./somedir -prune -or -path ./someotherdir -prune -or -name somefileSearches for files called somefile in /home/user except for the directories somedir and someotherdir
cd /home/user; find . -path ./somedir -prune -or -name somename -printPrints all entries in /home/user except for the directory somedir. Remark: -print is needed otherwise the to prune directory is also shown
find ~ -path /home/user/folder1 -prune -or -path /home/user/folder2 -prune -or -iname “*fileordirectory*”Exclude multiple paths
find / -type d -name vendor 2> /dev/nullFinds all directories named 'vendor' which are on the end of the tree

Aliases

These commands are handy to use as an alias

read -p "Case insensitive search starting from the current directory. Enter the search string: " somestring; find . -iname "*$somestring*" 2> /dev/null

Make sure there is a space between the ” and somestring

Warnings

This does not do what is expected. The cause are the quotes. They are not straigth | but / and \ oriented:

find . -size -10000c -printf ”%h/%f\n“  -exec sleep 0.2 \;

find issues

In the folders there are files which have extension “.mp3 (invalid encoding). The result is 0 (zero):

find . -iname "*.mp3\ (inv*" | wc -l

Does not find anything while the files are there:

find . -iname "*(*" | grep encod

Gives more results than expected:

find /etc/ -mtime -10 -exec ls -l '{}' \;

Main subjects on this wiki: Linux, Debian, HTML, Microcontrollers, Privacy

RSS
Disclaimer
Privacy statement
Bugs statement
Cookies
Copyright © : 2014 - 2024 Webevaluation.nl and the authors
Changes reserved.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
find.txt · Last modified: 22-01-2024 13:19 by wim