Using BASH command Find to locate files – Part 2

Using User-Defined Actions:

P1. Prompt the user before findings.

[u1@node1 Downloads]$ find -type f -name 'rrr' -ok ls -l '{}' ';'
< ls ... ./rrr > ? Y
-rw-r--r-- 1 u1 users 0 Nov 19 10:36 ./rrr

where

  • {} is the symbolic representation of the current path
  • ; is the required delimiter indicating the end of the command
  • -ok can replace -exec so that the user can be prompted before executing any commands.

 

Pre-defined Action

P1. Find the file and delete the files immediately

[u1@node1 Downloads]$ find -type f -name '*.mov' -delete

P2. Find and quit once the match has been made

[u1@node1 Downloads]$ find -type f -name '*.mov' -quit

P3. Perform the equivalent of ls -dil on the match file.

[u1@node1 Downloads]$ find -type f -name '*.mov' -ls
4554823854 70266 -rw-r--r--   1 u1   users 62574861 Sep  5  2018 ./miniconda.sh

 

Using Operators

[u1@node1 Downloads]$ find \(-type -f -not -name '*.txt' \) -or \( -type -r -not -name '*.doc' \)
  • Parentheses have special meaning to the shell. Putting a backslash character in front of the parenthesis help to prevent shell from interpreting them.

 

Options

Set the maximum number of levels that find will descend when performing tests and actions.

[u1@hpc-gekko1 Downloads]$ find -maxdepth 2 -type f -name '*' -ls
4554823854 70266 -rw-r--r--   1 u1   users 62574861 Sep  5  2018 ./miniconda.sh

 

Using BASH command Find to locate files – Part 1

P1. Use command to list files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find

Pt 2. Count the number of files by piping the list to wc to count the files.

[u1@node1 lammps-7Aug19]$ find | wc -l
14717

P3. Count the the directories listing by piping the list to wc to count the directories

[u1@node1 lammps-7Aug19]$ find -type d | wc -l
672

P4. Count the list of files that matched the wildcard pattern *.mov that are larger than 1M by piping to wc to count

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -size +1M |wc -l

P5.  Find and match files or directories whose contents or attributes were last modified more recently than those of “mybasefile”

[u1@node1 lammps-7Aug19]$ find -type f -name "*.jpg" -cnewer mybasefile

P6. Find empty files in the lammps-7Aug19 directory

[u1@node1 lammps-7Aug19]$ find -type f -empty

Pt 7. Find files or directories that belongs to username

[u1@node1 Downloads]$ find -type f -user root |wc -l
1572

References:

  1. The Linux Command Line