The find command is used to locate files on a Linux or Unix like operating system. The find command will search directory to match the supplied search criteria. You can search for files by type, name, owner, group, date, permissions and more. By default the find will search all subdirectories for you.
Find command basic syntax
The syntax is:
find where-to-look criteria action
find /dir/to/search -name filetosearch
find /dir/to/search -name "*.c"
find /home/nixcraft/project/ -name "*.py" -print
In this example, find will search the /tmp directory for any files named “data*.txt” and display their pathnames:
find /tmp -iname "data*.txt"
cd /tmp find . -iname "data*.txt" -print
In this above example, I do not have read permission for vmware-root and orbit-Debian-gdm directories. To to avoid this problem try the following syntax:
## redirect error spam to /dev/null ## find where-to-look criteria action 2>/dev/null find . -iname "data*.txt" -print 2>/dev/null
Sample outputs without permission denied spam from find command:
./rtzip/data005.txt ./rtzip/data001.txt ./rtzip/data004.txt ./rtzip/data003.txt ./rtzip/data002.txt ./rtzip/data008.txt ./rtzip/data006.txt ./rtzip/data007.txt ./rtzip/data009.txt
How does it works?
The 2>/dev/null at the end of the find command tells your shell to redirect the error messages (FD #2) to /dev/null, so you don’t have to see them on screen. Use /dev/null to to send any unwanted output from program/command. All data written on a /dev/null special file is discarded by the system. To redirect standard error to /dev/null and store file list to output.txt, type:
## redirect error spam to /dev/null ## find . -iname "data*.txt" -print 2>/dev/null > output.txt cat output.txt
- Man page for bash or ksh shell.