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
Command | Description |
---|
ls | Lists files and directories; -lh for detailed, human-readable output. |
cd | Changes the current directory to the specified one. |
mv | Moves or renames files or directories based on the destination. |
cp | Copies files or directories to a new location. |
rm | Removes specified files or directories. |
mkdir | Creates a new directory with the specified name. |
touch | Creates an empty file or updates a file's timestamp. |
Permissions and Ownership
Command | Description |
---|
chmod | Changes permissions of files or directories; chmod 755 file.txt . |
chown | Changes owner/group of files or directories; chown username:groupname file.txt . |
chgrp | Changes group of files or directories; chgrp groupname file.txt . |
Searching and Sorting
Command | Description |
---|
grep | Searches for a pattern in files or output; grep "pattern" file.txt . |
find | Searches for files or directories; find / -name "file.txt" . |
locate | Searches for files or directories in system database; locate file.txt . |
sort | Sorts lines of a file or stream alphabetically; sort file.txt . |
diff | Shows differences between two files; diff file1.txt file2.txt . |
Command | Description |
---|
top | Displays real-time process information. |
df | Shows disk space usage; -h for human-readable. |
du | Displays file or directory size; -sh for summary, human-readable. |
free | Shows memory usage; -m for megabytes. |
uname | Displays system information; -a for all info. |
uptime | Shows current uptime. |
who | Displays who is logged on. |
whoami | Shows the current user name. |
w | Shows who is logged on and what they are doing. |
File Viewing and Editing
Command | Description |
---|
cat | Concatenates and displays file contents; cat file1.txt file2.txt . |
head | Shows first 10 lines of a file; head file.txt . |
tail | Shows last 10 lines of a file; tail file.txt . |
less | Displays file contents one page at a time; less file.txt . |
nano | Terminal text editor; nano file.txt . |
vi | Terminal text editor; vi file.txt . |
sed | Stream editor for filtering and transforming text; sed 's/old/new/' file.txt . |
Package Management
Command | Description |
---|
apt | Manages packages on Debian-based systems; apt install package-name . |
dnf | Manages packages on Red Hat-based systems; dnf install package-name . |
Network Operations
Command | Description |
---|
ping | Tests network connectivity; ping google.com . |
traceroute | Shows packet path to a host; traceroute google.com . |
route | Displays/modifies routing table; route -n . |
ssh | Secure shell connection to a remote server; ssh user@hostname . |
curl | Transfers data using various protocols; curl http://example.com . |
wget | Downloads files from the internet; wget http://example.com/file.txt . |
Process Management
Command | Description |
---|
ps | Lists running processes; ps aux for detailed info. |
kill | Terminates processes; kill 12345 for process ID 12345. |
cron | Schedules commands; managed through crontab -e . |
crontab | Edits schedule for recurring tasks; crontab -e . |
Miscellaneous Commands
Command | Description |
---|
echo | Prints message to terminal; echo "Hello World" . |
date | Displays current date and time. |
clear | Clears terminal screen. |
reboot | Restarts the system; sudo reboot . |
awk | Text processing and data extraction; awk '{print $1}' file.txt . |
bash | Starts a new bash shell. |
gzip | Compresses files; gzip file.txt . |
unzip | Extracts zip archives; unzip archive.zip . |
tee | Sends output to terminal and file; echo "Hello" | tee file.txt . |
time | Measures command execution time; time ls . |
dmesg | Displays kernel message log. |
systemctl | Manages system services; systemctl start service-name . |
service | Manages services; service service-name start . |
chroot | Changes 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