Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Command

Description

find

Code Block
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:
...
EXPRESSION
       The  part  of the command line after the list of starting points is the expression.  This is a
       kind of query specification describing how we match files and what we do
...
  TESTS
    ...
    -name pattern
      Base of file name (the path with the leading directories removed) matches shell pattern pattern.
    ...
    -iname pattern
       Like -name, but the match is case insensitive.
    ...

...

Examples

Info

Find the file named: 20230121.txt

Code Block
[]$ cd ~/intro_to_linux/
[intro_to_linux]$ find . -name 20230121.txt
./data/2023/Jan/20230121.txt
# Check that this file is within the returned location.
[intro_to_linux]$ ls data/2023/Jan/
20230102.txt  20230108.txt  20230115.txt  20230121.txt
# Nothing returned – no file exists called “20230120.txt”
Info

Find the file named: 20230120.txt

Code Block
[intro_to_linux]$ find . -name 20230120.txt
[arcc-t05@blog1 intro_to_linux]$
Info

The command completed since we got back to the prompt and no errors were displayed.

No output means that this file could not be found.

Info

Find is case-sensitive. Find the file with the exact filename README.txt

Code Block
[intro_to_linux]$ find . -name README.txt
./data/2021/README.txt
# find is case–sensitive: use –iname option
Info

Use the alternative -iname option to search for a file name that is case-insensitive:

Code Block
[intro_to_linux]$ find . -iname README.txt
./data/2021/README.txt
./data/2022/readme.txt
./data/2023/ReadMe.txt

...

Examples

Code Blockinfo
#

Use

wildcards

to

find

all

files

with

the

postfix

.csv:

Code Block
[intro_to_linux]$ find . -name "*.csv"
./software.csv
./data/2022/Hello.csv
# Find any 
Info

Find any files/folders

that

contain

the

string “dec”

string dec. Case-sensitive versus case-insensitive.

Code Block
[arcc-t05@blog1 intro_to_linux]$ find . -name "*dec*"
./data/2022/Dec/2022_dec_01.txt

[arcc-t05@blog1 intro_to_linux]$ find . -iname "*dec*"
./data/2022/Dec
./data/2022/Dec/2022_dec_01.txt
# Find only folders.
Info

Find only folders using the type option and d for directory.

Question: Are we searching with respect to case-sensitive or insensitive?

Code Block
[arcc-t05@blog1 intro_to_linux]$ find . -type d -iname "*dec*"
./data/2022/Dec
# Find only files
Info

Find only files using the f (for file) value for the type option.

Code Block
[arcc-t05@blog1 intro_to_linux]$ find . -type f -iname "*dec*"
./data/2022/Dec/2022_dec_01.txt

...