Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 1 · Basics · Lesson 08 · 55 minutes

Find files and read their details

After this lesson you can
  • Find files by name, type, size and age with find
  • Read the details of a file with stat and file
  • Make and read hard links and symbolic links
Lab files of this lesson (8)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

A colleague hands you the copy of a small web site from a server that was probably broken into. It holds nine files in four folders now, but on a real site it would hold thousands. Your team lead asks: "Which files are settings files? Which are big? Which ones are old and should not have changed? And is anything in there only a pointer to another file?" You will answer all of this from the terminal.

Recall

What does "2> /dev/null" do, and when do you use it?

It throws away the error messages of a command. You use it to hide noise that you expect, such as "Permission denied" for folders that you may not read. Do not use it to hide errors that you need to see.

What does "grep -rl word ." list?

The names of the files below the current folder that contain the word. -r goes into folders, and -l prints only the names.

The idea

find PLACE TESTS walks through a folder and every folder below it, and prints each file that passes all the tests. The order of its output is not fixed, so add | sort when you need a stable list.

TestIt keeps files that
-name "*.conf"have a name that fits the pattern (* means any text). -iname ignores case.
-type f, -type d, -type lare files, directories or symbolic links.
-size +5k, -size -20care bigger than 5 KiB, or smaller than 20 bytes. c is bytes, k is KiB.
-mtime +30, -mmin -60changed more than 30 days ago, or in the last 60 minutes.
! before a testdo not pass that test.
A find command, taken apart
findwalk through folders.start here-type fkeep regular files-mtime +30changed over 30 days agofindwalk through folders.start here-type fkeep regular files-mtime +30changed over 30 days ago

Next to its name, a file has an inode: the record that holds everything about the file except its name, such as its size, owner, permissions, times and the place of its data on the disk. A directory is a list of names, and each name points to an inode. That gives two kinds of link.

One inode with three names
Names in a folderThe diskreport-soft.txtreport.txtreport-hard.txtInode and data123Names in a folderThe diskreport-soft.txtreport.txtreport-hard.txtInode and data123
  1. report.txt → Inode and data: the first name points to the data
  2. report-hard.txt → Inode and data: a hard link: same inode
  3. report-soft.txt → report.txt: a soft link points to a name

A hard link is one more name for the same inode, so both names are equal and the data stays until the last name is gone. A symbolic link (soft link) is a small file that holds a path. If the path is gone, the link is broken.

Worked example

1. Build a small web site

mkdir -p with several names makes several folders. touch -d "DATE" sets the time of change of a file to a date that you give, so that some files are old. Every reader gets the same old files.

mkdir -p ~/krlab/linux-basics/site && cd ~/krlab/linux-basics/site
mkdir -p css img conf backup
echo "<h1>home</h1>" > index.html
echo "<h1>about</h1>" > about.html
echo "body { color: black; }" > css/style.css
echo "body { color: red; }" > css/old.css
seq 1 2000 > img/banner.png
echo "logo" > img/logo.png
echo "port = 80" > conf/app.conf
echo "password = not-a-real-one" > conf/db.conf
echo "html copy" > backup/index.html.bak
touch -d "2026-01-10 09:00" css/old.css conf/app.conf backup/index.html.bak

2. Find files by name

Put the pattern in quotes, so that the shell does not expand the * before find sees it.

find . -name "*.conf" | sort
find . -name "INDEX*"
find . -iname "INDEX*" | sort
./conf/app.conf
./conf/db.conf
./backup/index.html.bak
./index.html

The first command found two settings files. The second found nothing, because INDEX is not index. The third ignored the case and found two.

3. Find files by type, size and age

-type d lists directories, and -type f lists regular files. banner.png has 8893 bytes, so -size +5k finds it. -mtime +30 finds the three files that touch -d made old.

find . -type d | sort
find . -type f | wc -l
find . -size +5k
find . -type f -mtime +30 | sort
.
./backup
./conf
./css
./img
9
./img/banner.png
./backup/index.html.bak
./conf/app.conf
./css/old.css

Tests join with "and". This line finds the files that are not HTML and are not old, and -exec runs a command on the results: ls -l shows their details. The {} is the place of the file names, and + ends the command.

find . -type f ! -name "*.html" ! -mtime +30 -exec ls -l {} +
-rw-rw-r-- 1 you you   26 Nov 21 10:56 ./conf/db.conf
-rw-rw-r-- 1 you you   23 Nov 21 10:56 ./css/style.css
-rw-rw-r-- 1 you you 8893 Nov 21 10:56 ./img/banner.png
-rw-rw-r-- 1 you you    5 Nov 21 10:56 ./img/logo.png

The order of the lines can differ on your machine.

Find looks at names and facts. To look inside the files, use grep of lesson 4: the settings file with a password is found by its content.

grep -rl password .
./conf/db.conf

4. Find a file with locate

locate TEXT prints the paths that hold the text. It does not walk through the disk. It reads a database of all names, which the command updatedb builds (the system does it once a day). That is fast, but the database can miss a file made today and can list one that is gone. locate -i ignores case, and locate -c counts. The package is plocate (or mlocate): install it with sudo apt install plocate. A file that you made a minute ago is not in the database, so run sudo updatedb first. The pattern /conf/db.conf keeps locate to your file.

sudo updatedb
locate -i /conf/DB.CONF
locate -c /conf/db.conf
find ~/krlab -name "db.conf"
/home/you/krlab/linux-basics/site/conf/db.conf
1
/home/you/krlab/linux-basics/site/conf/db.conf

The find command does the same job, always from the live disk. On the whole system, use find / -name db.conf 2>/dev/null. It is slower, and 2>/dev/null hides the "Permission denied" lines.

5. Read the details with stat and file

stat prints the facts that the inode holds. -c sets your own format: %n is the name and %s is the size.

stat conf/db.conf
stat -c '%n is %s bytes' conf/db.conf
  File: conf/db.conf
  Size: 26        	Blocks: 8          IO Block: 4096   regular file
Device: 0,43	Inode: 58589796    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ you)   Gid: ( 1000/ you)
Access: 2026-11-21 10:56:44.890291392 +0000
Modify: 2026-11-21 10:56:44.819284766 +0000
Change: 2026-11-21 10:56:44.819284766 +0000
 Birth: 2026-11-21 10:56:44.819284766 +0000
conf/db.conf is 26 bytes

The inode number, the device and the times are different on your machine. Three times are kept. Access is the last read. Modify is the last change of the content. Change is the last change of the inode (for example of the permissions). Links counts the names of the inode.

file looks at the content of a file, not at its name, and says what kind of data it is. Here the name logo.png is not true: the file is text.

file index.html img/logo.png conf
index.html:   ASCII text
img/logo.png: ASCII text
conf:         directory

ln OLD NEW makes a hard link. ln -s TARGET NEW makes a symbolic link. ls -li shows the inode number in front of each name. readlink prints where a symbolic link points. The target of ln -s is stored as typed, and the system reads a relative target from the folder of the link, not from the folder where you are.

mkdir -p ~/krlab/linux-basics/links && cd ~/krlab/linux-basics/links
echo "case 417: notes" > report.txt
ln report.txt report-hard.txt
ln -s report.txt report-soft.txt
ls -li
readlink report-soft.txt
total 8
58589818 -rw-rw-r-- 2 you you 16 Nov 21 10:56 report-hard.txt
58589819 lrwxrwxrwx 1 you you 10 Nov 21 10:56 report-soft.txt -> report.txt
58589818 -rw-rw-r-- 2 you you 16 Nov 21 10:56 report.txt
report.txt

The two hard links have the same inode number and the link count 2. The symbolic link has its own inode. Now delete the first name, and read both links.

rm report.txt
cat report-hard.txt
cat report-soft.txt
find . -xtype l
case 417: notes
cat: report-soft.txt: No such file or directory
./report-soft.txt

The data survived under the hard link. The symbolic link is broken: it points to a name that is gone. find -xtype l lists the broken links. find . -samefile report-hard.txt lists every name of one inode. A hard link cannot point to a directory, and cannot cross from one file system to another. A symbolic link can do both.

Your turn

Run this block once. It builds a shop folder with fixed content.

cd ~/krlab/linux-basics && rm -rf shop
mkdir -p shop/logs shop/data shop/tmp
printf 'start\nok\n' > shop/logs/app.log
seq 1 3000 > shop/logs/big.log
echo "warning" > shop/logs/old.log
echo "name,total" > shop/data/orders.csv
echo "key=abc123" > shop/data/secret.env
echo "scratch" > shop/tmp/scratch.txt
echo "readme" > shop/README
touch -d "2026-02-01 12:00" shop/logs/old.log shop/data/orders.csv

Do these tasks in order. There are no steps here.

  1. Find the regular files of shop that changed more than 30 days ago.
  2. Find the one file that is bigger than 10 KiB.
  3. Make the symbolic link shop/latest.log that points to logs/big.log.
  4. Make the hard link shop/data/orders-copy.csv to shop/data/orders.csv.

Last, find the file that holds the text key=, and say what file calls it.

Check yourself

Why do you write the pattern in quotes in "find . -name '*.conf'"?

Without quotes the shell expands *.conf first, to the .conf files of the current folder. Then find gets a list of names and not a pattern. Quotes give the pattern to find unchanged.

You delete the original of a file. Which link still works, the hard or the symbolic one?

The hard link. Both names point to the same inode, and the data is freed only when the last name is gone. The symbolic link holds the old path and is broken.

A file is called "photo.jpg". What does "file photo.jpg" tell you that the name does not?

The real type of the content. file reads the first bytes of the file. A program of an attacker can carry any name, so a name is a claim, not a fact.

Why can "locate" miss a file that "find" shows?

locate reads a database that is built once a day. A file made after the last update is not in it. find reads the disk as it is now.

Field notes

  • -exec COMMAND {} + runs the command on many files at once. -delete removes what matches. Run the command with -print first, read the list, and only then change it to -delete.
  • File names can hold spaces and new lines. With xargs, use find -print0 | xargs -0, or use -exec.
  • The Change time of an inode cannot be set with touch. If Modify is old and Change is new, someone changed the file and set back its time. Treat it as a clue.
  • A symbolic link in a folder that other users write to is a classic attack. A privileged program writes to a name that the attacker has replaced by a link to a system file. Audit links with find / -type l, and use readlink -f to see the final target.
  • locate lists names, and the database may show a file name that a user is not allowed to read. plocate checks the permissions of the user.