How to find files on Linux

$ find . -name myfile -print
./tests/eg/myfile
./tests/myfile

In the above example, the command displays only the name and location of the files that it finds. Use the -ls command instead of -print and you get the kind of details you’d expect when you list files with the ls -l command.

$ find . -name myfile -ls
    36417      4 -rw-r--r--   1 shs      shs       188 Jun 10 09:57 ./tests/eg/myfile
    36418      4 -rw-r--r--   1 shs      shs        83 Jun 10 09:57 ./tests/myfile

Understand that starting locations don’t have to be relative. You can always use a complete path like /home/jdoe or /usr/local/bin wherever you are sitting in the file system provided you have read access to those locations or use the sudo command to give you root-level access.

Finding files by partial name

To find differently named files that share only some portion of their filenames, enclose the shared portion of the file names in a string using quotes and use asterisks to specify the location of the variable portions of the file names (e.g., “*txt to find files with names that end in “txt”). In the command below, we find files that start with “zip”.

$ find /usr/bin -name "zip*"
/usr/bin/zipgrep
/usr/bin/zipinfo
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipnote
/usr/bin/zipsplit

Finding files by age

To find files that have been modified within the last 24 hours, use a command like this with the -mtime (modification time) option:

$ find . -mtime 0 -ls
     3060      0 drwx------   1 shs      shs          3050 Jun 10 10:35 .
    36415      0 drwxr-xr-x   1 shs      shs            16 Jun 10 09:54 ./tests
    36416      0 drwxr-xr-x   1 shs      shs            12 Jun 10 09:53 ./tests/eg

The 0 in that command means “0 days old” (i.e., less than a day old). You can also use -mtime with positive and negative signs. For example -mtime -2 or -mtime +4. Using -mtime -1 means “less than one day old”. Using -mtime +1 means “more than one day old”, so the results would be dramatically different.  If you’re looking for a file that you were working on a week ago, a command like this might work just fine:

$ find . -mtime -8 -ls

Another easy way to list recently modified files is to use the ls command. The -ltr arguments represent a long listing (-l), sort in newest-first time order (-t) and reverse the order (-r). Using these options, the files will be listed with the most recently modified files shown last.



Source link