Home / Command line

The command line

A few commands go a long way.

Terminal window on laptop next to an Agenda VR3.
CommandWhat it does
pwdShow the current folder
ls -lList files with details
cd folderChange folder
mkdir nameCreate a folder
cp a b / mv a bCopy or move a file
rm fileDelete a file (there is no recycle bin)
less fileRead a long text file page by page
grep word fileSearch for text inside files
man commandRead the manual for a command

Pipes and redirection

The | symbol sends the output of one command to another, and > writes output to a file:

ls -l /etc | grep conf
df -h > disk-report.txt

Be careful as root

Commands run with sudo can change or delete anything. Read a command twice before pressing Enter.

Wildcards, spaces and awkward file names

The shell can work on groups of files at once. That is handy, but it also means a command may affect more files than you expected.

  • *.txt means every file ending in .txt in the current folder.
  • report-?.md matches names with one character in that position, such as report-a.md.
  • image[0-9].png matches image1.png to image9.png.

Before pressing Enter, look at the pattern and picture the real file names it will expand to. cp *.jpg backup/ is convenient; rm *.jpg is final.

Spaces in names need care. If a folder is called Holiday photos, use quotes:

  • cd "Holiday photos"
  • mv "old name.txt" "new name.txt"

A backslash works too, as in Holiday\ photos, but quotes are usually easier to read.

Linux also has hidden files and folders whose names begin with a dot, such as .bashrc or .config. They are often used for settings in your home directory. ls -a shows them. Hidden does not mean protected; it only means they are not shown by default. For that side of things, see File system and Users & permissions.

One more awkward case: a file name that starts with -. Many commands read that as an option. The usual fix is --, which marks the end of options: rm -- -oldfile.

Finding files and checking where space has gone

Sooner or later you stop remembering where everything is. find is the standard tool for searching by name, type and age.

A few useful examples:

  • find . -name "*.log" — find log files below the current folder.
  • find . -iname "readme*" — case-insensitive search.
  • find ~/Documents -type f -mtime -7 — regular files changed in the last seven days.
  • find /etc -type f | less — browse a long list a page at a time.

If you search from the top of the system, you may see “Permission denied” messages. That is normal on a multi-user system and is part of how Linux protects files.

When the problem is disk space rather than missing files, du is often more useful than find:

  • du -sh . — total size of the current folder.
  • du -sh * — size of each item inside it.
  • du -sh .[!.]* * — include hidden items as well.

The -h flag makes sizes easier to read. If one folder is much larger than the rest, go into it and run the command again.

Another small but useful command is file. It guesses what a file contains, even when the name is unclear: file download.bin. That helps with odd attachments, old archives and unknown binaries when you are doing basic Troubleshooting.

Small terminal habits that save time

A few keyboard shortcuts make the command line less tedious.

  • Press Tab to complete a file or command name.
  • Press Tab twice, on many shells, to list possible matches.
  • Use the Up and Down arrows to move through previous commands.
  • Press Ctrl+C to stop a command that is hanging or running longer than expected.
  • Press Ctrl+L to clear the screen without closing the shell.
  • Press Ctrl+D to send end-of-input; in an empty shell, that usually logs you out.

The command history is worth knowing about too. history prints recent commands with numbers beside them. If you made a small mistake a few commands ago, it is usually faster to recall it with the arrow keys and edit it than to type it again.

Redirection has one or two extra tricks beyond >.

  • >> appends to a file instead of replacing it.
  • 2> writes error messages to a file.
  • 2>/dev/null discards error messages completely.

For example:

  • echo "Finished" >> build.log
  • find / -name "*.conf" 2>errors.txt

You can also save output and still see it on screen with tee:

  • uname -a | tee system.txt

That is useful when you want a record of a command while you work. Once these patterns feel natural, they carry straight into simple automation on the Shell scripting page.