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

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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