Catalog / Command Line Tools Cheatsheet

Command Line Tools Cheatsheet

A comprehensive guide to essential command-line tools, covering navigation, file manipulation, system information, and more. This cheat sheet provides quick references and examples to boost your command-line efficiency.

Navigation & File Management

Basic Navigation

pwd

Print working directory (shows the current directory).
Example:

pwd
# Output: /home/user/documents

cd

Change directory.
Examples:

cd /path/to/directory
cd ..        # Move up one level
cd ~         # Go to home directory
cd -         # Go to the previous directory

ls

List directory contents.
Examples:

ls          # List files and directories in current directory
ls -l       # Long listing format (permissions, size, date)
ls -a       # Show hidden files and directories
ls -t       # Sort by modification time (newest first)
ls -R       # List subdirectories recursively

File Manipulation

touch filename

Create an empty file.
Example:

touch newfile.txt

cp source destination

Copy a file or directory.
Examples:

cp file1.txt file2.txt  # Copy file1 to file2
cp -r dir1 dir2         # Copy directory dir1 to dir2 recursively

mv source destination

Move or rename a file or directory.
Examples:

mv file1.txt newfile.txt  # Rename file1 to newfile
mv file1.txt /new/path/   # Move file1 to a different directory

rm filename

Remove a file.
Example:

rm file.txt

Caution: This action is irreversible!

rm -r directory

Remove a directory recursively (including all files and subdirectories).
Example:

rm -r mydirectory

Caution: Use with extreme care!

mkdir directoryname

Create a new directory.
Example:

mkdir newdirectory

rmdir directoryname

Remove an empty directory.
Example:

rmdir emptydirectory

File Content Display

cat filename

Display the entire contents of a file.
Example:

cat mytextfile.txt

less filename

View file contents one page at a time. Navigate with arrow keys or q to quit.
Example:

less largefile.txt

head filename

Display the first few lines of a file (default 10 lines).
Example:

head mytextfile.txt
head -n 20 mytextfile.txt  # Show the first 20 lines

tail filename

Display the last few lines of a file (default 10 lines).
Example:

tail mytextfile.txt
tail -n 20 mytextfile.txt  # Show the last 20 lines
tail -f mytextfile.txt   # Follow the file for live updates

System Information & Process Management

System Information

uname -a

Display system information (kernel name, network node hostname, kernel release, kernel version, machine).
Example:

uname -a

df -h

Display disk space usage in a human-readable format.
Example:

df -h

free -m

Display memory usage in megabytes.
Example:

free -m

uptime

Show how long the system has been running, along with the current time and load averages.
Example:

uptime

lscpu

Display information about the CPU architecture.
Example:

lscpu

Process Management

ps aux

Display all running processes with detailed information.
Example:

ps aux

top

Display a dynamic real-time view of running processes.
Example:

top

kill PID

Terminate a process with the specified PID (Process ID).
Example:

kill 1234

kill -9 PID

Forcefully terminate a process (use as a last resort).
Example:

kill -9 1234

Caution: May cause data loss.

bg

Place a stopped process in the background.
Example:

# Ctrl+Z to stop a process, then:
bg

fg

Bring a background process to the foreground.
Example:

fg

User Management

whoami

Display the current user’s username.
Example:

whoami

id

Display user and group ID information.
Example:

id

w

Show who is logged on and what they are doing.
Example:

w

last

List last logged in users.
Example:

last

Text Processing & Search

Text Processing

grep pattern filename

Search for a pattern in a file.
Examples:

grep 'error' logfile.txt  # Find lines containing 'error'
grep -i 'error' logfile.txt # Case-insensitive search
grep -r 'error' ./       # Recursive search in current directory

sed 's/old/new/g' filename

Stream editor for replacing text in a file. This example replaces all occurrences of ‘old’ with ‘new’.
Example:

sed 's/apple/orange/g' fruits.txt

awk '{print $1}' filename

A powerful text processing tool. This example prints the first column of each line.
Example:

awk '{print $1}' data.txt  # Print the first column
awk '{print $1, $3}' data.txt # Print the first and third columns

sort filename

Sort lines in a file.
Example:

sort unsorted.txt
sort -n numbers.txt  # Sort numerically

uniq filename

Remove duplicate lines from a file (often used with sort).
Example:

sort file.txt | uniq  # Remove duplicate lines after sorting

wc filename

Count words, lines, and bytes in a file.
Example:

wc text.txt  # Output: lines words bytes filename
wc -l text.txt # Only count lines

cut -d',' -f1 filename

Cut sections from each line of a file. This example cuts the first field from a comma-separated file.
Example:

cut -d',' -f1 data.csv # Extract the first field

Find Command

find . -name "*.txt"

Find files with a specific name pattern in the current directory and its subdirectories.
Example:

find . -name "*.txt"  # Find all .txt files

find . -type f -size +1M

Find files larger than 1MB in the current directory and its subdirectories.
Example:

find . -type f -size +1M # Find files larger than 1MB

find . -mtime -7

Find files modified in the last 7 days.
Example:

find . -mtime -7       # Find files modified in the last 7 days

find . -empty

Find empty files and directories.
Example:

find . -empty          # Find empty files and directories

find . -perm 777

Find files with specific permissions.
Example:

find . -perm 777        # Find files with 777 permissions

Networking & Package Management

Networking Tools

ping hostname

Test network connectivity to a host.
Example:

ping google.com

traceroute hostname

Trace the route packets take to a host.
Example:

traceroute google.com

netstat -tulnp

Display network connections, listening ports, and associated processes.
Example:

netstat -tulnp

ifconfig or ip addr

Display network interface configuration.
Example:

ifconfig
ip addr

ssh user@host

Secure Shell - connect to a remote host.
Example:

ssh [email protected]

scp user@host:file localfile

Secure Copy - copy files between hosts.
Example:

scp [email protected]:/path/to/remote/file localfile.txt

Package Management (Debian/Ubuntu)

sudo apt update

Update the package lists.
Example:

sudo apt update

sudo apt upgrade

Upgrade installed packages.
Example:

sudo apt upgrade

sudo apt install package_name

Install a new package.
Example:

sudo apt install vim

sudo apt remove package_name

Remove a package.
Example:

sudo apt remove vim

sudo apt purge package_name

Remove a package and its configuration files.
Example:

sudo apt purge vim

apt show package_name

Show information about a package.
Example:

apt show vim

Package Management (RedHat/CentOS/Fedora)

sudo yum update

Update all packages.
Example:

sudo yum update

sudo yum install package_name

Install a package.
Example:

sudo yum install vim

sudo yum remove package_name

Remove a package.
Example:

sudo yum remove vim

yum info package_name

Show information about a package.
Example:

yum info vim

sudo dnf update

Update all packages (Fedora).
Example:

sudo dnf update

sudo dnf install package_name

Install a package (Fedora).
Example:

sudo dnf install vim