The Academy

Loading the search index…

Searches the academy on this device only.

Reading and searching: file, cat, less, head, tail, grep and find

Look inside files without changing them, search their text with grep, find files by name with find, and join commands together with a pipe. Every command here only reads.

30 minelementaryUbuntu/LinuxPractical task needs a laptop or desktop

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

By the end of this lesson you will be able to

  • Say what kind of file something is with file, and read it with cat, less, head and tail.
  • Search the lines of a file with grep, using -i, -n and -c.
  • Find files by name with find and a quoted pattern.
  • Join two commands with a pipe and explain what travels through it.

A great deal of everyday work is reading: checking what a file contains, finding the one line that matters in a long list, or locating a document whose name you half remember. The commands in this lesson do exactly that and nothing more. They never change a file, so they are as safe as ls. They are also the same tools that system administrators use to read long logs, which makes them worth learning early.

A sample file to read

First we need something to read. printf prints text exactly as you write it, and \n means 'start a new line'. Together with > it writes ten fictional lines into a new file called visitors.txt. Check that you are in the practice folder first. If the file already exists, it will be replaced, which is fine for a sample file.

Terminal (Linux) · you can run this on your own computer
cd ~/practice
printf "Amina Nairobi 12-Oct\nJoseph Mombasa 12-Oct\nWanjiru Nakuru 13-Oct\nKamau Nairobi 13-Oct\nFatuma Kisumu 14-Oct\nAmina Nairobi 15-Oct\nJoseph Mombasa 15-Oct\nWanjiru Nakuru 16-Oct\nKamau Nairobi 17-Oct\nFatuma Kisumu 17-Oct\n" > visitors.txt

What is it, and what does it say? file and cat

file looks at the contents of something and tells you what kind it is. A file name or extension can lie, but the contents do not. cat (short for concatenate) prints a whole file on the screen. Use it only on small text files: printing a huge or non-text file floods the terminal with rubbish.

Terminal (Linux) · you can run this on your own computer
file visitors.txt projects
cat visitors.txt
Simulated interface: not a real screenshot

file names the kind of thing; cat prints a small text file (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: file reports the type from the contents
  2. Marker 2: cat prints the whole file
file names the kind of thing; cat prints a small text file (simulated).
Read this figure as text

A simulated terminal showing the file command describing a text file and a directory, then cat printing ten visitor lines.

A simulated bash terminal in ~/practice. Marker 1: file visitors.txt projects prints visitors.txt: ASCII text, and projects: directory. Marker 2: cat visitors.txt prints the ten lines: Amina Nairobi 12-Oct, Joseph Mombasa 12-Oct, Wanjiru Nakuru 13-Oct, Kamau Nairobi 13-Oct, Fatuma Kisumu 14-Oct, Amina Nairobi 15-Oct, Joseph Mombasa 15-Oct, Wanjiru Nakuru 16-Oct, Kamau Nairobi 17-Oct and Fatuma Kisumu 17-Oct.

Long files: less, head and tail

For a file that does not fit on one screen, less shows one screenful at a time. Press Space for the next page, the arrow keys to scroll a line, type / followed by a word to search for it, and q to quit. If you forget how to leave, press q: it is the way out of less. head shows only the first lines of a file and tail only the last lines. By default each shows 10 lines; the -n option sets how many.

Terminal (Linux) · you can run this on your own computer
head -n 3 visitors.txt
tail -n 2 visitors.txt
less visitors.txt
Commands for reading files
CommandBest forRemember
file nameFinding out what a file really isReads the contents, not the extension
cat nameSmall text filesPrints everything at once
less nameLong filesSpace = next page, /word = search, q = quit
head -n 3 nameThe beginning of a fileDefault is 10 lines
tail -n 3 nameThe end, such as the latest log entriesDefault is 10 lines

Searching inside files: grep

grep prints only the lines of a file that contain the text you give it. The first argument is the pattern to look for and the second is the file. Capital letters matter unless you add -i. Adding -n shows line numbers and -c prints only a count of matching lines.

Terminal (Linux) · you can run this on your own computer
grep Nairobi visitors.txt
grep -n Amina visitors.txt
grep -c Nairobi visitors.txt
grep -i kisumu visitors.txt
grep Eldoret visitors.txt
Simulated interface: not a real screenshot

grep prints matching lines, with options for numbers and counts (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: Only lines containing Nairobi
  2. Marker 2: -n adds line numbers
  3. Marker 3: -c counts matching lines
  4. Marker 4: -i ignores capital letters
  5. Marker 5: No match: no output, and no error
grep prints matching lines, with options for numbers and counts (simulated).
Read this figure as text

A simulated terminal showing grep finding four Nairobi lines, numbered Amina lines, a count, a case-insensitive search and a search with no result.

A simulated bash terminal in ~/practice. Marker 1: grep Nairobi visitors.txt prints four lines: Amina Nairobi 12-Oct, Kamau Nairobi 13-Oct, Amina Nairobi 15-Oct and Kamau Nairobi 17-Oct. Marker 2: grep -n Amina visitors.txt prints 1:Amina Nairobi 12-Oct and 6:Amina Nairobi 15-Oct, where the number is the line number. Marker 3: grep -c Nairobi visitors.txt prints 4. Marker 4: grep -i kisumu prints the two Fatuma Kisumu lines, matching despite the lower case k. Marker 5: grep Eldoret visitors.txt prints nothing, because no line matches.

Joining commands with a pipe

A Pipe is the vertical bar | (usually on the key left of Z or beside Enter, depending on your keyboard). It takes the output of the command on its left and hands it to the command on its right as input, like water flowing from one section of a pipe into the next. Nothing is saved to a file: the text just flows through.

Terminal (Linux) · you can run this on your own computer
grep Nairobi visitors.txt | head -n 2
grep Nairobi visitors.txt | wc -l

The first line searches for Nairobi and shows only the first two matches. The second hands the matches to wc -l (word count, lines), which counts lines and prints 4. You get the same count as grep -c, but now you can see how a pipe builds a bigger answer from small tools.

Diagram

grep Nairobi visitors.txt | head -n 2: text flows from left to right.

  1. Step 1: visitors.txt10 lines
  2. Step 2: grep Nairobikeeps 4 lines
  3. Step 3: head -n 2keeps the first 2
grep Nairobi visitors.txt | head -n 2: text flows from left to right.
Read this figure as text

A flow of three boxes: the file visitors.txt, grep Nairobi, and head -n 2, joined by arrows labelled with what passes through.

Three steps joined by arrows. 1: the file visitors.txt holds ten lines. 2: grep Nairobi keeps only the four lines containing Nairobi. 3: head -n 2 keeps only the first two of those, which are Amina Nairobi 12-Oct and Kamau Nairobi 13-Oct. The vertical bar between grep and head is the pipe, and the output of one command becomes the input of the next.

Finding files by name: find

grep searches inside files; find searches for files by name, size or type. Give it a starting folder and a test. -name matches the file name. Put the pattern in quotes so the shell does not expand the Wildcard * (which stands for 'anything') before find sees it. A single dot . means 'the working directory'.

Terminal (Linux) · you can run this on your own computer
find . -name "*.txt"
find . -type d
Simulated interface: not a real screenshot

find lists every match below the starting folder (simulated).

amina@laptop: ~/practice (simulated)
  1. Marker 1: Files whose name ends in .txt, at any depth
  2. Marker 2: -type d means directories only
find lists every match below the starting folder (simulated).
Read this figure as text

A simulated terminal where find lists all .txt files below the practice folder and then all directories.

A simulated bash terminal in ~/practice. Marker 1: find . -name "*.txt" prints ./visitors.txt, ./first.txt and ./projects/notes.txt, each path starting with a dot meaning this folder. Marker 2: find . -type d prints ., ./projects and ./projects-copy, the folders. The order is not alphabetical and will differ on your computer, as will the names, depending on what you made earlier.

Common mistakes

  • ✖ Instead of: Running cat on a program or a very large file.

    ✔ Do this: Use file first, then less or head. cat on a non-text file prints unreadable symbols that can mess up the terminal.

  • ✖ Instead of: Forgetting the quotes round *.txt in find.

    ✔ Do this: Write find . -name "*.txt". Without quotes the shell may change the pattern before find sees it.

  • ✖ Instead of: Getting stuck inside less and closing the window.

    ✔ Do this: Press q. less is a viewer, and q always quits it.

If something goes wrong

grep: visitors.txt: No such file or directory
You are in the wrong folder or the file was never made. Run pwd, cd ~/practice and ls, then create the file again with the printf command.
grep prints nothing but I know the word is there.
Check capital letters (use -i), check spelling and check that you gave the right file name. Remember that grep matches text inside a line, so extra spaces in your pattern also stop a match.
find prints lots of 'Permission denied' lines.
You started the search somewhere large, such as /. Press Ctrl+C to stop it and run find again from . inside ~/practice.

Key terms

grep
A command that searches inside text files for lines that match a word or pattern.
Pipe
The | symbol, which passes the output of one command to another as its input.
Wildcard
A special character such as * that stands for any characters in a file name.

Practise it for real

Reading is not the same as being able to do it. Complete these on your own computer, then tick only what you have actually done. These ticks are your own honest record: nothing checks them for you.

Guided exerciseNeeds a laptop or desktop

Read and search the visitors file

In ~/practice create visitors.txt with the printf command from this lesson. Run file visitors.txt, head -n 3 visitors.txt, tail -n 3 visitors.txt and less visitors.txt (leave with q). Then use grep to list the lines for Joseph, show Wanjiru's lines with line numbers, and count the Kisumu lines. Finish with find . -name "*.txt".

You have succeeded when:
Independent challengeNeeds a laptop or desktop

Challenge: answer a question with a pipe

Without copying a ready-made command, use one pipe to show only the last two lines that mention Mombasa. Then change it to count how many lines mention Nakuru. Write down both commands.

You have succeeded when:

Knowledge check

7 questions. You need 80% to pass. Mistakes here are part of learning, and you can retry as often as you like.

  1. Question 1.You want to read a file with 5,000 lines, one page at a time. Which command is best?
  2. Question 2.When grep Eldoret visitors.txt prints nothing, the command has failed.
  3. Question 3.Match each command to its job.
  4. Question 4.You have a long file called log.txt and you only want to know how many lines contain the word error.Which command gives that?
  5. Question 5.Put these actions in order to read a long file with less and leave it.
    1. Press Space to move to the next page
    2. Type /Nairobi and press Enter to search
    3. Press q to quit
    4. Type less visitors.txt and press Enter
  6. Question 6.Why is the pattern written as "*.txt" in quotes in find . -name "*.txt"?
  7. Question 7.Which of these commands changes your files?

Summary

  • file tells you what a file really is; cat prints small text files; less pages through long ones.
  • head and tail show the start and the end of a file, 10 lines by default, or -n lines.
  • grep prints the lines that contain your text; -i ignores case, -n numbers lines, -c counts.
  • A pipe | sends the output of one command into the next.
  • find searches for files by name below a folder; quote the pattern and keep it inside ~/practice.

Checklist: can you do these?

Marking a lesson as read does not prove you can do it. Skills count as demonstrated when you pass the quiz (not yet) and complete the practical tasks (not yet).

Related lessons

References and further reading

Official sources for details that can change between versions.

On this page