Command line basics: shell, terminal, pwd, ls, cd, man, history, tab completion, I/O redirection, pipes

Linux Essentials Preparation · lesson 5 of 23 · 25 min

The Linux Terminal

The terminal (or command line) is the primary interface for Linux administration. Commands follow the pattern: command [options] [arguments]. Options modify behavior (usually prefixed with - or --); arguments are the targets the command acts on.

Example: ls -la /home — ls is the command, -la are options (long format + show hidden), /home is the argument.

Essential Commands

Useful Shortcuts and Operators

Pipe example

grep "error" /var/log/syslog | less — search for “error” in syslog and page through the results.

Getting Help

Flashcard Drill

pwd Print Working Directory — shows your current location in the file system. ls -la List all files (including hidden) in long format — shows permissions, owner, size, date. cd .. Move up one directory level. cd ~ Go to your home directory. rm -rf Recursively and forcefully remove a directory and all contents — use with caution! grep Search for a pattern in files — grep “error” /var/log/syslog. Pipe | Sends output of one command as input to another — cmd1 | cmd2. Redirect > Writes command output to a file (overwrites) — echo “text” > file.txt. man Displays the manual page for a command — man ls. Tab completion Press Tab to auto-complete commands and file/directory names.

Worked Scenario: Command Line Troubleshooting

Scenario

A sysadmin needs to find all lines containing “FAILED” in /var/log/auth.log and save them to a file called failed_logins.txt. Write the command.

Answer: grep "FAILED" /var/log/auth.log > failed_logins.txt

Breaking it down: grep "FAILED" searches for the pattern, /var/log/auth.log is the file to search, and > redirects the output to failed_logins.txt (creating or overwriting it). To append instead of overwrite, use >>.

To also count how many lines matched: grep "FAILED" /var/log/auth.log | wc -l — pipe to wc -l (word count, lines).

Knowledge Check — 12 Questions

Command Line Basics (Linux Essentials)

Which command shows your current directory?

  • ls
  • cd
  • pwd
  • dir

Answer: pwd
pwd (Print Working Directory) shows the full path of your current location.

Which command lists all files including hidden files in long format?

  • ls
  • ls -l
  • ls -la
  • ls -h

Answer: ls -la
ls -la shows all files (-a includes hidden files starting with .) in long format (-l).

To go to your home directory, type cd _____.

Answer: ~
cd ~ navigates to your home directory (/home/username).

Which command searches for the word “failed” in /var/log/auth.log?

  • find “failed” /var/log/auth.log
  • grep “failed” /var/log/auth.log
  • cat “failed” /var/log/auth.log
  • less “failed” /var/log/auth.log

Answer: grep “failed” /var/log/auth.log
grep searches for patterns in files — grep “pattern” filename.

What does the pipe operator | do?

  • Redirects output to a file
  • Sends output of one command as input to another
  • Appends output to a file
  • Runs two commands simultaneously

Answer: Sends output of one command as input to another
The pipe | chains commands — output of the left command becomes input to the right.

rm -rf will ask for confirmation before deleting files.

  • True
  • False

Answer: False
rm -rf (recursive, force) deletes without confirmation — extremely dangerous. Use with caution.

Which command displays the manual page for the ls command?

  • help ls
  • ls –manual
  • man ls
  • info ls

Answer: man ls
man (manual) displays the full documentation for any command — man ls.

To write command output to a file (overwriting it), use the _____ operator.

Answer: >
The > operator redirects output to a file, overwriting existing content.

Which keyboard shortcut cancels a running command?

  • Ctrl+D
  • Ctrl+Z
  • Ctrl+C
  • Ctrl+X

Answer: Ctrl+C
Ctrl+C sends SIGINT to the running process, canceling it.

Which command pages through a large file one screen at a time?

  • cat
  • more
  • less
  • head

Answer: less
less allows you to scroll through large files — press q to quit, / to search.

To append output to an existing file without overwriting it, use:

|

  • <
  • Answer: >>

appends output to a file. > overwrites. Example: echo “line” >> file.txt

Which command counts the number of lines in a file?

  • count
  • wc -l
  • lines
  • grep -c

Answer: wc -l
wc -l (word count, lines) counts the number of lines in a file or piped input.


Previous lesson: Linux career paths: sysadmin, DevOps, cloud engineer, security analyst
Back to Linux Essentials Preparation
Next lesson: Linux file system hierarchy: /, /etc, /var, /home, /tmp, /proc, /dev, /usr, /bin; file types