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
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | find command+ Unix like os |
Time | N/A |
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 /path/to/dir -name "pattern" -print find /tmp -iname "data*.txt"
OR
cd /tmp find . -iname "data*.txt" -print
Sample outputs:
Fig. 01: Find will show an error message for each directory on which you don’t have read permission.
How to fix find command permission denied messages
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 message 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
Exclude all “permission denied” messages from “find” command on Linux
There is one problem with the following command. It would filter out all error messages created by find command, not just the permission denied ones:
find / -name foo 2>/dev/null find / -type d -name bar 2>/dev/null
To avoid that try the following find command along with grep command on Linux or Unix-like systems:
find / -name foo 2>&1 | grep -v "Permission denied" find / -type d -name bar 2>&1 | grep -v "Permission denied"
In short you should use following syntax to skip “permission denied” errors messages when running find in Linux or Unix-based systems:
find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied" find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied"
To store output to a file run:
find /path/to/dir -name "search-patter" 2>&1 | grep -v "Permission denied" > output-file find /etc -name "x*.conf" 2>&1 | grep -v "Permission denied" > output.txt
Display output.txt using cat command:
cat output.txt
In the above example, we used find command along with grep command to filter out permission denied error messages.
See also
- Man page for bash or ksh shell.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Both errors and warning are thrown into stderr is there any way we can identify that the command has a warning in it and not the error ??
Apart from checking the return code !!