COMMANDS

Linuxcheatsheet

Navigate the terminal, work with files, then inspect permissions and processes.

Download .txt ↓

66 commands

Navigate directories

Show your current directory

View
pwd

List files

View
ls

Include details and hidden files

View
ls -lah

Enter a directory

Change
cd /PATH/TO/DIRECTORY

Go up one directory

Change
cd ..

Go to your home directory

Change
cd ~

Create, copy & move files

Create a directory

Change
mkdir DIRECTORY

Create nested directories

Change
mkdir -p PATH/TO/DIRECTORY

Creates missing parent directories too.

Create an empty file

Change
touch FILE

If the file exists, updates timestamps without clearing its content.

Copy a file

Change
cp -i SOURCE DESTINATION

Prompts before overwriting an existing destination.

Move or rename a file

Change
mv -i SOURCE DESTINATION

Prompts before overwriting an existing destination.

Read files & logs

Print a file

View
cat FILE

Read a file one screen at a time

View
less FILE

Press q to quit; / searches within the file.

Read the first 30 lines

View
head -n 30 FILE

Read the last 50 lines

View
tail -n 50 FILE

Follow a rotating log

View
tail -F FILE

Retries when the file is replaced. Stop with Ctrl+C.

Search files & text

Find files by name

View
find . -type f -name "*.log"

Searches under the current directory. Keep the pattern quoted.

Search with line numbers

View
grep -n -- "PATTERN" FILE

PATTERN is a regular expression.

Search for literal text

View
grep -Fn -- "TEXT" FILE

Treats the search text as literal characters.

Count lines

View
wc -l FILE

Identity & permissions

Show your username

View
whoami

Show your identity and groups

View
id

Inspect file permissions

View
ls -l FILE

Show detailed file information

View
stat FILE

Make a file private to its owner

Change
chmod 600 FILE

Owner can read and write; group and others have no access. Does not change ownership.

Make a script executable by its owner

Change
chmod u+x SCRIPT.sh

Storage & processes

Show filesystem free space

View
df -h

Show a directory size

View
du -sh DIRECTORY

Permission errors may mean some paths were not counted.

List processes

View
ps -ef

Request a process to stop

Change
kill -TERM PID

Verify the process ID first. Sends a termination signal.

Remove files & directories

Remove an empty directory

Delete
rmdir DIRECTORY

Only succeeds if the directory is empty.

Remove a file with confirmation

Delete
rm -i -- FILE

Permanently deletes the named file after a prompt; does not move it to trash.

Get command help

Read a command manual

View
man COMMAND

For example: man ls. Press q to quit.

Show built-in shell help

View
help cd

For Bash shell built-ins.

Pipelines and structured text

Count lines, words, and bytes

View
wc -l -w -c FILE

Sort numbers numerically

View
sort -n FILE

Count repeated sorted lines

View
sort FILE | uniq -c

Extract a delimited field

View
cut -d: -f2 FILE

Selects the second colon-separated field.

Print a whitespace-separated field

View
awk '{print $1}' FILE

Match only the start of a line

View
grep -n '^ERROR ' FILE

Select lines that do not match

View
grep -v 'PATTERN' FILE

Preview an exact-line replacement

View
sed 's/^OLD$/NEW/' FILE

Prints the result without changing FILE.

Replace an exact line in place

Change
sed -i 's/^OLD$/NEW/' FILE

Linux sed example; overwrites the matched lines. Review or back up important files first.

Write output to screen and a file

Change
COMMAND | tee output.txt

Overwrites output.txt; tee -a appends.

Links and file metadata

Create a relative symbolic link

Change
ln -s TARGET LINK_NAME

The target path is interpreted from the link’s directory.

Read a link target

View
readlink LINK_NAME

Inspect a file mode and owner

View
stat FILE

Create private files by default

Change
umask 077

Applies to subsequently created files in this shell.

Find log files safely

View
find . -type f -name '*.log'

Pass filenames using null separators

View
find . -type f -name '*.log' -print0 | xargs -0 wc -l

Avoids splitting paths containing spaces.

Show hidden files with details

View
ls -la

Identify file content type

View
file FILE

Requires the file utility.

Compare, archive, and verify

Show a unified diff

View
diff -u BEFORE AFTER

Exit 1 means differences; greater than 1 means an error.

Compare bytes without printing contents

View
cmp FIRST SECOND

Exit 0 means identical; 1 means different.

Record a checksum

Change
sha256sum FILE > FILE.sha256

Writes a digest and filename into a new checksum file.

Check a recorded checksum

View
sha256sum -c FILE.sha256

List a compressed tar archive

View
tar -tzf ARCHIVE.tar.gz

Create a compressed archive

Change
tar -czf ARCHIVE.tar.gz DIRECTORY

Creates or replaces the named archive.

Extract into a chosen directory

Change
tar -xzf ARCHIVE.tar.gz -C DESTINATION

Inspect untrusted archive paths and use a dedicated destination.

Processes and services

List processes

View
ps -ef

Inspect one process

View
ps -p PID -o pid,ppid,comm

Request a specific process to stop

Change
kill -TERM PID

Check the PID and owner first; this can stop running work.

Inspect listening TCP sockets

View
ss -lnt

Linux iproute2 utility; package availability varies.

Show interface addresses

View
ip address show

Linux iproute2 utility.

Read recent service logs

View
journalctl -u SERVICE -n 100 --no-pager

Requires systemd journal access.

Inspect a service without changing it

View
systemctl status SERVICE --no-pager

Requires a systemd-based environment.