How to find a file in Linux

How to find a file in Linux

There are a few ways to find a file in Linux. 

The most used way is in Terminal as this can become part of a script very easily and automate tasks on a Linux server.

If you are using a desktop environment such as Gnome or Kde, the search for files is as simple as just typing text in a search box.

While it is easy to do in GUI desktop mode, the CLI command line interface needs a bit of typing.

Most times you would need to do this in a script and the find command is very useful at finding all the files of a specific type and change their properties. 

A script once written and tested can be re-applied many times with predictibe output results and it is much faster than mouse clicks and manual typing.

If you had to rename all test.jpg files to test-final-for-real.jpg on 100 server you would want to use a script that is executed at the same time on all systems.

Find by name

In command line (CLI) or SSH session you can use the find command.

You pass the path where it should search and the file name or partial file name.

If you don’t pass any path it defaults to the current directory.

find /path/ options file-name

Examples

sudo find / -name dragos
sudo find / -name *.log
sudo find /var/ -name *.log
sudo find /var/log/ -name *.log

Find by type

Lets you find all directories or regular files and you can combine options. 

You can look for specific file names or directory names in the search directory.

To find all files of a specific type you can use the -type option setting.

For -type most used options are: f – a regular file; d – directory; l – symbolic links

Examples

sudo find / -name dragos -type f  # all files named dragos in /
sudo find / -name dragos -type d  # all directories named dragos in /

Find by size

Finding large files is a quick way to free-up disk space so this can be useful to find the big files on your system.

With this one, it’s better not to specify name or type options on the first run so that you can see everything.

Examples

sudo find / -size +1G # use M for Megabytes, G for Gigabytes

If too many files show, you can change the -size paramter to increase the size requirements.

Output results to files

If you might need to look at this as a list or go thru them in a spreadsheet later, you can use > to send all output from the command to a file you specify.

Examples

sudo find / -size +50M > 50MB_file_list
sudo find / -size +500M > 500MB_file_list

To view the file contents you can use cat, tail, less, more commands or even vim, vi, nano text editors.

cat 500MB_file_list # see the list
tail -50f 500MB_file_list # see the list and follow any appends - show on screen
vim 500MB_file_list # open the file for editing
less 500MB_file_list # see the latest changes and be able to scroll and see previous changes also.

Real-world uses

Change permissions on all sub-directories 

This is a quick and convenient way to change permissions using find. 

This can be done on directories

find /var/wwwroot/diyrednumberone/ -type d -exec chmod 755 {} \;

It can also be used to change the permissions of all files in a directory

find /var/wwwroot/diyrednumberone/ -type f -exec chmod 644 {} \;

Find and move all log files to another directory

The files can be copied also and you can use the other options on the find command to move only large files if needed.

find /var/wwwroot/diyrednumberone/ -iname '*.log' -exec mv '{}' /var/wwwroot/diyrednumberone/logs/ \;

Bonus find in files

To find files containing a specific string it’s much better to sue grep.

Examples

grep -rnw '/var/wwwroot/diyrednumberone/' -e 'exception'
grep -inr '/var/wwwroot/diyrednumberone/' -e 'exception'

You can still use find to find in file contents by combining find and grep with xargs

Example

Looks in all files and pases the file list as arguments with xargs to grep

find / -type f | xargs grep 'exception-text'

Commands

[dragos@localhost ~]$ sudo find / -name dragos
/home/dragos
/run/sudo/ts/dragos
find: ‘/run/user/1001/gvfs’: Permission denied
/var/db/sudo/lectured/dragos
/var/spool/mail/dragos
[dragos@localhost ~]$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:

operators (decreasing precedence; -and is implicit where no others are given):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
      -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
      -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
      -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context CONTEXT


actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
      -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
      -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
      -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Report (and track progress on fixing) bugs via the findutils bug-reporting
page at http://savannah.gnu.org/ or, if you have no web access, by sending
email to <bug-findutils@gnu.org>.
[dragos@localhost ~]$


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.