LINUX
Linux terminal commands, part 1: navigate, search and read files
ls, pwd, cd, find, grep, cat, less and wc. Revisiting the January 2024 guide, whose sample outputs didn't match the text. This version uses real outputs.
In January 2024 I published the first part of a Linux terminal commands guide, with the commands for finding your
way around, searching for files and reading what’s inside them. Rereading it, the sample outputs didn’t match the
explanations. ls -a showed an ellipsis instead of ..; grep showed a single line while the text said “two
lines”; the user’s directory appeared as /user/user_name, when it’s /home/user_name.
For this version, I set up a sample folder and ran each command in it. The outputs below are what the terminal
returned. The sample file is in Portuguese, the language this guide was first written in; that matters further down,
in wc. Part 2 continues with the commands that create, move and delete files.
The sample folder is the user’s directory, with three folders, two hidden files and a few files inside the folders.
One of them, Documentos/notas.txt, has four lines:
Linux é uma família de sistemas operacionais de código aberto.
Ele usa o kernel criado por Linus Torvalds em 1991.
Eu uso Linux para programar.
É rápido e dá para configurar quase tudo.
Where am I: pwd
$ pwd
/home/ana
pwd shows the current directory. When you open the terminal, it’s usually the user’s directory, under
/home/<user>.
What’s here: ls
$ ls
Documentos Downloads Musica
With -a, hidden items show up too, the ones starting with a dot:
$ ls -a
. .. .bashrc .profile Documentos Downloads Musica
. is the current directory itself and .. is the one above it. .bashrc and .profile are shell configuration
files.
For details such as permissions, owner, size and date, use ls -l. I wrote a post just about the ls options:
The ls command.
Moving around: cd
$ cd Documentos # goes into Documentos
$ cd .. # goes up one level
$ cd # with nothing, goes back to the user's directory
$ cd - # goes back to the previous directory
~ is a shortcut for the user’s directory: cd ~/Downloads works from anywhere.
Finding files: find
Without a filter, find lists everything below the path, recursively:
$ find .
.
./.bashrc
./.profile
./Documentos
./Documentos/notas.txt
./Documentos/relatorio.pdf
./Downloads
./Downloads/pacote.zip
./Musica
With -name, it filters by name. The quotes matter: without them, the shell tries to expand * before find gets
the pattern.
$ find . -name "*.txt"
./Documentos/notas.txt
Two useful filters: -type f returns only files, and -type d only directories. To ignore case in the name, use
-iname.
Searching inside files: grep
grep shows the lines that contain the text you’re looking for:
$ grep "Linux" Documentos/notas.txt
Linux é uma família de sistemas operacionais de código aberto.
Eu uso Linux para programar.
Two of the four lines contain the word “Linux”. A few useful options:
$ grep -n -i "linux" Documentos/notas.txt # -n shows the line number, -i ignores case
1:Linux é uma família de sistemas operacionais de código aberto.
3:Eu uso Linux para programar.
$ grep -c "Linux" Documentos/notas.txt # only counts the lines
2
$ grep -r "Linux" . # searches every file, recursively
Reading files: cat and less
cat dumps the whole file into the terminal. It’s for short files:
$ cat Documentos/notas.txt
Linux é uma família de sistemas operacionais de código aberto.
Ele usa o kernel criado por Linus Torvalds em 1991.
Eu uso Linux para programar.
É rápido e dá para configurar quase tudo.
For long files, less opens the content in a screen you can scroll with the arrow keys or Page Up and Page Down.
Inside it, /text searches, n goes to the next match and q quits.
$ less /var/log/syslog
Counting: wc
$ wc Documentos/notas.txt
4 33 192 Documentos/notas.txt
That’s 4 lines, 33 words and 192 bytes. Notice that the text has 186 characters but 192 bytes: each accented letter
takes two bytes in UTF-8. To count characters, use wc -m; lines only, wc -l.
wc -l combines well with the other commands. For example, to find out how many .txt files exist below the current
directory:
$ find . -name "*.txt" | wc -l
1
Summary
| Command | What it does |
|---|---|
pwd |
Shows the current directory |
ls / ls -a |
Lists the content / including hidden items |
cd folder / cd .. |
Goes into the folder / up one level |
cd |
Goes back to the user’s directory |
find . -name "*.txt" |
Finds files by name |
grep "text" file |
Shows the lines that contain the text |
cat file |
Shows the whole file |
less file |
Opens the file to scroll and search |
wc -l file |
Counts the lines |
Who writes this
Eduardo Lira
Software developer in Rio de Janeiro. I started this blog in 2012, in technical support at a hosting company, writing down the fixes for the problems that came up every day. Today I work at Sofis Tecnologia, on critical healthcare systems and SQL Server database migrations.