DevTools

Cheatsheet Linux CMD

Comandos para terminal Linux

Back to languages
Linux CMD
33 cards found
Categories:
Versions:

Files and Folders


5 cards
Navigation
pwd           # current folder
ls -la       # list with details
cd /home/user # change folder
cd ..        # go up one level
cd ~         # go to home
cd -         # previous folder

Move around the system.

File information
file photo.png  # file type
du -sh folder/  # folder size
du -h --max-depth=1 # per subfolder
wc -l file  # count lines
stat file   # metadata

Details and sizes.

Create and delete
mkdir folder       # create folder
mkdir -p a/b/c   # create nested
touch file.txt # create empty
rm file.txt   # delete
rm -r folder      # delete folder
rmdir folder      # only if empty

Create and remove.

Copy and move
cp a.txt b.txt    # copy file
cp -r folder/ dest/ # copy folder
mv a.txt b.txt    # move/rename
mv a.txt /tmp/    # move to folder
ln -s target link   # symbolic link

Copy, move and link.

ls in detail
ls -l  # long format
ls -a  # include hidden
ls -h  # readable sizes
ls -t  # by date
ls -S  # by size
ls -R  # recursive

ls options.

Text and Filters


5 cards
View content
cat file   # show everything
less file   # paginate (q exits)
head -n 10 f   # first 10 lines
tail -n 10 f   # last 10 lines
tail -f log.txt # follow in real time

Read text files.

sort, uniq, cut
sort f.txt      # sort
sort -n f.txt   # sort numeric
sort -r f.txt   # reverse
uniq f.txt      # remove duplicates
cut -d: -f1 /etc/passwd # 1st field

Sort and extract.

grep (search)
grep "error" log.txt   # search text
grep -i "error" f     # ignore case
grep -r "TODO" .     # recursive
grep -n "default" f   # with line no.
grep -v "ok" f      # exclude
grep -E "a|b" f     # extended regex

Filter lines by pattern.

sed (replace)
# Replace text
sed 's/old/new/' f.txt

# Replace all (global)
sed 's/a/b/g' f.txt

# Edit in place
sed -i 's/foo/bar/' f.txt

Transform text with sed.

awk (columns)
# Print 1st column
awk '{print $1}' f.txt

# 1st and 3rd columns
awk '{print $1, $3}' f.txt

# Filter lines
awk '$3 > 100' f.txt

Process by columns.

Permissions and Users


4 cards
chmod (permissions)
chmod 755 script.sh # rwxr-xr-x
chmod 644 f.txt    # rw-r--r--
chmod +x script.sh  # make executable
chmod -R 755 folder/ # recursive

# r=4 w=2 x=1
# owner | group | others

Set permissions.

chown (owner)
chown user f.txt      # change owner
chown user:group f.txt # owner and group
chown -R user folder/  # recursive

Change owner.

Users
whoami        # current user
id            # user info
sudo command  # the root
su - user     # switch user
passwd        # change password

Identity management.

sudo and root
sudo apt update   # command the root
sudo -i          # root shell
sudo visudo      # edit sudoers
sudo su -        # become root

Administrator privileges.

Processes and System


4 cards
View processes
ps aux         # all processes
ps aux | grep nginx # filter
top            # real-time monitor
htop           # interactive top
pgrep -l node  # search by name

List active processes.

Terminate processes
kill 1234      # terminate by PID
kill -9 1234   # force (SIGKILL)
pkill -f node  # by name
killall nginx  # all with the name
xkill          # click to close

Stop processes.

System resources
df -h        # disk space
free -h      # RAM memory
uptime       # time and load
uname -a     # kernel info
lscpu        # CPU info

Monitor resources.

Background
command &      # run in background
Ctrl+Z        # suspend
bg            # resume in background
fg            # bring to foreground
jobs          # list tasks
nohup cmd &    # ignore hangup

Job control.

Network


4 cards
Connectivity
ping google.com    # test connection
ping -c 4 host     # 4 packets
ip addr            # interfaces/IP
ip route           # routes
hostname -I        # local IPs

Diagnose the network.

SSH and SCP
ssh user@host       # connect remote
ssh -p 2222 user@h  # custom port
scp f.txt user@h:/tmp # send file
scp -r folder/ u@h:/d  # send folder
ssh-keygen          # generate key

Secure remote access.

Transfers
wget url          # download
wget -c url       # resume
curl -O url       # save file
rsync -av src/ dst/ # synchronize

Download and synchronize.

Ports and DNS
ss -tulpn      # open ports
dig dominio.com # DNS query
nslookup host  # resolve DNS
traceroute host # packet path

Ports and resolution.

Compression and Files


3 cards
tar
# Create .tar.gz
tar -czvf backup.tar.gz folder/

# Extract .tar.gz
tar -xzvf backup.tar.gz

# List content
tar -tzvf backup.tar.gz

tar.gz archives.

gzip / gunzip
gzip file    # compress (.gz)
gunzip file.gz # decompress
gzip -d f.gz     # decompress
gzip -k f        # keep original
zcat f.gz        # view without extracting

gzip compression.

zip / unzip
zip -r archive.zip folder/ # create
unzip archive.zip       # extract
unzip -l archive.zip    # list
zip -e safe.zip f.txt  # with password

ZIP format.

File Search


3 cards
find
find . -name "*.txt"    # by name
find / -type d -name log # only folders
find . -size +100M     # by size
find . -mtime -7       # last 7 days
find . -name "*.tmp" -delete # delete

Advanced search.

locate and which
locate file  # fast database
sudo updatedb  # update index

which python   # executable path
whereis gcc    # binary + source + man
type ls       # command type

Locate quickly.

Combine with xargs
# Delete all the .log
find . -name "*.log" | xargs rm

# Count lines of several files
find . -name "*.py" | xargs wc -l

Chain commands.

Shortcuts and Tricks


5 cards
Pipes and redirection
cmd1 | cmd2    # output → input
cmd > f.txt    # overwrite
cmd >> f.txt   # append
cmd 2> errors   # stderr to file
cmd < input  # read from file

Data flow.

Package manager (apt)
sudo apt update      # update list
sudo apt upgrade     # upgrade packages
sudo apt install pkg  # install
sudo apt remove pkg   # remove
apt search word   # search

Manage software (Debian/Ubuntu).

History
history      # view history
!!           # last command
!123        # command no. 123
Ctrl+R      # incrementsl search
sudo !!     # repeat with sudo

Reuse commands.

Keyboard shortcuts
Ctrl+C  # interrupt
Ctrl+L  # clear screen
Ctrl+A  # start of line
Ctrl+E  # end of line
Ctrl+U  # delete to the start
Tab     # autocomplete

Productivity in the terminal.

Help
man command    # full manual
command --help # quick help
command -h    # brief help
info command  # info documentation
apropos word # search in the man

Get help.