Using find Command in Linux

Using find Command in Linux

The find is a command line tool that allows to search files and directories in a directory hierarchy based on conditions such as name, size, type, permissions, owner, etc.

This post provides usage examples of find command. Examples have been tested on Ubuntu 20.04 LTS.

Let's say we have the following directory hierarchy:

test/
├── config/
├── data1/
│   ├── config
│   ├── file1.txt
│   └── File1.txt
├── data2/
│   ├── file2.csv
│   └── file2.txt
├── file1.csv
├── file1.txt
└── my_file1.txt

Directory hierarchy can be created using these commands:

mkdir -p test/config test/data1 test/data2
touch test/data1/config test/data1/file1.txt test/data1/File1.txt
touch test/data2/file2.csv test/data2/file2.txt
touch test/file1.csv test/file1.txt test/my_file1.txt

1. Find files by specific name

find test -name file1.txt

Output:

test/data1/file1.txt
test/file1.txt

2. Find files by specific name while ignoring case

find test -iname file1.txt

Output:

test/data1/file1.txt
test/data1/File1.txt
test/file1.txt

3. Find files by pattern

  • Find files which name starts with a specific string:
find test -name file2*

Output:

test/data2/file2.csv
test/data2/file2.txt
  • Find files which name ends with a specific string:
find test -name *file1.txt

Output:

test/data1/file1.txt
test/file1.txt
test/my_file1.txt

4. Find files by extension

find test -name *.csv

Output:

test/file1.csv
test/data2/file2.csv

5. Find files by type

  • Find regular files:
find test -type f -name config

Output:

test/data1/config
  • Find directories:
find test -type d -name config

Output:

test/config

Available file types:

TypeDescription
bBlock device
cCharacter device
dDirectory
pNamed pipe (FIFO)
fRegular file
lSymbolic link
sSocket

Leave a Comment

Cancel reply

Your email address will not be published.