Skip to main content

Lesson: The Everyday Command Toolkit

What you'll learn

  • Move around the filesystem confidently with pwd, cd, and ls.
  • Create, copy, move, and delete files and directories — safely.
  • Look inside files with cat, less, head, and tail.
  • Search for files with find and search inside files with grep.
  • Combine small tools with pipes, and get help with man and --help.

Skill gained: the everyday muscle memory to actually drive a Linux box from the terminal.

The lesson

This is the chapter you'll use every single day. Each command is a small program that does one thing well; you combine them to get big results. Type along on the Jumpbox (10.100.100.254, user ubuntu) — reading is not enough, you must build muscle memory.

1. Knowing where you are: pwd, cd, ls

ubuntu@Jumpbox:~$ pwd                 # where am I?
/home/ubuntu
ubuntu@Jumpbox:~$ ls                  # what's here?
Desktop  notes.txt  scripts
ubuntu@Jumpbox:~$ ls -l               # long format: permissions, owner, size, date
-rw-r--r-- 1 ubuntu ubuntu  42 Jun  1 10:00 notes.txt
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun  1 10:00 scripts
ubuntu@Jumpbox:~$ ls -la /etc         # -a also shows hidden files (names starting with .)

Useful ls flags: -l (long), -a (all, incl. hidden), -h (human-readable sizes like 4.0K), -t (sort by time). Combine them: ls -lah.

Move around with cd:

ubuntu@Jumpbox:~$ cd /var/log     # absolute path
ubuntu@Jumpbox:/var/log$ cd ..    # up one level -> /var
ubuntu@Jumpbox:/var$ cd           # no argument -> back home
ubuntu@Jumpbox:~$ cd -            # toggle to the previous directory

2. Making and removing: mkdir, rm, rmdir

ubuntu@Jumpbox:~$ mkdir project              # make a directory
ubuntu@Jumpbox:~$ mkdir -p a/b/c             # -p makes parents as needed
ubuntu@Jumpbox:~$ rmdir project              # remove an EMPTY directory
ubuntu@Jumpbox:~$ rm a/b/c/file.txt          # remove a file
ubuntu@Jumpbox:~$ rm -r a                    # -r removes a directory and everything in it

rm is permanent. There is no Recycle Bin. Before running rm -r, look at what you're about to delete. Never run rm -rf / or rm -rf ~ — those destroy the system or your home. A safety habit: ls the target first, then rm it.

3. Copying and moving: cp, mv

ubuntu@Jumpbox:~$ cp notes.txt notes.bak       # copy a file
ubuntu@Jumpbox:~$ cp -r scripts scripts.bak    # -r to copy a whole directory
ubuntu@Jumpbox:~$ mv notes.bak archive/        # move into a directory
ubuntu@Jumpbox:~$ mv oldname.txt newname.txt   # mv also RENAMES

mv does double duty: moving and renaming are the same operation. Like rm, mv and cp will silently overwrite an existing destination — add -i to be asked first.

4. Looking inside files: cat, less, head, tail

ubuntu@Jumpbox:~$ cat /etc/hostname     # dump a whole (small) file to screen
Jumpbox
ubuntu@Jumpbox:~$ less /var/log/syslog   # scroll a BIG file page by page
ubuntu@Jumpbox:~$ head -n 5 /var/log/syslog   # first 5 lines
ubuntu@Jumpbox:~$ tail -n 20 /var/log/syslog  # last 20 lines
ubuntu@Jumpbox:~$ tail -f /var/log/syslog     # FOLLOW: show new lines as they arrive

Use cat for short files. Use less for long ones — inside less, press Space to page down, /word to search, q to quit. tail -f is your friend for watching a log live (press Ctrl-C to stop).

5. Searching inside files: grep

grep searches text for lines matching a pattern. It is probably the command you'll reach for most.

ubuntu@Jumpbox:~$ grep "error" /var/log/syslog        # lines containing "error"
ubuntu@Jumpbox:~$ grep -i "error" /var/log/syslog     # -i: case-insensitive
ubuntu@Jumpbox:~$ grep -r "TODO" scripts/             # -r: search a whole directory
ubuntu@Jumpbox:~$ grep -n "ssh" /etc/ssh/sshd_config  # -n: show line numbers

6. Searching for files: find

find locates files by name, type, age, size, and more. It walks a directory tree.

ubuntu@Jumpbox:~$ find /etc -name "*.conf"          # files ending in .conf under /etc
ubuntu@Jumpbox:~$ find . -type d                    # directories under here
ubuntu@Jumpbox:~$ find /var/log -name "*.log" -mtime -1   # .log files changed in last day

Remember the difference: grep looks inside files; find looks for files.

7. Pipes: combining tools

This is the heart of the Unix philosophy. A pipe (|) takes the output of one command and feeds it as the input of the next. Small tools become a powerful chain.

        ls -l /etc          grep ".conf"          wc -l
        +---------+   |     +-----------+   |     +------+
        | output  | -----> |  filter   | -----> | count|
        +---------+        +-----------+        +------+
ubuntu@Jumpbox:~$ ls /etc | grep conf            # only entries containing "conf"
ubuntu@Jumpbox:~$ cat /var/log/syslog | grep -i error | wc -l   # count error lines
ubuntu@Jumpbox:~$ ps aux | grep ssh              # find ssh among running processes

wc -l counts lines. You can chain as many stages as you like. Related symbols: > writes output to a file (overwriting), >> appends to a file. command > /dev/null throws output away.

ubuntu@Jumpbox:~$ grep -i error /var/log/syslog > errors.txt   # save matches to a file

8. Getting help: man and --help

You will not memorize every flag. You don't need to — every command documents itself.

ubuntu@Jumpbox:~$ ls --help        # quick summary of flags
ubuntu@Jumpbox:~$ man ls           # full manual page (q to quit, /text to search)

man ("manual") opens the full reference. Manuals are organized in sections; man 5 proc opens the section-5 page for proc. Reading man pages is a real DevOps skill — get comfortable with them now.

9. Tab completion: type less, err less

Press the Tab key and the shell completes file names, directory names, and commands for you. Press Tab twice to list the options when there's more than one match.

ubuntu@Jumpbox:~$ cd /var/lo<Tab>      # completes to /var/log/
ubuntu@Jumpbox:~$ systemc<Tab>         # completes to systemctl

Tab completion prevents typos (a real cause of broken commands and bad rms) and helps you discover what's available. Use it constantly. Also handy: the Up arrow recalls your previous commands, and Ctrl-R searches your command history.

10. A worked example

Find every .conf file under /etc that mentions "ssh", showing line numbers:

ubuntu@Jumpbox:~$ grep -rn "ssh" /etc --include="*.conf"

Or count how many users have a home directory:

ubuntu@Jumpbox:~$ ls /home | wc -l

None of these is special knowledge — it's the same small tools, recombined. Master these ten and you can operate any Linux machine.

Dig deeper

Search terms

  • linux basic commands cheat sheet
  • grep vs find difference
  • linux pipe and redirection tutorial
  • tail -f follow log file
  • how to read a man page

Check yourself

  1. What is the difference between grep and find?
  2. Why should you be careful with rm -r, and what habit reduces the risk?
  3. What does the pipe | do? Give an example of a two-stage pipe.
  4. How do you watch a log file update in real time?
  5. Name two ways to get help on a command without leaving the terminal.