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

Linux grep and find Commands with Examples

A server has thousands of files and log files with millions of lines. You can't open them one by one. In this chapter we learn the two commands every Linux admin and DevOps engineer uses daily: grep finds text inside files, and find finds the files themselves. We practise on the files that list the users of a Linux server, /etc/passwd and /etc/shadow, so first we see how Linux numbers its users.

What you'll learn in this class

  • Linux user types: root (UID 0), system users and normal users, and what a sudo user is
  • /etc/passwd and /etc/shadow: what each line means and who can read them
  • grep: find text in a file without opening it; search inside vi with /, n and N
  • The pipe |: send one command's output into the next command
  • grep options -i, -r and -n, and the "Is a directory" message
  • find: by name, with wildcards, by type and by permission; sudo and Permission denied
  • find | grep and wc -l, and when -name is the better choice
  • Why admins grep logs: who logged in to the server

1. Quick revision: networking

Why: grep and find are mostly used on servers to answer networking and access questions from logs: which IP connected, which port failed. You need the basics fresh to read those lines.

Classroom line

As long as you're in DevOps or cloud, never forget networking and IP.

Quick revision

  1. A security group is a firewall around one EC2 instance. If only port 80 is open, visitors reach your app only through the web server on port 80.
  2. A private IP works only inside its private network (an office, or your VPC). To be reached from the internet, a server needs a public IP.
  3. A private network inside one company is an intranet; the internet connects networks worldwide.
  4. Private ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Every IP has a network part and a host part; the subnet mask shows where one ends.
  5. 0.0.0.0 means "any address". 127.0.0.1 is localhost, the machine itself (the whole 127.0.0.0/8 range is loopback).

Need more? Read Networking Basics for Cloud: IP Address, Client and Server Explained and Public vs Private IP Address Explained.

2. Linux user types and UIDs

Why: Linux decides what you may do by who you are. Every user has a number, the UID (user ID), and Linux checks that number, not the name.

What: there are three kinds of users.

  1. root, UID 0. The superuser. It can read, change and delete everything.
  2. System users, UID 1 to 999. Linux and installed software create them to run services, for example sshd or nginx. Most can't log in.
  3. Normal users, UID 1000 and up. These are people. The first user on an EC2 instance (ec2-user on Amazon Linux, ubuntu on Ubuntu) is 1000; the next user you create gets 1001.

A sudo user is not a fourth type. It is a normal user who is allowed to run commands as root, through the wheel group on Amazon Linux or the sudo group on Ubuntu.

Classroom line

(Root to a sudo user) Do your work yourself, don't come to me.

How: check your own user.

  1. Run id.
  2. Read uid=1000(ec2-user): your UID and name.
  3. Read groups=...: wheel (or sudo on Ubuntu) means you can use sudo.
  4. Run sudo id. Now it shows uid=0(root), because sudo runs the command as root.
Who is who: Linux user types by UID root UID 0 The superuser: can do everything on the server. System users UID 1-999 Run services such as sshd, nginx. Usually cannot log in. Normal users UID 1000+ People: ec2-user or ubuntu is 1000, the next user 1001. A sudo user is a normal user with permission to run commands as root (group wheel onAmazon Linux, sudo on Ubuntu). Same UID range. Check yourself: id shows your UID, groups and name.

Figure 1. Linux user types by UID: root is 0, system users 1 to 999, normal users from 1000. A sudo user is a normal user allowed to act as root.

3. /etc/passwd and /etc/shadow

Why: when you need to know which users exist on a server, their UID, home folder or shell, it's all in one file.

What:

  1. /etc/passwd is the list of users. One line = one user. Everyone can read it.
  2. /etc/shadow holds the password hashes. Only root can read it.
  3. A hash can't be turned back into the password. A forgotten password is never "read back"; it is reset with sudo passwd USERNAME.

A line of /etc/passwd has seven fields separated by :

ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash
  1. ec2-user: the user name.
  2. x: the password is kept in /etc/shadow.
  3. 1000: the UID.
  4. 1000: the group ID.
  5. EC2 Default User: a comment.
  6. /home/ec2-user: the home folder.
  7. /bin/bash: the login shell. /sbin/nologin here means the account can't log in, which is normal for system users.

How:

  1. Run cat /etc/passwd. Every user is listed.
  2. Run cat /etc/shadow. You get Permission denied: a normal user can't read it.
  3. Run sudo cat /etc/shadow. As root it works.
One line of /etc/passwd = one user ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash ec2-user 1. user name x 2. password is in /etc/shadow 1000 3. UID 1000 4. group ID EC2 Default User 5. comment /home/ec2-user 6. home folder /bin/bash 7. login shell /etc/passwd: everyone can read it Names, UIDs, home folders, shells. /etc/shadow: only root can read it Password hashes. A hash cannot be read back. Shell /sbin/nologin = this account cannot log in. Forgot apassword? Reset it: sudo passwd USERNAME.

Figure 2. One line of /etc/passwd: user name, x, UID, group ID, comment, home folder and shell. /etc/shadow holds the password hashes and only root can read it.

4. grep: find text without opening the file

Why: a log file can have millions of lines. Opening it in an editor is slow and can even freeze the terminal, and scrolling up looking for one error wastes your time.

Classroom line

(Scrolling up a giant log looking for the error) Go up, where is it, go up... That's why we use grep.

What: grep searches a file for a word (a pattern) and prints only the lines that contain it. The file is never opened in an editor.

Classroom line

It's grep, not grapes!

Classroom line

This is also an interview question: how do you find a pattern without opening the file?

How: make a practice file, search it in vi, then with grep.

  1. Connect to your EC2 instance with SSH.
  2. Run vi demo (vim demo works the same).
  3. Press i and paste a few lines of text about Linux.
  4. Press Esc, type :wq and press Enter.
  5. Open it again with vi demo.
  6. Type /Linux and press Enter. The cursor jumps to the first match.
  7. Press n for the next match and N for the previous one.
  8. Type :q to leave the editor.
  9. Now run grep "Linux" demo. Only the lines with Linux are printed, and the word is highlighted.
Terminal: the command grep "Linux" demo, followed by three lines of text about package managers in which each Linux is highlighted in red, then the prompt again.

Whiteboard: grep "Linux" demo prints only the lines that contain Linux, with the word highlighted.

More examples:

  1. grep ec2-user /etc/passwd prints your own user line.
  2. grep nologin /etc/passwd lists the accounts that can't log in.
  3. sudo grep -i "error" /var/log/httpd/error_log shows the errors in the Apache log (on Amazon Linux).

Classroom line

As a DevOps engineer, reading logs is your job.

5. The pipe: one command's output into the next

Why: some commands print a lot. Instead of reading all of it, you can pass it straight to grep and keep only what you need.

What: the pipe sign | takes everything the left command prints and gives it to the right command as its input.

How:

  1. Run cat /etc/passwd. Every user line scrolls past.
  2. Run cat /etc/passwd | grep ec2-user. Only your line is left.
  3. Run grep ec2-user /etc/passwd. Same result: grep can also read the file itself.
  4. Run ls /etc | grep conf. Only the names in /etc that contain conf.

Classroom line

I'll put this into a pipe, and put grep after it.

The pipe: one command output becomes the nextcommand input $ cat /etc/passwd | grep ec2-user 1. Two commands joined by a pipe. 2. cat prints every line of /etc/passwd ... root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash nginx:x:991:991:Nginx webserver:/var/lib/nginx:/sbin/nologin | grep ec2-user keeps matching lines 3. ... but the pipe hands those lines to grep instead of thescreen. ec2-user:x:1000:1000:EC2 DefaultUser:/home/ec2-user:/bin/bash 4. grep shows only the matching line. Same as: grep ec2-user/etc/passwd You type one line: two commands joined by the

Figure 3. Animation: cat /etc/passwd prints every line, the pipe hands the lines to grep, and grep keeps only the line with ec2-user.

6. grep options: -i, -r and -n

Why: you don't always know the exact spelling, or which file has the word.

Classroom line

I don't know which file has the word, but I want to find it. How?

What:

  1. Case matters. Linux and linux are different words for grep.
  2. -i ignores case: Linux, linux and LINUX all match.
  3. -r (recursive) searches every file in a folder and in the folders inside it.
  4. -n adds the line number in front of every match.
  5. Options can be joined: -irn = ignore case + recursive + line numbers.

Classroom line

In Linux, everything is case-sensitive.

How:

  1. grep -r "Linux" ~/ searches every file in your home folder. Each line starts with the file name.
  2. grep -r "linux" ~/ finds other lines: case matters.
  3. grep -i "linux" ~/ stops with Is a directory: grep reads files, and a folder is not a file.
  4. grep -ir "linux" ~/ works: -r lets grep go into the folder.
  5. grep -irn "linux" ~/ also shows the line numbers, as file:line:text.
  6. man grep opens the full manual (press q to quit). grep --help prints a short list of options.
Terminal: grep -r "Linux" /home/ubuntu/ prints matching lines from .viminfo and demo, each starting with the file name in purple; grep -r "linux" prints different lines from .bash_history; two grep -i "linux" /home/ubuntu/ commands each answer grep: /home/ubuntu/: Is a directory.

Whiteboard: grep -r searches every file in the folder; "Linux" and "linux" find different lines; without -r, grep on a folder says Is a directory.

Terminal: grep -irn "linux" /home/ubuntu/ prints matches from .viminfo, .bash_history and demo; each line starts with the file name in purple, a green line number such as 14 or 1, then the text with Linux highlighted.

Whiteboard: grep -irn: ignore case, every file in the folder, and the line number after each file name.

Classroom line

The best way: make a mistake, read the error, understand it, and try again.

grep options you will use every day grep "Linux" demo Lines with Linux in the file demo. Exact case only. grep -i "linux" demo Ignore case: Linux, linux, LINUX. -i = ignore case grep "linux" ~/ Error: Is a directory grep reads files, not folders. grep -r "Linux" ~/ Every file in the folder and below. -r = recursive grep -irn "linux" ~/ Ignore case, recursive, with line numbers. file:line:text Help: man grep (full manual, q to quit) or grep --help (short list).

Figure 4. grep options: exact case by default, -i to ignore case, -r for a whole folder, -n for line numbers. grep on a folder without -r says Is a directory.

One more listing trick: ls -R lists a folder and every folder inside it, like a tree. Try ls -R ~/.

7. find: find the file itself

Why: grep needs to know where to look. When you don't know where a file is, you need find.

What: find walks through a folder and every folder below it and prints the path of each match.

find WHERE -type f|d|l -name "NAME"
  1. WHERE: the folder to start from: . (here), ~/, /home or / (the whole server). find always needs a starting folder; it doesn't read a list from a pipe.
  2. -type f = files, -type d = folders (directories), -type l = links.
  3. -name "NAME": the exact name. * is a wildcard: "*.log" = every name ending in .log.
  4. -perm 644: files with exactly that permission.

Classroom line

It goes into every file and searches.

How:

  1. find . -name "demo" finds demo in the current folder and below.
  2. find /home -name "demo" starts from /home.
  3. find . -name "*.log" lists all .log files here and below.
  4. find / -name "passwd" from the whole server, as a normal user, prints a flood of Permission denied lines for folders you may not read.
  5. sudo find / -name "passwd" reads every folder and lists every match.
  6. find / -name "passwd" 2>/dev/null is another way: it hides the error lines and keeps the results.
  7. sudo find / -type f -name "passwd" lists files only; a folder called passwd is skipped.
  8. sudo find / -type d -name "nginx" lists folders only.
  9. sudo find /var/www -perm 644 lists files with permission 644.

If you forget -name before the name, find stops with paths must precede expression. Add -name and run it again.

Terminal: a few lines find: './var/log/amazon': Permission denied and similar from an earlier run, then the command sudo find . -name "passwd" run from the top folder, followed by a list of paths ending in passwd such as ./snap/core22/2437/etc/pam.d/passwd, ./snap/core22/2437/usr/bin/passwd and ./etc/pam.d/passwd.

Whiteboard: Without sudo, find prints Permission denied for folders it can't read. With sudo it lists every passwd file with its full path.

Reading a find command, left to right sudo find / -type f -name "passwd" sudo read every folder find the command / where to start -type f files only (d = folders) -name "passwd" exact name, wildcards allowed More examples find . -name "*.log" # all .log files here and below sudo find /var/www -type d # only folders sudo find /var/www -perm 644 # files with permission644 find always needs a starting folder. Forgot -name before thename? find stops with: paths must precede expression.

Figure 5. Reading sudo find / -type f -name "passwd" from left to right, with more examples for wildcards, folders and permissions.

How find searches: folder by folder $ find / -name passwd / → etc/ root/ usr/ home/ … 1. find starts at / and opens every folder below it. find: '/root': Permission denied find: '/proc/1/map_files': Permission denied 2. As a normal user, folders you cannot read print Permissiondenied. $ sudo find / -name passwd 3. With sudo, find can read every folder and prints each match. /etc/passwd /etc/pam.d/passwd /usr/bin/passwd ... every match, in every folder 4. Full paths of every file named passwd.

Figure 6. Animation: find opens every folder from the starting point. Without sudo some folders print Permission denied; with sudo every match is listed.

8. find | grep, and counting with wc -l

Why: sometimes you want every path that has a word anywhere in it, or you just want to know how many there are.

What:

  1. find prints full paths. grep then keeps the lines (paths) that contain the word.
  2. grep matches the word anywhere in the path, folder names included.
  3. wc -l counts lines.

How:

  1. sudo find / -type f | grep "passwd" lists every file whose path contains passwd.
  2. Look closer: a file such as /usr/share/doc/passwd/copyright is listed only because a folder in its path is called passwd.
  3. For files whose own name contains passwd, use find alone: sudo find / -type f -name "*passwd*".
  4. Add | wc -l to count: sudo find / -type f -name "*passwd*" | wc -l.
find | grep matches the whole path; -namematches only the name sudo find / -type f | grep passwd /etc/passwd /etc/pam.d/passwd /usr/bin/passwd /usr/share/doc/passwd/copyright The last file is called copyright: it matched because a folder in its path is called passwd. Add | wc -l to count the lines. sudo find / -type f -name "*passwd*" /etc/passwd /etc/pam.d/passwd /usr/bin/passwd Only files whose own name contains passwd. Drop the stars for the exact name. Use grep to filter find output by any word in the path;use -name when only the file name matters.

Figure 7. find | grep matches the word anywhere in the path; find -name matches only the file name. Add | wc -l to count.

9. Why admins grep logs

Why: Logs are the server's diary. When a site goes down or someone tries to break in, grep turns millions of lines into the five that matter.

On your own servers (on-premises, in a company data centre) or on EC2, someone has to answer questions like "who logged in last night?" and "did anyone try to break in?". The answers are in log files, and grep finds them in seconds.

  1. On Amazon Linux: sudo journalctl -u sshd | grep "Accepted" shows successful SSH logins.
  2. sudo journalctl -u sshd | grep -E "Failed|Invalid" shows failed attempts.
  3. On Ubuntu the same lines are also in /var/log/auth.log: sudo grep "Accepted" /var/log/auth.log.
Who logged in? Ask grep, not your eyes $ sudo journalctl -u sshd | grep -E"Accepted|Failed|Invalid" sshd: Accepted publickey for ec2-user fromCLIENT_IP port 50412 ssh2 sshd: Failed password for invalid user admin fromCLIENT_IP port 41022 sshd: Invalid user test from CLIENT_IP port 39816 sshd: Accepted publickey for ec2-user fromCLIENT_IP port 50630 ssh2 Accepted A successful login: who, and from where. Failed, Invalid user Someone tried and failed: watch these. Ubuntu also writes these lines to /var/log/auth.log: sudo grep"Accepted" /var/log/auth.log

Figure 8. Who logged in? grep the SSH log for Accepted (successful logins) and Failed or Invalid user (failed attempts).

Linux is a multi-user system: many people can be logged in over SSH at the same time, each doing their own work. Run who to see who is logged in right now.

10. The whole flow on one page

  1. id shows your UID: root 0, system users 1 to 999, normal users 1000+.
  2. cat /etc/passwd lists users; sudo cat /etc/shadow shows hashes (root only); reset passwords with sudo passwd USERNAME.
  3. grep "word" file prints matching lines; in vi, /word, n, N.
  4. command | grep word filters another command's output.
  5. -i ignore case, -r whole folder, -n line numbers; Is a directory means add -r.
  6. find WHERE -type f -name "NAME"; sudo for the whole server, or 2>/dev/null.
  7. find ... | grep word matches anywhere in the path; -name matches the name only; | wc -l counts.
  8. journalctl -u sshd | grep "Accepted" (Amazon Linux) or grep "Accepted" /var/log/auth.log (Ubuntu) shows who logged in.

11. Hands-on lab: find users, text and files from the terminal

Why this lab. On a server you can't scroll through every file. This lab answers real questions (who am I, which users exist, where is a file, which file mentions a word) with grep, find and the pipe.

  1. Check your UID
    • Command: id
    • You should see: A line starting with uid=1000(ec2-user) gid=1000(ec2-user), with wheel among the groups.
    • Check: id root → uid=0(root) gid=0(root) groups=0(root)
    • Why: Linux decides what you may do by your number: 0 is root, 1 to 999 are system users, 1000 and up are people. wheel is why sudo works for you.
  2. Find your line in /etc/passwd
    • Command: grep ec2-user /etc/passwd
    • You should see: ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash
    • Check: Seven fields separated by :; the last is the shell.
    • Why: grep prints only matching lines, so you get one line instead of the whole file.
  3. Try to read /etc/shadow
    • Command: cat /etc/shadow
    • You should see: cat: /etc/shadow: Permission denied
    • Check: sudo grep ec2-user /etc/shadow prints one line starting with ec2-user:.
    • Why: Password hashes live in /etc/shadow, readable only by root. Everyone can read /etc/passwd because it holds no passwords.
  4. Ignore case and number the lines
    • Command: grep -in ROOT /etc/passwd
    • You should see: Lines such as 1:root:x:0:0:root:/root:/bin/bash, each with its line number.
    • Check: Without -i, grep ROOT /etc/passwd prints nothing.
    • Why: -i helps when you don't know the exact spelling; -n tells you which line to open in an editor.
  5. Search a whole folder
    • Command: mkdir -p ~/lab/logs && echo "error disk full" > ~/lab/logs/a.log && echo "all good" > ~/lab/logs/b.log && grep -r error ~/lab
    • You should see: /home/ec2-user/lab/logs/a.log:error disk full
    • Check: Only a.log is listed.
    • Why: -r looks inside every file in every subfolder, and prints the file name in front of each match.
  6. Count with a pipe
    • Command: grep -c bash /etc/passwd
    • You should see: A small number, such as 2 (root and ec2-user).
    • Check: grep bash /etc/passwd | wc -l prints the same number.
    • Why: The pipe | sends one command's output into the next. Here both ways count the users who can log in with bash.
  7. Find a file by name
    • Command: sudo find / -type f -name passwd
    • You should see: Paths such as /etc/passwd, /etc/pam.d/passwd and /usr/bin/passwd.
    • Check: Each line is a full path ending in exactly passwd.
    • Why: find searches for the file itself, starting at /. sudo avoids hundreds of Permission denied lines; -type f skips folders.
  8. Find by pattern, then filter
    • Command: find ~/lab -name "*.log"
    • You should see: /home/ec2-user/lab/logs/a.log and /home/ec2-user/lab/logs/b.log.
    • Check: find ~/lab -name "*.log" | grep a.log keeps only the first.
    • Why: Quotes stop the shell from expanding *.log before find sees it. Piping into grep filters the paths by any word.
  9. Clean up
    • Command: rm -r ~/lab
    • You should see: Nothing.
    • Check: ls ~ no longer shows lab. Stop the instance.
    • Why: Practice files left on a server confuse the next search you run.

12. Try at home

Try at home

Task 1: users

  1. Run id and sudo id and compare the UIDs.
  2. Run grep ec2-user /etc/passwd and name all seven fields.
  3. Run grep nologin /etc/passwd | wc -l to count the accounts that can't log in.
  4. Try cat /etc/shadow, then sudo cat /etc/shadow.

Task 2: grep

  1. Create demo with a few lines that contain Linux and linux.
  2. Run grep "Linux" demo, then grep -i "linux" demo, and compare.
  3. Run grep -i "linux" ~/, read the message, then fix it with -r.
  4. Add -n and find the line numbers.

Task 3: find

  1. find ~/ -name "demo".
  2. find / -name "passwd", then the same with sudo.
  3. sudo find / -type f -name "*passwd*" | wc -l.
  4. sudo find / -type f | grep "passwd" | wc -l, and explain why the count is different.

Recap

In short

  1. root is UID 0, system users 1 to 999, normal users 1000 and up; a sudo user is a normal user allowed to act as root.
  2. /etc/passwd lists users (seven fields); /etc/shadow holds password hashes, root only; forgotten passwords are reset, never read.
  3. grep prints only matching lines without opening the file; in vi, /word, n and N.
  4. The pipe | sends one command's output into the next.
  5. grep is case-sensitive; -i, -r and -n fix that, search folders and add line numbers. A folder without -r gives Is a directory.
  6. find WHERE -type f -name "NAME" finds files by name, type or permission; use sudo (or 2>/dev/null) for the whole server.
  7. find | grep matches anywhere in the path, -name matches the name only, and wc -l counts.
  8. Admins grep the logs to see who logged in.

Practice Tasks

About this scenario

Tokyo Ramen House is a restaurant business in Tokyo. Yuki Tanaka looks after its IT and cloud work. They count money in JPY and run their AWS resources in the ap-northeast-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 / 10

Easy Check memory

Why: If RAM is full the server slows down or kills apps.

What to do: Show used and free memory in human-readable units.

How:
  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 errors in a log

Why: grep shows only the lines that contain a word, so you find problems fast.

What to do: Show every line containing ERROR in /var/log/tokyoramen/app.log.

How:
  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 Count matching lines

Why: A count tells you quickly how often something happened today.

What to do: Count how many lines in /var/log/nginx/access.log contain " 404 ".

How:
  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 Case-insensitive search with line numbers

Why: Logs mix "Timeout" and "TIMEOUT". Line numbers help you open the exact spot.

What to do: Search for timeout in /var/log/tokyoramen/api.log, ignoring upper/lower case, and show line numbers.

How:
  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 Find files by name

Why: find searches folder trees for files that match a pattern.

What to do: Find all files ending in .log under /var/log/tokyoramen.

How:
  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 Permission number

Why: Key files must be private.

What to do: What chmod number gives the owner read+write and nobody else anything?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Find big files

Why: When the disk is full, find large files first.

What to do: Find files bigger than 100 MB anywhere under /var.

How:
  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 Search a whole folder

Why: Settings can hide in any file. A recursive search checks them all.

What to do: Find every line that mentions DB_HOST in all files under /etc/tokyoramen, with line numbers.

How:
  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 Find old files

Why: Old files fill the disk. -mtime +N finds files changed more than N days ago.

What to do: Find regular files under /backup/tokyoramen that were changed more than 42 days ago.

How:
  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 Find a running process

Why: A pipe sends the full process list into grep so you see only what matters.

What to do: List all processes and show only the lines that contain node.

How:
  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.

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.

Revision card: Find text with grep. Logs have thousands of lines. Reading them all is impossible. grep prints only lines that contain a word. -i ignores case, -n shows line numbers, -r searches folders. grep WORD FILE, with sudo for protected logs. Who logged in:
Revision card: Find text with grep · Download PNG
Revision card: The pipe |. One command rarely gives exactly what you need. | sends the output of one command as input to the next. Chain small tools: list, filter, count. Count users with bash:
Revision card: The pipe | · Download PNG
Revision card: Find files with find. You know a file name but not where it is. find searches folders by name, type or permission. find WHERE -name PATTERN; add sudo to avoid Permission denied. All conf files:
Revision card: Find files with find · Download PNG
Revision card: /etc/passwd and /etc/shadow. You must know where Linux keeps users and passwords. /etc/passwd lists users (anyone can read). /etc/shadow holds password hashes (root only). Read passwd with cat; shadow needs sudo. A passwd line:
Revision card: /etc/passwd and /etc/shadow · Download PNG
Comparison: grep vs find. grep: Searches for text inside files. find: Searches for files by name, type or permission.
Comparison: grep vs find · Download PNG
Interview card: What does the pipe | do? Give an example.. It sends one command's output into the next command. Small tools combine into powerful one-liners. Count failed logins:
Interview card: What does the pipe | do? Give an example. · Download PNG
Interview card: find vs grep: what does each search?. find searches file NAMES and attributes. grep searches TEXT inside files. One finds where a file is; the other finds what is in it. find /etc -name "*.conf" vs grep -rn "listen" /etc/nginx.
Interview card: find vs grep: what does each search? · Download PNG
Interview card: Why can normal users read /etc/passwd but not /etc/shadow?. passwd has public info; shadow has password hashes. Hashes could be cracked offline if anyone could read them. ls -l /etc/shadow shows ---------- root.
Interview card: Why can normal users read /etc/passwd but not /etc/shadow? · Download PNG

More cards: Ravi Sir's Cards.