...
Table of Contents | ||
---|---|---|
|
...
02.01a Searching for Files: find
Let us look at a folder with many subfolders and files.
Code Block |
---|
[]$ cd ~/intro_to_linux
[intro_to_linux]$ ls
clusters data software.csv
[intro_to_linux]$ ls -R
.:
clusters data Intro_to_linux.pdf software.csv vegatables.txt workshop_all.txt workshop_me.txt
./clusters:
beartooth.html loren.html teton.html wildiris.html
./data:
2021 2022 2023 dd.tx
./data/2021:
Apr Nov Sep
./data/2021/Apr:
20210403.txt 20210427.txt 20210428.txt |
...
02.01b Searching for Files: find
Code Block |
---|
./data/2021/Nov:
20211114.txt 20211115.txt 20211116.txt hello.txt
./data/2021/Sep:
20210908.txt 20210921.txt
./data/2022:
Dec Feb Hello.csv Jul Jun
./data/2022/Dec:
20221207.txt 20221220.txt 20221230.txt 20221231.txt
./data/2022/Feb:
20220203.txt 20220223.txt
./data/2022/Jul:
20220720.txt 20220722.txt 20220723.TX
... |
...
02.02 Searching for Files: find
Command | Description | ||
find |
|
...
02.03 Examples
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”
[intro_to_linux]$ find . -name 20230120.txt
[arcc-t05@blog1 intro_to_linux]$
[intro_to_linux]$ find . -name README.txt
./data/2021/README.txt
# find is case–sensitive: use –iname option
[intro_to_linux]$ find . -iname README.txt
./data/2021/README.txt
./data/2022/readme.txt
./data/2023/ReadMe.txt |
...
02.04 Examples
Code Block |
---|
# Use wildcards to find all files with the postfix .csv:
[intro_to_linux]$ find . -name "*.csv"
./software.csv
./data/2022/Hello.csv
# Find any files/folders that contain the string “dec”
[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.
[arcc-t05@blog1 intro_to_linux]$ find . -type d -iname "*dec*"
./data/2022/Dec
# Find only files
[arcc-t05@blog1 intro_to_linux]$ find . -type f -iname "*dec*"
./data/2022/Dec/2022_dec_01.txt |
...
02.05 Exercises
Questions:
What do we notice about some of the
find
command options?Find any files that contain the string “
hello
”, regardless of case, within their filename.Find any folders or files that contain the string “
feb
” regardless of case.Can you list only the folders?
Find any files that have the postfix “
tx
” – must be lowercase.
...
02.06 Answers
1: What do we notice about some of the find command options?
That some of the single dash options (
-name
) are similar to long-names and not single letters.
2: Find any files that contain the string “hello
”, regardless of case, within their filename.
Code Block |
---|
[intro_to_linux]$ find . -name "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 |
...
02.07 Answers
3: Find any folders or files that contain the string “feb
” regardless of case.
Can you list only the folders?
Code Block |
---|
[intro_to_linux]$ find . -name feb
./data/2021/feb
[intro_to_linux]$ find . -iname feb
./data/2021/feb
./data/2023/Feb
[intro_to_linux]$ find . -iname "*feb*"
./data/2021/feb
./data/2021/feb/february_01_2021.tx
./data/2022/February
./data/2023/Feb
[intro_to_linux]$ find . -type d -iname "*feb*"
./data/2021/feb
./data/2022/February
./data/2023/Feb |
...
02.08a Answers
4: Find any files that have the postfix “tx
” – must be lowercase.
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 |
...
02.08a Answers
Code Block |
---|
# dd.tx is actually a folder.
# Notices the ’d’ in the long format list.
[intro_to_linux]$ ls -l data
total 4
drwxrwxr-x 6 arcc-t05 arcc-t05 2021
drwxrwxr-x 6 arcc-t05 arcc-t05 2022
drwxrwxr-x 5 arcc-t05 arcc-t05 2023
drwxrwxr-x 2 arcc-t05 arcc-t05 dd.tx
[intro_to_linux]$ find . -type f -name "*.tx"
./data/2021/feb/february_01_2021.tx
# We explicitly want lowercase.
[intro_to_linux]$ find . -type f -iname "*.tx"
./data/2021/feb/february_01_2021.tx
./data/2022/20220723.TX |
...
...