Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux find Command with xargs: Search Files by Name, Size, Time and Run Actions

find walks through folders and lists files that match tests like name, type, size, age, owner or permission, and can run a command on each one. xargs takes a list of items and turns them into arguments for another command, fast and in parallel. Together they clean disks, fix permissions, audit security and batch-process thousands of files.

In this chapter

  • Find by name and type
  • Find by size and time
  • Find by owner and permission
  • Control depth, prune folders and combine tests
  • Run actions with -exec and -delete
  • Use xargs safely with -0, -n, -P and -I; know locate

1. An everyday example: cleaning a messy cupboard

Shalini cleans her big cupboard before Diwali. She looks for: everything named 'bill', only boxes (not papers), things heavier than 5 kg, things not touched for a year, and things that belong to her brother. Then for each old bill she does the same action: put it in the recycle bag. Her son helps by taking a whole pile at once instead of one by one.

Named bill = -name. Only boxes = -type d. Heavier than 5 kg = -size +5G. Not touched for a year = -mtime +365. Brother's = -user. Same action on each = -exec. Son taking a pile = xargs (many items per command).

How find worksStart at a folder (find /var/log ...)Walk into every sub-folder (depth first)For each file, run the tests left to rightAll tests true? Do the action (-print by default)-exec or xargs runs a command on the resultsErrors (Permission denied) go to stderr
Tests are ANDed by default. Use -o for OR and \( \) to group.

2. find by name: -name, -iname, -path

You know part of a file name but not where it is.

find START -name 'pattern' matches the file name with shell wildcards (* ? [ ]). Always quote the pattern so the shell does not expand it first. -iname ignores case. -path '*/conf.d/*' matches the full path. -regex uses a regex on the whole path. Send errors away with 2>/dev/null.

  1. Find a file by exact name.
find /etc -name 'nginx.conf' 2>/dev/null

Output:

/etc/nginx/nginx.conf

2>/dev/null hides Permission denied noise.

  1. All .log files under /var/log.
find /var/log -name '*.log' | head -3

Output:

/var/log/dnf.log
/var/log/nginx/access.log
/var/log/nginx/error.log

Quotes keep *.log for find.

  1. Ignore case.
find ~/Downloads -iname '*invoice*'

Output:

/home/ravi/Downloads/Invoice-Oct.pdf
/home/ravi/Downloads/invoice_amazon.pdf

Matches Invoice and invoice.

  1. Match on the path.
find /etc -path '*/conf.d/*.conf'

Output:

/etc/nginx/conf.d/shop.conf
/etc/httpd/conf.d/ssl.conf

Only .conf files inside any conf.d folder.

  1. Several names with OR.
find . \( -name '*.jpg' -o -name '*.png' \) | wc -l

Output:

342

\( \) groups, -o is OR.

3. find by type: -type f, d, l

You want only folders, or only regular files, or only symlinks.

-type f regular file, d directory, l symlink, s socket, p named pipe, b/c device files. -xtype l with broken links. -empty finds empty files or folders.

  1. Only folders.
find /srv/app -maxdepth 1 -type d

Output:

/srv/app
/srv/app/releases
/srv/app/shared

-type d means directory.

  1. Only files, counted.
find /etc -type f 2>/dev/null | wc -l

Output:

1874

Folders and links are skipped.

  1. Broken symlinks.
find /srv/app -xtype l

Output:

/srv/app/current

current points to a deleted release.

  1. Empty files and folders.
find /tmp/build -empty

Output:

/tmp/build/out.log
/tmp/build/cache

Safe to clean.

4. find by size: -size

The disk is full. You need the biggest files fast.

-size +100M bigger than 100 MiB, -size -1k smaller than 1 KiB, -size 0 empty. Units: c bytes, k KiB, M MiB, G GiB. Stay on one disk with -xdev. Combine with -exec du -h or ls -lh to see sizes.

  1. Files over 500 MB on the root disk.
sudo find / -xdev -type f -size +500M -exec ls -lh {} + 2>/dev/null

Output:

-rw-r----- 1 root root 2.1G /var/log/journal/x/system.journal
-rw-r--r-- 1 app app 900M /srv/app/shared/dump.sql

-xdev skips /proc and other disks.

  1. Top 5 biggest files.
find /var -type f -printf '%s %p\n' 2>/dev/null | sort -nr | head -5

Output:

2254857830 /var/log/journal/x/system.journal
943718400 /var/lib/mysql/ibdata1

-printf %s is size in bytes, %p the path.

  1. Small files under 1 KB.
find ~/notes -type f -size -1k | wc -l

Output:

48

- means smaller than.

5. find by time: -mtime, -mmin, -newer

Delete logs older than 7 days, or see which files changed in the last 10 minutes during an incident.

-mtime +7 modified more than 7 days ago, -mtime -1 within the last day, -mmin -10 within 10 minutes. -atime/-amin access time, -ctime/-cmin change of inode (permissions, owner). -newer ref finds files newer than a reference file. -newermt '2026-10-25 10:00' uses a date (GNU).

TestMeans
-mtime +7Older than 7 days
-mtime -1Changed in the last 24 h
-mmin -30Changed in the last 30 min
-newer fNewer than file f
-newermt DATENewer than a date
  1. Logs older than 7 days.
find /var/log/app -name '*.log' -mtime +7

Output:

/var/log/app/app-2026-10-10.log
/var/log/app/app-2026-10-12.log

Always list before you delete.

  1. Changed in the last 10 minutes.
sudo find /etc -mmin -10 -type f

Output:

/etc/nginx/conf.d/shop.conf

Who changed this right before the outage?

  1. Newer than a deploy marker.
find /srv/app -newer /srv/app/.deployed -type f | head -3

Output:

/srv/app/shared/uploads/a.png

Files written after the deploy.

  1. Between two dates.
find /data -newermt '2026-10-01' ! -newermt '2026-10-08' -type f | wc -l

Output:

1204

! negates the second test.

6. find by owner and permission: -user, -group, -perm

Security audits ask: which files are world-writable, which have SUID, which belong to a deleted user.

-user name, -group name, -nouser, -nogroup (orphaned files). -perm 644 exact mode, -perm -o+w at least these bits (world-writable), -perm /u+s any of these bits, -perm -4000 SUID. -readable, -writable, -executable test for the current user.

  1. Files owned by a user.
sudo find /home -user rahul -type f | wc -l

Output:

2310

Before deleting rahul's account.

  1. World-writable files (risky).
sudo find / -xdev -type f -perm -o+w 2>/dev/null

Output:

/srv/app/shared/config.php

Anyone can change this file. Fix it.

  1. SUID programs.
sudo find / -xdev -perm -4000 -type f 2>/dev/null | head -3

Output:

/usr/bin/passwd
/usr/bin/sudo
/usr/bin/mount

Compare against a known list in audits.

  1. Orphaned files.
sudo find / -xdev -nouser 2>/dev/null

Output:

/opt/old/app.jar

Owner UID no longer exists.

7. Depth, prune and combining tests

Before combining tests, decide which names belong in the output. An implicit AND requires both tests to succeed. -o combines alternatives. To match regular files with either .txt or .csv, put the name alternatives together in quoted or escaped parentheses. If you omit that grouping, the second name test can admit paths that did not pass the file-type test.

Use a scratch tree with two ordinary files and a directory whose name ends in .csv. Predict two matching paths and run find . -type f \( -name '*.txt' -o -name '*.csv' \) -print. Confirm the directory is absent. Preview the exact candidate list before replacing any display action with a command that changes files.

Searching / or a big repo is slow, and you want to skip .git, node_modules or /proc.

-maxdepth N stops going deeper than N levels, -mindepth N skips the top levels (put both first). -prune stops find from entering a folder: find . -path ./node_modules -prune -o -name '*.js' -print. -xdev stays on one file system. Tests join with AND; -o is OR; ! or -not negates; \( \) groups.

  1. Only the top level.
find /var/log -maxdepth 1 -type f -name '*.log'

Output:

/var/log/dnf.log
/var/log/boot.log

Does not go into sub-folders.

  1. Skip node_modules.
find . -path ./node_modules -prune -o -name '*.js' -print | head -3

Output:

./src/app.js
./src/cart.js

-prune cuts that branch; -print is needed at the end.

  1. NOT a pattern.
find . -type f ! -name '*.md' | head -2

Output:

./src/app.js
./Dockerfile

! negates -name.

  1. Combine: big AND old.
find /backup -type f -size +1G -mtime +30

Output:

/backup/db-2026-09-01.sql.gz

Both tests must be true.

8. Actions: -exec, -ok, -delete, -printf

Finding is half the job. You need to delete, compress, chmod or copy what you found.

-exec cmd {} \; runs cmd once per file ({} is the file). -exec cmd {} + passes many files at once (much faster). -ok asks before each run. -delete removes matches (put it last; it implies -depth). -printf formats output: %p path, %s size, %TY-%Tm-%Td date, %u owner. -ls prints like ls -l.

  1. Compress old logs.
find /var/log/app -name '*.log' -mtime +3 -exec gzip {} +

Output:

(no output)

{} + gives gzip many files per run.

  1. Fix folder permissions.
find /srv/www -type d -exec chmod 755 {} + && find /srv/www -type f -exec chmod 644 {} +

Output:

(no output)

Folders 755, files 644: the classic web fix.

  1. Ask before each delete.
find ~/tmp -name '*.bak' -ok rm {} \;

Output:

< rm ... /home/ravi/tmp/a.bak > ? y

Type y to confirm each one.

  1. Delete old temp files (list first!).
find /tmp/build -type f -mtime +7 -print; find /tmp/build -type f -mtime +7 -delete

Output:

/tmp/build/x.o
/tmp/build/y.o

Same tests, -print first, then -delete.

  1. Custom output.
find /srv/app -maxdepth 1 -printf '%TY-%Tm-%Td %u %p\n'

Output:

2026-10-25 deploy /srv/app/releases
2026-10-20 deploy /srv/app/shared

Like a report.

  1. Copy found files.
find ~/Photos -name '*.jpg' -newermt '2026-10-01' -exec cp -t /backup/oct/ {} +

Output:

(no output)

cp -t puts the target first so {} + works.

9. xargs: turn a list into arguments

Many commands (rm, gzip, grep) take file names as arguments, not from stdin. With huge lists you also hit 'Argument list too long'.

cmd1 | xargs cmd2 reads items from stdin and runs cmd2 with them, packing as many as fit. -0 reads NUL-separated items (pair with find -print0, safe for spaces and new lines in names). -n N items per run, -P N run N in parallel, -I{} puts each item where {} is (one item per run), -r (GNU) does not run if input is empty, -t prints each command first, -p asks.

OptionUse
-0NUL separated input (with -print0)
-n NN items per command
-P NN commands in parallel
-I{}Place item anywhere
-rSkip if no input
-tShow commands
  1. Delete found files safely.
find /tmp/cache -name '*.tmp' -print0 | xargs -0 -r rm -f

Output:

(no output)

Works even with spaces in names; -r skips when nothing found.

  1. Why -0 matters.
touch 'my file.txt'; find . -name '*.txt' | xargs ls

Output:

ls: cannot access './my': No such file or directory
ls: cannot access 'file.txt': No such file or directory

Without -print0/-0 the space split the name.

  1. Grep inside found files.
find . -name '*.py' -print0 | xargs -0 grep -l 'boto3'

Output:

./sync/main.py

xargs gives grep many files at once.

  1. Parallel compression.
find /data/logs -name '*.log' -print0 | xargs -0 -n 1 -P 4 gzip

Output:

(no output)

4 gzip jobs at the same time on 4 CPUs.

  1. Place each item with -I.
cat servers.txt | xargs -I{} ssh {} 'uptime'

Output:

web1  10:40 up 12 days
web2  10:40 up 3 days

{} becomes each server name.

  1. Show and batch.
seq 1 5 | xargs -t -n 2 echo

Output:

echo 1 2
1 2
echo 3 4
3 4
echo 5
5

-t prints the command, -n 2 groups by two.

  1. Fix 'Argument list too long'.
find /var/spool/mail/old -type f -print0 | xargs -0 rm -f

Output:

(no output)

rm /var/spool/mail/old/* would fail with 200,000 files.

find walks the disk every time. For a quick 'where is that file?' a database is faster.

locate (plocate/mlocate package) searches a database built by updatedb, usually daily. -i ignore case, -c count, -n 5 limit, -e only existing files. New files appear only after updatedb.

  1. Instant name search.
locate -i nginx.conf

Output:

/etc/nginx/nginx.conf
/usr/share/doc/nginx/nginx.conf.default

Very fast, from the database.

  1. Refresh the database.
sudo updatedb && locate -c '*.log'

Output:

213

New files appear only after updatedb (runs daily by timer).

11. which: which program will run

Two versions of python or java are installed. Which one runs when you type the name?

which cmd searches your PATH and prints the first match. -a prints all matches.

  1. Which python runs?
which python3

Output:

/usr/bin/python3

First match in PATH.

  1. All matches.
which -a python3

Output:

/usr/local/bin/python3
/usr/bin/python3

The first one wins.

12. whereis: binary, source and manual

You want to see where a command and its manual page live.

whereis cmd lists binary, source and man page paths from standard places. -b binary only, -m man only.

  1. All places of a command.
whereis tar

Output:

tar: /usr/bin/tar /usr/share/man/man1/tar.1.gz

Binary and manual page.

  1. Binary only.
whereis -b nginx

Output:

nginx: /usr/sbin/nginx

-b hides man pages.

13. type: alias, builtin, function or file

A command behaves oddly. Maybe it is an alias or a shell function, not the real program.

type is a shell builtin. It says if a name is an alias, keyword, function, builtin or a file on disk. type -a shows all, type -t prints just the kind.

  1. Alias or file?
type ll

Output:

ll is aliased to `ls -l --color=auto'

type knows shell aliases; which may not.

  1. Builtin vs file.
type -a echo

Output:

echo is a shell builtin
echo is /usr/bin/echo

The builtin runs first.

14. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

Team and cityHow they use itCommand
Manoj, hosting support, IndoreFrees a full disk by finding files over 1 GBfind / -xdev -type f -size +1G -exec ls -lh {} +
Laura, security audit, MadridLists world-writable and SUID filesfind / -xdev \( -perm -4000 -o -perm -o+w \) -type f
Kwame, media pipeline, AccraConverts thousands of images in parallelfind in/ -name '*.png' -print0 | xargs -0 -P 8 -I{} convert {} {}.webp
Tanvi, data team, PuneDeletes temp exports older than 14 days via cronfind /data/tmp -type f -mtime +14 -delete
Ben, DevOps, SydneyFinds configs changed just before an outagefind /etc -type f -mmin -30
Riya, web agency, JaipurResets web file permissionsfind /var/www -type f -exec chmod 644 {} +

15. Common errors and how to fix them

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

Error you seeWhy it happensHow to fix
find: '/proc/1234': Permission deniedNormal user cannot read some foldersUse sudo, -xdev, or hide with 2>/dev/null.
sudo find / -xdev -name x 2>/dev/null
find: paths must precede expression: 'b.txt'Unquoted *.txt was expanded by the shellQuote the pattern.
find . -name '*.txt'
find: missing argument to `-exec'Forgot \; or + at the endEnd -exec with \; or +.
find . -name '*.log' -exec gzip {} +
-bash: /usr/bin/rm: Argument list too longToo many files from a glob like *Use find -delete or xargs.
find dir -type f -delete
xargs: unmatched single quoteA file name has a quote and you did not use -0Use find -print0 | xargs -0.
find . -print0 | xargs -0 ls -l
rm runs with no files and errors (xargs)Empty input still ran the commandAdd -r (GNU xargs).
find . -name '*.tmp' -print0 | xargs -0 -r rm
-delete removed more than expected-delete was placed before the testsPut -delete last, and dry-run with -print.
find /tmp -name '*.tmp' -print
find -mtime 7 finds nothingExactly 7 means 'between 7 and 8 days'Use +7 for older than or -7 for newer.
find . -mtime +7
locate: can not stat () `/var/lib/plocate/plocate.db'The database is not built yetRun updatedb.
sudo updatedb
-prune does not skip the folderMissing -o ... -print, or the path does not match exactlyUse the -path X -prune -o ... -print form.
find . -path ./node_modules -prune -o -type f -print

16. Troubleshooting step by step

When find returns nothing or too much, build the command one test at a time and always -print before any action.

Go top to bottom. Stop at the first step that shows the problem.

  1. Start broad and check results exist.
find /var/log -type f | head
  1. Add one test at a time.
find /var/log -type f -name '*.log' | head
  1. Check the time maths.
find /var/log -type f -name '*.log' -mtime +7 -printf '%TY-%Tm-%Td %p\n' | head
  1. Check the count before acting.
find /var/log -type f -name '*.log' -mtime +7 | wc -l
  1. Dry-run xargs with echo or -t.
find . -name '*.tmp' -print0 | xargs -0 -r echo rm
  1. Then run the real action.
find . -name '*.tmp' -print0 | xargs -0 -r rm -f

17. Lab 1: Free a full disk safely

Create test files, find big and old ones, compress and delete safely.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Create files of different sizes and ages.
mkdir -p ~/lab12/logs && cd ~/lab12/logs && for i in 1 2 3 4 5; do truncate -s ${i}0M app$i.log; touch -d "$((i*3)) days ago" app$i.log; done; ls -lh

Output:

-rw-r--r-- 1 ravi ravi 10M app1.log ... 50M app5.log
  1. Files bigger than 25 MB.
find . -type f -size +25M -printf '%s %p\n' | sort -nr

Output:

52428800 ./app5.log
41943040 ./app4.log
31457280 ./app3.log
  1. Files older than 7 days.
find . -name '*.log' -mtime +7

Output:

./app3.log
./app4.log
./app5.log
  1. Compress them.
find . -name '*.log' -mtime +7 -exec gzip {} + && ls

Output:

app1.log  app2.log  app3.log.gz  app4.log.gz  app5.log.gz
  1. Delete gz files older than 12 days (print first).
find . -name '*.gz' -mtime +12 -print -delete

Output:

./app5.log.gz

18. Lab 2: Names with spaces and xargs

See how spaces break pipelines and fix them with -print0 and -0; run jobs in parallel.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Make tricky file names.
mkdir -p ~/lab12/docs && cd ~/lab12/docs && touch 'Oct bill.txt' 'Nov bill.txt' plain.txt

Output:

(no output)
  1. Unsafe way.
find . -name '*.txt' | xargs ls 2>&1 | head -2

Output:

ls: cannot access './Oct': No such file or directory
ls: cannot access 'bill.txt': No such file or directory
  1. Safe way.
find . -name '*.txt' -print0 | xargs -0 ls

Output:

./Nov bill.txt  ./Oct bill.txt  ./plain.txt
  1. Parallel jobs with -P and -I.
find . -name '*.txt' -print0 | xargs -0 -P 3 -I{} sh -c 'echo "done: $1"' _ {}

Output:

done: ./plain.txt
done: ./Oct bill.txt
done: ./Nov bill.txt
  1. Fix permissions in one go.
find ~/lab12 -type f -exec chmod 644 {} + && find ~/lab12 -type f ! -perm 644 | wc -l

Output:

0

Ravindra Bagale's Tip

Before any find -delete or xargs rm, run the exact same find with -print (or xargs echo) and check the count with wc -l. Deleting the wrong 10,000 files takes one second; getting them back may be impossible.

19. Frequently asked questions

Q. find or locate?

locate is instant but uses a daily database. find is always current and can test size, time and owner.

Q. Why quote '*.txt'?

Without quotes the shell expands it to file names in the current folder before find sees it.

Q. -exec {} \; or {} +?

\; runs once per file (slow). + passes many files per run (fast). Use + when the command accepts many files.

Q. Why -print0 and xargs -0?

File names can contain spaces, quotes or new lines. NUL can never be in a name, so it is a safe separator.

Q. What does -mtime +7 mean exactly?

Modified more than 7 full days (×24 h) ago. -mtime 7 means between 7 and 8 days.

Q. Why use -xdev?

It stops find from crossing into other disks, network mounts, /proc and /sys.

Q. xargs or a while read loop?

xargs is faster and can run in parallel. A while loop is easier for complex logic per file.

Q. Is find -delete safe?

Yes, if you test the same command with -print first and keep -delete as the last option.

20. Interview questions and answers

Beginner level

Q. How do you find a file named app.conf under /etc?

find /etc -name app.conf.

Q. How do you find only folders?

find PATH -type d.

Q. How do you find files bigger than 100 MB?

find / -xdev -type f -size +100M.

Q. What does xargs do?

It turns lines from stdin into arguments for a command.

Mid level level

Q. Delete .log files older than 30 days.

find /var/log/app -name '*.log' -mtime +30 -delete (print first).

Q. Difference between -exec {} \; and {} +?

\; one process per file; + batches many files into one process, like xargs.

Q. How do you handle spaces in file names with xargs?

find ... -print0 | xargs -0 cmd.

Q. Find files changed in the last 15 minutes.

find /path -mmin -15 -type f.

Senior level

Q. Disk is 100% but find shows no big files. Why?

Deleted files still held open (lsof +L1), files hidden under a mount point, inode exhaustion (df -i), or reserved blocks. Check each.

Q. Process 2 million files with a command, fast and safely.

find -print0 | xargs -0 -n 500 -P $(nproc) cmd; or find -exec cmd {} +; tune -n for memory, avoid one-process-per-file.

Q. How do you audit SUID/SGID and world-writable files across a fleet?

find / -xdev -type f \( -perm -4000 -o -perm -2000 \) and -perm -o+w, ship results via Ansible/SSM, diff against a baseline and alert on new entries.

Q. Why is find . -name '*.tmp' -delete -o -name '*.bak' dangerous if written wrongly?

Operator precedence: AND binds tighter than OR, and -delete placed before tests deletes everything it reaches. Group with \( \) and put -delete last.

Try at home

  1. In your Downloads folder, find all PDF files bigger than 1 MB and older than 30 days, and print them with their size.
  2. Make 3 files with spaces in the names and list them safely with find -print0 | xargs -0 ls -l.

Practise this chapter

Interview questions and answers

Why might a find expression return directories even though it contains -type f?

An ungrouped OR can allow a later name test to match a directory without also requiring the file-type test. I would group the name alternatives and preview the result on a tree with both regular files and a directory ending with the same extension.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. For regular files ending in .txt or .csv, what should be grouped together?

Answer and explanation

A. The two name alternatives

Grouping the alternatives lets the file-type condition apply to both choices.

2. What does -o mean in a find expression?

Answer and explanation

A. Logical OR

The OR makes a match possible when either alternative is true, subject to grouping and other tests.

3. What should you run before adding any file-changing action?

Answer and explanation

C. The same narrow match expression with a print action

A read-only preview shows the intended candidate paths before making changes.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Ahmednagar Sugar Co-op is a sugar co-operative business in Ahilyanagar. Balasaheb Pawar looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 18

Easy Find by name

Lost config file.

Find files named app.conf under /etc.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Find by extension

All logs in one list.

Find all files ending in .log under /opt/app/logs. Quote the pattern.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Ignore case

Invoice or receipt.

Find files under ~/Documents whose name contains receipt in any case. Pattern '*receipt*'.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Only folders

Count project folders.

Find only directories under /srv/app.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Instant name lookup

find is slow on big disks.

Search the file name database for php.ini, ignoring case.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Which program runs

Two pythons installed.

Show the path of the pip3 that runs from your PATH.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Big files

Disk is full.

Find regular files bigger than 500 MB under /var.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Old files

Logs older than a week.

Find files under /var/log/app modified more than 7 days ago.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Recently changed

What changed before the outage?

Find files under /etc/systemd changed in the last 15 minutes.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Files of a user

Before removing an account.

Find files under /home owned by the user sneha.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Top level only

Do not walk deep folders.

Find .log files directly inside /srv/logs only (depth 1). Put -maxdepth first.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Compress found files

Save disk space.

Gzip .log files under /srv/api/logs older than 5 days, batching many files per gzip run with {} +.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Delete old temp files

Cron cleanup.

Delete regular files under /var/tmp/cache older than 10 days using find's own delete action.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Safe delete with xargs

Names may contain spaces.

Find *.bak files in the current folder and remove them with xargs, safely handling spaces (-print0 and -0).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard World-writable files

Security audit.

Find regular files under /srv that are writable by others. Use -perm -o+w.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Skip a folder

vendor is huge.

From the current folder, list *.php files but skip ./vendor using -path ... -prune -o ... -print.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Parallel gzip

Use all CPUs.

Gzip all *.log files under /data, one file per gzip, 4 at a time. Use -print0, xargs -0 -n 1 -P 4.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Run a command per server

Check uptime everywhere.

Read servers.txt with cat and, for each line, run ssh SERVER uptime using xargs -I{}.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. find START tests actions; quote patterns.
  2. -name -iname -type -size -mtime -mmin -user -perm.
  3. -maxdepth, -prune, -xdev, ! and -o.
  4. -exec {} + is fast; -delete goes last; always -print first.
  5. find -print0 | xargs -0 -r -n -P -I.
  6. locate for instant name lookups; which/whereis/type for commands.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

find by anything. Locate files across folders. -name -type -size -mtime -user. Quote patterns; add 2>/dev/null. Big files on root disk.
Revision card: find by anything · Download PNG · Open card page
find + xargs. Act on thousands of files. -exec {} + or xargs batches. -print0 | xargs -0 is space safe. Clean old temp files.
Revision card: find + xargs · Download PNG · Open card page
find -exec vs xargs. Part of find. / Separate command in a pipe.
Comparison: find -exec vs xargs · Download PNG · Open card page
find vs locate. Walks the disk live. / Reads a daily database.
Comparison: find vs locate · Download PNG · Open card page
find says Permission denied?. You cannot read some folders. Use sudo or hide errors. Quiet search.
Interview card: find says Permission denied? · Download PNG · Open card page
Argument list too long?. * expanded to too many files. Let find or xargs batch. Delete safely.
Interview card: Argument list too long? · Download PNG · Open card page
xargs breaks on spaces?. It splits on blanks. Use NUL separators. Safe pipeline.
Interview card: xargs breaks on spaces? · Download PNG · Open card page
Disk full but no big files?. Deleted files held open, or inodes. Check lsof and df -i. Open deleted files.
Interview card: Disk full but no big files? · Download PNG · Open card page

More cards: Ravi Sir's Cards.