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.
    ...
Info

The find command is naturally recursive.

...

Examples

Info

Find the file named: 20230121.txt

...

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 The find command is case-sensitive. Find the file with the exact filename README.txt

...

Info

Use wildcards to find all files with the postfix .csv:

Lets look at two versions:

Info

With quotes:

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

Without quotes:

Code Block
[intro_to_linux]$ find . -iname *.csv
./software.csv
Note

Using or not using quotes across commands is an advanced and confusing subject.

If you do not use quotes, then if you have a file in the current directory ending with .csv, the wildcard is essentially expanded by the shell.

So, if you have a file named say "software.csv" (which we do), the command that gets executed is find . -name software.csv.

This single file is found, and the command stops.

Surrounding the search term with quotes prevents this from happening.

Info

Find any files/folders that contain the string dec. Case-sensitive versus case-insensitive.

...

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

...

Exercises: Find Files

Info

Questions:

  1. What do we notice about some of the find command options?

  2. Find any files that contain the string “hello”, regardless of case, within their filename.

  3. Find any folders or files that contain the string “feb” regardless of case.

    1. Can you list only the folders?

  4. Find any files that have the postfix “tx” – must be lowercase.

...

Code Block
[intro_to_linux]$ find . -name "hello"hello“

[intro_to_linux]$ find . -name "hello.*"
./data/2021/Nov/hello.txt

[intro_to_linux]$ find . -iname "hello.*"
./data/2021/Nov/hello.txt
./data/2022/Hello.csv
./data/2023/Mar/HELLO.txt

...

Code Block
[intro_to_linux]$ find . -name "tx"

[intro_to_linux]$ find . -name "*tx*"
./data/2021/README.txt
./data/2021/Nov/20211115.txt
./data/2021/Nov/hello.txt
./data/2021/Nov/20211114.txt
…
[intro_to_linux]$ find . -name "*tx"
./data/dd.tx
./data/2021/feb/february_01_2021.tx
./data/2023/Jan/texttx
[intro_to_linux]$ find . -name "*.tx"
./data/dd.tx
./data/2021/feb/february_01_2021.tx

...

Answers (4)

Code Blockinfo
#

Notice: dd.tx

is

actually

a

folder. # Notices the ’d’ in the long format list.

folder, defined by the ’d’ in the long format list.

Code Block
[intro_to_linux]$ ls -l data
total 4
drwxrwxr-x 6 arcc-t05 arcc-t05<username> <username> 2021
drwxrwxr-x 6 arcc-t05 arcc-t05<username> <username> 2022
drwxrwxr-x 5 arcc-t05 arcc-t05<username> <username> 2023
drwxrwxr-x 2 arcc-t05 arcc-t05<username> <username> dd.tx

[intro_to_linux]$ find . -type f -name "*.tx"
./data/2021/feb/february_01_2021.tx
# We explicitly want lowercase.
Note

If we (forget and) ignore the case (using iname), we would see:

Code Block
[intro_to_linux]$ find . -type f -iname "*.tx"
./data/2021/feb/february_01_2021.tx
./data/2022/20220723.TX

...

Notice this has listed a file with a capital a postfix .TX - this is not what we wanted.

...

...