Thursday, July 9, 2015

How Do I Search My Linux and Unix Server For a File?

You need to either use find command or locate command to search files on a Linux or Unix-like server.

Find command


The find command is used to find files on a Linux or Unix like system. It will search directories you specify for files that match the supplied search condition. A search condition can be used to search files by name, owner, group, type, permissions, date, time, case, and other criteria. The search is recursive i.e. it will search all subdirectories.ooks like this:
find {dir-name} -name {file-name} action
OR
find where-to-look criteria action
The default action is to print file names:
find /dir/ -name "file-to-search" -print

Find command examples

To find out a file called foo.txt in entire server, type:
# find / -name foo.txt
To find out httpd.log file:
# find / -type f -name httpd.log
To find out httpd.log file in /home/web-server/ directory, type:
# find /home/web-server/ -type f -name httpd.log
To find out httpd.log file in /home/web-server/ directory without case i.e. match httpd.log, HTTPD.LOG, HTTpd.LOG case, type:
# find /home/web-server/ -type f -iname httpd.log
To find out all php file (*.php) in /var/www/ directory, enter:
# find /var/www/ -type f -iname "*.php" -print

Find all files except "*.c" in a directory called ~/projects/?

To find all files except a specific pattern i.e. invert the search with -not or !.
# find /dir/to/search/ -not -name "*.c" -print
# find $HOME -not -iname "*.c" -print

Or
# find /dir/to/search/ \! -name "*.c" print
# find $HOME \! -iname "*.c" print

Finding file by type

The syntax is as follows:
find /dir/to/search/ -type X -name "file_pattern" -print
find $HOME -type X -iname "file_pattern" -print
Where -type X can be any one of the following chracter:
  1. f : Search for normal file only.
  2. d : Search for directory only.
  3. l : Search for symbolic link only
To search for all *.pl (perl) files in /var/www/, enert:
find /var/www/ -type f -name "*.pl" -print
## OR case insensitive search ### 
find /var/www/ -type f -iname "*.pl" -print

How do find files by content on a Linux or UNIX based server?

grep 'string' *.txt
grep -R 'string' *.txt 
Search /etc/ directory for all files and only show files that content an IP address 192.168.1.5:
# find /etc/ -iname "*" | xargs grep '192.168.1.5'
Executing commands on files
You can run any command (such as rm or cp) on files that located using find command. The syntax is:
 
find /dir/to/search [options] -name "file_pattern" -exec command-name1 {} \;
find /dir/to/search [options] -iname "file_pattern" -exec command-name1 {} \;
find /dir/to/search type f -iname "file_pattern" -exec command-name1 -arg1 {} \;
 
In this example, find all *.php files as set permission to 0644 using chmod command:
# find /var/www/ -type f -iname "*.php" -exec chmod 700 {} \;

Finding files by owner or group

To find all files owned by the "www" user, type:
# find / -user www -print
To find all files owned by the "lighttpd" group, type:
# find / -group lighttpd -print


Find files that do not have any owners or do not belong to any user under Linux/UNIX

You can use find command to find out all files that do not have any owners or do not belong to any user under Linux/UNIX/BSD operating systems.
Find file owned by user id 1050 (useful if you deleted user account):
# find / -nouser
Find out all files that are not owned by any user:
Find out all files that are not owned by any group:
# find / -nogroup
For example in real life on busy clustered hosting server some time we remove 5-10 users and for security reasons you need to find out all files are not owned by any user or group then you can type command:
# find / -nogroup -nouser