1. cheat-sheets
  2. /command line

Command Line Essentials – A Cheat Sheet for Linux and Unix-like Systems

Through the command line, users can execute commands and run scripts. On Linux and Unix-like systems, this interaction is facilitated by a shell, which is a program that interprets and executes commands from the user. Bash is one of the most widely used shells. The commands listed in this cheat sheet are primarily tailored for Bash, but many of them are also applicable to other shells like Zsh or Ksh and on Unix-like systems including macOS. For Windows users, these commands can be accessed through a Unix-like environment such as Cygwin or Windows Subsystem for Linux (WSL).

File Management

CommandDescription
lsLists files and directories; -lh for detailed, human-readable output.
cdChanges the current directory to the specified one.
mvMoves or renames files or directories based on the destination.
cpCopies files or directories to a new location.
rmRemoves specified files or directories.
mkdirCreates a new directory with the specified name.
touchCreates an empty file or updates a file's timestamp.

Permissions and Ownership

CommandDescription
chmodChanges permissions of files or directories; chmod 755 file.txt.
chownChanges owner/group of files or directories; chown username:groupname file.txt.
chgrpChanges group of files or directories; chgrp groupname file.txt.

Searching and Sorting

CommandDescription
grepSearches for a pattern in files or output; grep "pattern" file.txt.
findSearches for files or directories; find / -name "file.txt".
locateSearches for files or directories in system database; locate file.txt.
sortSorts lines of a file or stream alphabetically; sort file.txt.
diffShows differences between two files; diff file1.txt file2.txt.

System Information and Monitoring

CommandDescription
topDisplays real-time process information.
dfShows disk space usage; -h for human-readable.
duDisplays file or directory size; -sh for summary, human-readable.
freeShows memory usage; -m for megabytes.
unameDisplays system information; -a for all info.
uptimeShows current uptime.
whoDisplays who is logged on.
whoamiShows the current user name.
wShows who is logged on and what they are doing.

File Viewing and Editing

CommandDescription
catConcatenates and displays file contents; cat file1.txt file2.txt.
headShows first 10 lines of a file; head file.txt.
tailShows last 10 lines of a file; tail file.txt.
lessDisplays file contents one page at a time; less file.txt.
nanoTerminal text editor; nano file.txt.
viTerminal text editor; vi file.txt.
sedStream editor for filtering and transforming text; sed 's/old/new/' file.txt.

Package Management

CommandDescription
aptManages packages on Debian-based systems; apt install package-name.
dnfManages packages on Red Hat-based systems; dnf install package-name.

Network Operations

CommandDescription
pingTests network connectivity; ping google.com.
tracerouteShows packet path to a host; traceroute google.com.
routeDisplays/modifies routing table; route -n.
sshSecure shell connection to a remote server; ssh user@hostname.
curlTransfers data using various protocols; curl http://example.com.
wgetDownloads files from the internet; wget http://example.com/file.txt.

Process Management

CommandDescription
psLists running processes; ps aux for detailed info.
killTerminates processes; kill 12345 for process ID 12345.
cronSchedules commands; managed through crontab -e.
crontabEdits schedule for recurring tasks; crontab -e.

Miscellaneous Commands

CommandDescription
echoPrints message to terminal; echo "Hello World".
dateDisplays current date and time.
clearClears terminal screen.
rebootRestarts the system; sudo reboot.
awkText processing and data extraction; awk '{print $1}' file.txt.
bashStarts a new bash shell.
gzipCompresses files; gzip file.txt.
unzipExtracts zip archives; unzip archive.zip.
teeSends output to terminal and file; echo "Hello" | tee file.txt.
timeMeasures command execution time; time ls.
dmesgDisplays kernel message log.
systemctlManages system services; systemctl start service-name.
serviceManages services; service service-name start.
chrootChanges root directory; sudo chroot /mnt/new-root.

Considerations and Caveats

  • For detailed command options and flags, use the man command followed by the command name (e.g., man ls).

  • Some commands may need superuser privileges, which can be obtained using the sudo command (e.g., sudo reboot).

  • The behavior of some commands may vary slightly depending on the environment or shell you are using. It's advisable to refer to the man pages or help command (command --help) in your specific environment to understand any nuances.

Additional Resources

Learn Enough Command Line to Be Dangerous

The Linux Command Line (Book by William Shotts)

Windows Subsystem for Linux Documentation

VS Code Cheat Sheet