pete > guides > linux > commands
This page enumerates, categorizes, and very briefly describes a bunch of Linux command-line programs that you may find useful. The documentation provided here is (intentionally) insufficient to actually use them; recall that you can use the man command to read the online manual page (arrow keys and pgup/pgdn scroll, 'q' to exit). Therefore, to read about all the options accepted by the ls program, run
$ man ls
Some of the programs below are standard, as part of the "coreutils" and "util-linux" packages in Arch Linux; others will need to be installed separately.
Vocabulary
"Recursive" refers to an operation that descends into child directories, their children, their children’s children, and so forth. Therefore, applying the '-r' parameter to grep will cause it to search not only the files in the current directory, but also all files within and below it.
"Regular expression" (in the context of command-line stuff) is an amped-up version of the thing with the same name that you saw/will see in CSCI 0200/0301. They are often used to describe data being searched for. There are different varieties (POSIX, Extended POSIX, Perl-compatible, …), but regexone.com gives a good overview with exercises.
Commands
The single most important one
- man: access the online manual
The second most important one, for system administration
- sudo: run a program with temporarily-increased privileges
Get me outta here
- exit: terminate the terminal session
- logout: terminate the terminal session
- Ctrl-d: terminate the terminal session (only when no other characters have been entered at the prompt; otherwise behaves as Enter)
Basic filesystem stuff
- pwd: report the current working directory
- ls: list information about files
- cd: change to a different directory
- mkdir: create a directory
- mv: move a file (equivalent to renaming it, even if its new name puts it in a new directory)
- cp: copy a file
- rm: remove a file
- rmdir: remove a directory
- touch: create a file or, if the file exists, update its modification time
- chmod: change permissions of a file
- chown: change ownership (user/group) of a file
- ln: create a (symbolic) link
Text analysis
- grep: search inside file(s)
- wc: count characters/words/lines in a file
- echo: output a string to the terminal
- cat: output the contents of a file to the terminal
- head: print lines from the beginning of a file
- tail: print lines from the end of a file
- diff: compare the contents of files
- less: allow keyboard-based scrolling through a file (manpages are run through less, thus the same navigation commands apply)
Text manipulation
- sort: you’ll never guess
- uniq: filter out consecutive duplicate lines from a file
- cut: combine files line by line
- paste: separate files line by line
- sed: modify files, line by line, according to regular expressions
- awk: lightweight programming language for modifying files line by line
- fold: break long lines into paragraphs
- fmt: more complex version of fold
- column: neatly format text into evenly-spaced columns
More complex file stuff
- find: locate files based on metadata (eg, name, size, modification time)
- xargs: often used to send the results of find to other programs
- tar: create an archive containing multiple files
- gzip: compress a file (historically ubiquitous)
- xz: compress a file (more recently popular)
- stat: report metadata information about a file
- dd: copy file contents (more tweakable than cp, useful for copying from raw devices, eg, /dev/urandom)
Measuring and monitoring
- du: measure sizes of files/directories
- time: measure the runtime of a program
- ps: list running processes (instant snapshot)
- top: list running processes (updates repeatedly)
Miscellaneous
- date: report the date
- sleep: pause for a configurable duration
- history: list recently-run commands
- clear: clear the terminal screen
- reset: clear the terminal screen with extreme prejudice (useful if you want to erase the scrollback buffer or reverse text corruption when playing with, eg, customized fonts/colors in your shell)
Network
- ifconfig: report information about network interface configuration (classic tool)
- ip: report information about network interface configuration (newer tool)
- route: manipulate routing table
- netstat: report information about network configuration
- ping: check reachability of machines over the Internet
- traceroute: trace the route to another machine on the Internet
- mtr: trace the route to another machine on the Internet (my preferred choice)
- netcat: the Swiss Army Knife of the Internet
- wget: save web pages as files
- curl: save web pages as files (alternate)
More complex
- ssh: log into a machine over the Internet
- rsync: efficiently copies files (even across the Internet)
- tmux: resumable terminal sessions
- script: record the results of a terminal session in a file
- cron: schedule programs to be run at certain times
- bc: do math
Playing with input and output
Most of the aforementioned commands print output to the screen. Many of them can accept input from the keyboard.
You can save the output of a program in a file with the ">" operator. The following shell command will cause the output of ls -ltr to be saved in the file named foo:
$ ls -ltr > foo
Likewise, you can provide a file as input to a program with the "<" operator. This is exactly equivalent to running the program and typing the contents of the file at the program directly. The following shell command will cause the contents of the file bar to be fed to the program my-guessing-game as if they had been typed by a user using the keyboard:
$ ./my-guessing-game < bar
Furthermore, you can use the "|" operator (called a "pipe") to pass the output of a program directly to another program as input:
$ ls -ltr | ./my-guessing-game
To be more precise with the above, the ">" redirection causes the standard output (stdout, file descriptor 1) to refer to the named file; the "<" redirection causes standard input (stdin, file descriptor 0) to refer to the named file; and the "|" operator to affect stdout of the preceding program and stdin of the succeeding program. If you want to redirect standard error (stderr, file descriptor 2), use "2>".
More information on redirection can be found in the Advanced Bash-Scripting Guide.