Getting Help in Linux
MAN Pages
man ls
Navigation inside man (uses less):
h: Helpq: Quitspace: Next screen/string: Search forward?string: Search backwardn/N: Next/previous search
Command Type
type rm # shows if rm is builtin or executable
type cd # cd is a shell builtin
Help for Commands
help cd # shell built-in help
rm --help # executable help
command --help # general help
Search Man Pages
man -k uname
apropos passwd
Keyboard Shortcuts
TAB TAB: autocompleteCTRL+L: clearCTRL+D: close shellCTRL+U: cut current lineCTRL+A: move cursor to startCTRL+E: move cursor to endCTRL+C: stop commandCTRL+Z: suspend commandCTRL+R: reverse search history
Bash History
history # show history
history -d 100 # delete entry
!! # rerun last command
!20 # run command #20
!abc # run last command starting with abc
Root Access
sudo command # run as root
sudo su # become root
sudo passwd root # set root password
passwd username # change user password
su # switch to root (if password set)
Linux Paths
- Absolute: starts with
/ - Relative: relative to current
.: current dir..: parent dir~: home dir
Changing Directories
cd # home
d cd ~ # home
cd - # last dir
cd /path # go to path
pwd # print working dir
Listing and Tree
ls # list
ls -l # long list
ls -a # include hidden
ls -lh # human readable
ls -t # sort by time
tree . # tree
Disk Usage
du -sh ~ # size of home
File Timestamps
ls -lu # access time
ls -lt # modification time
ls -lc # change time
stat file # all timestamps
Touch
touch file.txt
Date and Calendar
date
cal
cal 2021
cal -3 # prev, current, next
Viewing Files
cat file
cat -n file # with line numbers
less file
head -n 15 file
tail -f file # follow
Creating and Managing Files
touch file
mkdir dir
mkdir -p a/b/c
Copy
cp file1 file2
cp -r dir1 dir2
Move/Rename
mv file1 file2
mv file1 dir/
Remove
rm file
rm -r dir
rm -rf dir # force
Secure Delete
shred -vu -n 100 file
Piping and Redirection
ls -lSh /etc/ | head
ps -ef | grep sshd
ps aux > processes.txt
id >> users.txt
tail -n 10 /var/log/*.log > out.txt 2> err.txt
Finding Files
locate filename
find ~ -type f -size +1M
grep -n pattern file
VIM Basics
Modes: Command, Insert, Last Line
vim file
:wq # save & quit
:q! # quit without saving
User Management
useradd user
usermod user
userdel -r user
groupadd group
groupdel group
File Permissions
ls -l file
chmod 755 file
chmod u+x file
chown user file
chgrp group file
Special: SUID (u+s), SGID (g+s), Sticky Bit (o+t)
Processes
ps -ef
ps aux | less
top
htop
kill pid
killall name
Background/Foreground:
command &
jobs
fg %1
bg %1
Networking
ifconfig
ip addr
route -n
ping host
netstat -tupan
ss -tupan
lsof -iTCP -sTCP:LISTEN
nmap -sS 192.168.0.1
SSH
ssh user@host
ssh -p 2222 user@host
scp file user@host:~
rsync -av /src /dest
Software Management (Debian)
dpkg -i pkg.deb
apt update
apt install package
apt remove package
apt autoremove
Cron Scheduling
crontab -e
* * * * * cmd # every min
30 18 * * * cmd # daily 18:30
@reboot cmd # on boot
System Hardware Info
lscpu
lsusb
lsblk
uname -a
free -m
Services
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl enable nginx
Bash Scripting Basics
Aliases
alias c='clear'
unalias name
Variables
var=value
echo $var
Flow Control
if [ cond ]; then cmd; fi
for i in {1..5}; do echo $i; done
while [ cond ]; do cmd; done
Functions
function name() { cmd; }