Finding files on Linux in all sorts of ways


The Linux find command can locate files based on almost any criteria that you might need. This post describes the many criteria you can use to find what you’re looking for – even when you can’t remember what you named a file or when you last changed it or added content.

Basic find syntax

The basic syntax for the find command looks like this:

$ find [starting location] [criteria] [options] [action to take]

The starting location can be a directory name (e.g., /var/log), the current directory (.), your home directory whether you’re sitting in it or not (~), or a directory relative to your current position (e.g., ./bin). You can be as specific as you want when entering the starting location.

Finding files by name

Searching for files by name is the easiest and most straightforward way to find a file. Note the command below uses an asterisk so that it will find any files that start with “fix”.

$ find ~ -name "fix*" -print
/home/shs/fixit

Finding files by type

You can also find files that are of a specific type (e.g., a file, directory or symbolic link)  To find a file, use “-type f”. 

$ find Documents -name junk* -type f -print
Documents/junk.odt
Documents/junk.docx

To find a symbolic link, use “-type l”.

$ find . -name "h*" -type l -ls
    28603      0 lrwxrwxrwx   1 shs      shs             9 Jun 27 12:27 ./hold -> /tmp/hold4me

To find a directory, use “-type d”.

$ find . -type d -ls | head -3
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 14:24 .
     3467      4 drwxr-xr-x   2 shs      shs          4096 May 20  2021 ./Pictures
     3468      4 drwxr-xr-x   2 shs      shs          4096 May  7  2021 ./Videos

Determining what you want to see or do

You probably noticed in the two previous commands that you can list (-ls means a long listing) or simply display the name of a file (-print). These are not the only options.

To delete a file, you need to add the -exec rm command as shown below. This command will remove any file with a “.old” extension from your home directory (including subdirectories).

$ find ~ -name *.old -exec rm {} ;

The exec command can even allow you to view the contents of a file. Here’s an example:

$ find . -name "h*" -type l -exec cat {} ;
Meow, Meow!

Finding files by owner and/or group

To find files by user, include the “-user uname” specification. You can specify the username or the user ID. The third example below send output that generates an error (e.g., permission denied) to /dev/null so that it doesn’t clutter up the screen.

$ find . -user shs -ls | head -4
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 13:18 .
     5617      4 -rw-rw-r--   1 shs      shs            22 Feb 15  2021 ./CHECKME
     8001      4 -rwx------   1 shs      shs           150 Feb 25  2021 ./send_msg
    12727     24 -rw-rw-r--   1 shs      shs         20805 Apr 15  2021 ./history-rece
$ find . -user 1000 -ls | head -4
     3389      4 drwxr-x---  25 shs      shs          4096 Jun 27 13:18 .
     5617      4 -rw-rw-r--   1 shs      shs            22 Feb 15  2021 ./CHECKME
     8001      4 -rwx------   1 shs      shs           150 Feb 25  2021 ./send_msg
    12727     24 -rw-rw-r--   1 shs      shs         20805 Apr 15  2021 ./history-recent
$ find /tmp -user shs -ls 2> /dev/null
 26476575      4 -rw-rw-r--   1 shs      shs        10 Jun 27 12:44 /tmp/haha
 26476577      4 drwx------   2 shs      shs      4096 Jun 27 12:09 /tmp/tree
 26476585      0 srwxrwxrwx   1 shs      shs         0 Jun 27 12:09 /tmp/dbus
 26476595      4 -r--r--r--   1 shs      shs        11 Jun 27 12:09 /tmp/.X1-lock

Depending on file permissions, you might be able to find files belonging to other users as well.

$ find /home -group tom -print
/home/tom
/home/tom/report
/home/tom/myfile

To find files by group, use the “-group gname” specification. Group IDs can be used in place of group names.

$ find /tmp -group 1000 -ls 2>/dev/null
 26476575      4 -rw-rw-r--   1 shs      shs            10 Jun 27 12:44 /tmp/haha
 26476577      4 drwx------   2 shs      shs          4096 Jun 27 12:09 /tmp/tracker-extract-files.1000
 26476585      0 srwxrwxrwx   1 shs      shs             0 Jun 27 12:09 /tmp/dbus-HpFApADlMO
 26476595      4 -r--r--r--   1 shs      shs            11 Jun 27 12:09 /tmp/.X1-lock
 26476580      0 srwxrwxrwx   1 shs      shs             0 Jun 27 12:09 /tmp/.ICE-unix/2051
 26476596      0 srwxrwxr-x   1 shs      shs             0 Jun 27 12:09 /tmp/.X11-unix/X1
 26476594      0 srwxrwxr-x   1 shs      shs             0 Jun 27 12:09 /tmp/.X11-unix/X0
 26476586      4 -r--r--r--   1 shs      shs            11 Jun 27 12:09 /tmp/.X0-lock

Finding files by file permissions

To find files that have group write permissions, you could use a command like this one:

$ find /usr/bin -name "net*" -perm -g=w -ls
    26666  0 lrwxrwxrwx   1 root  root   24 Feb 14  2021 /usr/bin/netcat -> /etc/alternatives/netcat

Notice that the beginning of the file name is included or there would be more files listed.

You can also look for files by file permissions as in the following example that looks for files that provide all access (777):

$ find /usr/bin -name "d*" -perm 777 -ls | head -3
    34026      0 lrwxrwxrwx   1 root     root            9 Mar 12  2021 /usr/bin/dvipdfmx -> xdvipdfmx
    29934      0 lrwxrwxrwx   1 root     root           18 Jan 29  2021 /usr/bin/distro-info -> ubuntu-distro-info
    33159      0 lrwxrwxrwx   1 root     root            6 Feb 17  2021 /usr/bin/dvilualatex -> luatex

Finding files by age

To find a file by age, use the -mtime option and specify the number of days since the file was modified using a number like +100 (more than 100 days old) or -10 (modified within the last 10 days).

$ find Documents -mtime -1
Documents
Documents/junk.doc

Finding files by size

To find empty files, use a command like this one:

$ find . -size 0 -ls | head -1
    28959      0 -rw-rw-r--   1 shs      shs             0 Jun 27 13:18 ./.abc

Without the addition of the head command in this example, this command would find a lot more empty files – representing the cache and such.

$ find . -size 0 -ls | wc -l
45

The next command finds files that are larger than 1 GB: Notice that the command sends all the “permission denied” messages to the /dev/null.

$ find / -type f -size +1G -ls 2>/dev/null
  12 2097156 -rw-------  1 root  root  2147483648 Feb 14  2021 /swapfile
  18 8388612 -rw-------  1 root  root  8589934592 Sep  9  2021 /mint/swapfile/40265

Previous posts on the find command

Some of my earlier posts on the find command are listed below:

https://www.networkworld.com/article/3527420/how-to-find-what-you-re-looking-for-on-linux-with-find.html
https://www.networkworld.com/article/3233305/using-the-linux-find-command-with-caution.html

Wrap-up

The find command has a lot of options that you can use to narrow down your search when you are looking for specific files on your Linux system.

Copyright © 2023 IDG Communications, Inc.



Source link