RHEL / Linux Commands Reference
Extracted from RHCSA Certification Materials
Table of Contents
- Essential System Tools
- Navigation and Directory Operations
- File Management
- File Viewing and Text Processing
- File Permissions and Ownership
- File Searching
- Compression and Archiving
- Text Editors
- User and Group Management
- System Information and Monitoring
- System Boot, Shutdown, and Runlevel
- Remote Access (SSH)
- File Transfer
- I/O Redirection and Pipes
- Shell and Environment
- Help and Documentation
- Access Control Lists (ACLs)
- Process Management
1. Essential System Tools
| Command | Description | Example |
|---|---|---|
alias |
Display or create command aliases | alias ll='ls -l --color=auto' |
chvt <num> |
Change virtual terminal | chvt 4 |
clear |
Clear the terminal screen | clear |
echo <text> |
Display a line of text / print variable values | echo $PATH |
env |
Display all environment variables | env |
exit |
Exit the current shell session | exit |
history |
Display command history | history |
history -c |
Clear the in-memory command history | history -c |
history -d <number> |
Delete a specific command from history | history -d 299 |
history -w |
Write current history to .bash_history file | history -w |
logout |
Log out from the current session | logout |
time <command> |
Measure the time it takes to execute a command | time ls |
type <command> |
Show the type of a command (alias, builtin, file) | type pwd |
unalias <name> |
Remove an alias | unalias ll |
which <command> |
Show the full path of a command | which ls |
whereis <command> |
Show the location of binary, source, and man pages | whereis ls |
2. Navigation and Directory Operations
| Command | Description | Example |
|---|---|---|
cd |
Change to home directory | cd |
cd ~ |
Change to home directory | cd ~ |
cd - |
Switch between current and previous directories | cd - |
cd .. |
Move up one level to parent directory | cd .. |
cd / |
Change to root directory | cd / |
cd /path/to/dir |
Change to specified directory (absolute path) | cd /etc/sysconfig |
cd path/to/dir |
Change to specified directory (relative path) | cd etc/sysconfig |
mkdir <dir> |
Create a directory | mkdir files |
mkdir -p <dir> |
Create directory and parent directories if needed | mkdir -p dir2/perl/perl5 |
mkdir -v <dir> |
Create directory with verbose output | mkdir -v files |
pwd |
Print current working directory | pwd |
rmdir <dir> |
Remove an empty directory | rmdir files |
tree |
Display directory hierarchy in tree format | tree /root |
tree -a |
Include hidden files in tree output | tree -a /root |
tree -d |
Show directories only | tree -d /root |
tree -f |
Print full path for each file | tree -f /etc/sysconfig |
tree -h |
Display file sizes in human-readable format | tree -h /etc/sysconfig |
tree -p |
Include file permissions in output | tree -p /etc/sysconfig |
3. File Management
| Command | Description | Example |
|---|---|---|
cat <file> |
Display file contents | cat /etc/profile |
cat -n <file> |
Display file contents with line numbers | cat -n .bash_profile |
cat > <file> |
Create a short text file using input redirection | cat > catfile1 |
cp <src> <dest> |
Copy a file | cp /etc/hosts /tmp |
cp -a <src> <dest> |
Copy preserving all attributes (archive mode) | cp -a ~ /tmp |
cp -R <src> <dest> |
Copy a directory recursively | cp -R /etc /tmp |
cp -r <src> <dest> |
Copy a directory recursively (lowercase) | cp -r /etc /tmp |
cp -v <src> <dest> |
Copy with verbose output | cp -v file1 /tmp |
ln <target> <link> |
Create a hard link | ln file1 hardlink1 |
ln -s <target> <link> |
Create a symbolic (soft) link | ln -s /home /tmp/home_link |
ls |
List files and directories | ls |
ls -a |
List all files including hidden files | ls -a |
ls -d <dir> |
Show directory info, not contents | ls -d /etc |
ls -l |
Long listing with detailed file information | ls -l |
ls -ld <dir> |
Long listing of directory itself, not contents | ls -ld /etc |
ls -lh |
Long listing with human-readable sizes | ls -lh |
ls -lt |
List sorted by modification time (newest first) | ls -lt |
ls -ltr |
List sorted by modification time (oldest first) | ls -ltr |
ls -R |
Recursive listing of directory and subdirectories | ls -R /etc |
ll |
Alias for ls -l --color=auto |
ll |
mv <src> <dest> |
Move or rename a file/directory | mv myfile /tmp |
rm <file> |
Remove a file | rm file1 |
rm -f <file> |
Force remove without confirmation | rm -f file1 |
rm -i <file> |
Interactive removal (prompt before each removal) | rm -i file1 |
rm -r <dir> |
Remove directory and contents recursively | rm -r dir1 |
rm -rf <dir> |
Force remove directory recursively | rm -rf dir1 |
stat <file> |
Display detailed file status information | stat file1 |
tac <file> |
Display file contents in reverse order | tac .bash_profile |
touch <file> |
Create an empty file or update timestamp | touch file1 |
touch -a <file> |
Change only the access time | touch -a file1 |
touch -d <date> <file> |
Set a specific date on a file | touch -d "2019-09-20" file1 |
touch -m <file> |
Change only the modification time | touch -m file1 |
touch -r <ref> <file> |
Set modification time to reference file’s time | touch -r ref.txt file1 |
touch -t <time> <file> |
Set a specific time on a file | touch -t 201909201200 file1 |
4. File Viewing and Text Processing
| Command | Description | Example |
|---|---|---|
awk '{print $4}' <file> |
Print the 4th field of each line | awk '{print $4}' /etc/passwd |
awk -F : '{print $4}' <file> |
Print 4th field using : as delimiter |
awk -F : '{print $4}' /etc/passwd |
awk -F : '/user/ {print $4}' <file> |
Search for pattern and print field | awk -F : '/user/ {print $4}' /etc/passwd |
cut -c <chars> <file> |
Cut specific character positions | cut -c 1-5 file.txt |
cut -d : -f 1 <file> |
Cut specific field using delimiter | cut -d : -f 1 /etc/passwd |
grep <pattern> <file> |
Search for pattern in file | grep anna /etc/passwd |
grep -i <pattern> <file> |
Case-insensitive search | grep -i anna /etc/passwd |
grep -v <pattern> <file> |
Show lines NOT matching pattern | grep -v '^#' /etc/services |
grep -r <pattern> <dir> |
Recursive search in directory | grep -r "error" /var/log/ |
grep -E <pattern> <file> |
Use extended regular expressions | grep -E 'b.+t' regex.txt |
grep -e <pat1> -e <pat2> <file> |
Search for multiple patterns | grep -e '^#' -e '^$' /etc/services |
grep -A <n> <pattern> <file> |
Show n lines after match | grep -A 5 "error" /var/log/messages |
grep -B <n> <pattern> <file> |
Show n lines before match | grep -B 5 "error" /var/log/messages |
head <file> |
Show first 10 lines of a file | head /etc/profile |
head -n <num> <file> |
Show first n lines | head -n 5 /etc/passwd |
less <file> |
View file page by page (forward and backward) | less /etc/passwd |
more <file> |
View file page by page (forward only) | more /etc/passwd |
sed -n <num>p <file> |
Print specific line number | sed -n 5p /etc/passwd |
sed -i s/<old>/<new>/g <file> |
Replace text in file (in-place) | sed -i s/old-text/new-text/g ~/myfile |
sed -i -e '<num>d' <file> |
Delete specific line number | sed -i -e '2d' ~/myfile |
sort <file> |
Sort file contents | sort /etc/passwd |
sort -n <file> |
Sort numerically | sort -n file.txt |
sort -r <file> |
Sort in reverse order | sort -r file.txt |
sort -k3 -t : <file> |
Sort by 3rd column with : delimiter |
sort -k3 -t : /etc/passwd |
sort -rn <file> |
Sort numerically in reverse order | du -h | sort -rn |
tail <file> |
Show last 10 lines of a file | tail /etc/profile |
tail -n <num> <file> |
Show last n lines | tail -n 5 /etc/passwd |
tail -f <file> |
Follow file in real time (monitor updates) | tail -f /var/log/messages |
wc <file> |
Count lines, words, and characters | wc /etc/profile |
wc -l <file> |
Count lines only | wc -l /etc/profile |
wc -w <file> |
Count words only | wc -w /etc/profile |
wc -c <file> |
Count characters/bytes only | wc -c /etc/profile |
5. File Permissions and Ownership
| Command | Description | Example |
|---|---|---|
chmod <mode> <file> |
Change file permissions (octal) | chmod 644 file1 |
chmod u+x <file> |
Add execute permission for owner | chmod u+x script.sh |
chmod g+w <file> |
Add write permission for group | chmod g+w file1 |
chmod o-w <file> |
Remove write permission for others | chmod o-w file1 |
chmod a=rwx <file> |
Set read, write, execute for all | chmod a=rwx file1 |
chmod -R <mode> <dir> |
Change permissions recursively | chmod -R 755 /sdir |
chmod +4000 <file> |
Set setuid bit | chmod +4000 /usr/bin/su |
chmod u+s <file> |
Set setuid bit (symbolic) | chmod u+s /usr/bin/su |
chmod +2000 <file> |
Set setgid bit | chmod +2000 /usr/bin/write |
chmod g+s <dir> |
Set setgid bit on directory | chmod g+s /sdir |
chmod +1000 <dir> |
Set sticky bit | chmod +1000 /tmp |
chmod o+t <dir> |
Set sticky bit (symbolic) | chmod o+t /tmp |
chmod -v <mode> <file> |
Change permissions with verbose output | chmod -v 755 file1 |
chown <user> <file> |
Change file owner | chown user1 file1 |
chown <user>:<group> <file> |
Change owner and group | chown root:sgrp /sdir |
chown -R <user>:<group> <dir> |
Change ownership recursively | chown -R root:sgrp /sdir |
chgrp <group> <file> |
Change file group | chgrp sgrp file1 |
umask |
Display current umask value | umask |
umask -S |
Display umask in symbolic notation | umask -S |
umask <value> |
Set umask value | umask 027 |
file <file> |
Determine file type | file /etc/fstab |
6. File Searching
| Command | Description | Example |
|---|---|---|
find <path> -name <name> |
Find files by name | find . -name "file10" |
find <path> -iname <name> |
Find files by name (case-insensitive) | find /dev -iname "usb*" |
find <path> -size <size> |
Find files by size | find ~ -size -1M |
find <path> -user <user> |
Find files by owner | find / -user daemon |
find <path> -group <group> |
Find files by group | find / -group user1 |
find <path> -not -group <group> |
Find files NOT in group | find / -user daemon -not -group user1 |
find <path> -type <type> |
Find by file type (f=file, d=dir, l=link, b=block, c=char) | find /dev -type b |
find <path> -maxdepth <n> |
Limit search depth | find /usr -name "src" -maxdepth 2 |
find <path> -mindepth <n> |
Minimum search depth | find /usr -name "src" -mindepth 3 |
find <path> -mtime <days> |
Find files modified n days ago | find /etc -mtime +2000 |
find <path> -mmin <mins> |
Find files modified n minutes ago | find /var/log -mmin -100 |
find <path> -perm <mode> |
Find files by exact permissions | find /dev -type b -perm 660 |
find <path> -perm -<mode> |
Find files with at least these permissions | find /dev -type c -perm -222 |
find <path> -name <name> -exec <cmd> {} \; |
Execute command on found files | find / -name "core" -type d -exec ls -ld {} \; |
find <path> -name <name> -ok <cmd> {} \; |
Execute command with confirmation | find /etc/sysconfig -name "*.conf" -ok cp {} /tmp \; |
locate <name> |
Find files using updatedb database | locate file1 |
updatedb |
Update the locate database | sudo updatedb |
whereis <command> |
Find binary, source, and man pages | whereis ls |
7. Compression and Archiving
| Command | Description | Example |
|---|---|---|
gzip <file> |
Compress a file (creates .gz) | gzip fstab |
gzip -l <file.gz> |
Display compression info | gzip -l fstab.gz |
gzip -r <dir> |
Compress directory recursively | gzip -r /tmp/dir1 |
gunzip <file.gz> |
Decompress a .gz file | gunzip fstab.gz |
bzip2 <file> |
Compress a file (creates .bz2) | bzip2 fstab |
bunzip2 <file.bz2> |
Decompress a .bz2 file | bunzip2 fstab.bz2 |
tar -cf <archive> <source> |
Create a tar archive | tar -cf /tmp/home.tar /home |
tar -tvf <archive> |
List contents of a tar archive | tar -tvf /tmp/home.tar |
tar -xf <archive> |
Extract a tar archive | tar -xf /tmp/home.tar |
tar -xf <archive> -C <dir> |
Extract to specific directory | tar -xf /tmp/home.tar -C /tmp |
tar -xf <archive> <file> |
Extract a single file from archive | tar -xf /tmp/files.tar etc/yum.conf |
tar -rf <archive> <file> |
Append files to uncompressed archive | tar -rf /tmp/home.tar /etc/yum.repos.d |
tar -uf <archive> <file> |
Append newer files to archive | tar -uf /tmp/home.tar /etc/yum.repos.d |
tar -czf <archive> <source> |
Create gzip-compressed tarball | tar -czf /tmp/home.tar.gz /home |
tar -cjf <archive> <source> |
Create bzip2-compressed tarball | tar -cjf /tmp/home.tar.bz2 /home |
tar -tzf <archive> |
List contents of gzip-compressed archive | tar -tzf /tmp/home.tar.gz |
tar -xzf <archive> |
Extract gzip-compressed archive | tar -xzf /tmp/home.tar.gz |
tar -xjf <archive> |
Extract bzip2-compressed archive | tar -xjf /tmp/home.tar.bz2 |
tar -cvf <archive> <source> |
Create archive with verbose output | tar -cvf /tmp/home.tar /home |
tar -cpf <archive> <source> |
Create archive preserving permissions | tar -cpf /tmp/home.tar /home |
star -cf <archive> <source> |
Create archive using star (like tar) | star -cf /tmp/home.tar /home |
8. Text Editors
vim Commands
| Command | Description | Example |
|---|---|---|
vim <file> |
Open file in vim editor | vim /etc/fstab |
vim |
Open vim without a file | vim |
vimtutor |
Launch vim tutorial | vimtutor |
i (in vim) |
Insert text before cursor | i |
I (in vim) |
Insert at beginning of line | I |
a (in vim) |
Append text after cursor | a |
A (in vim) |
Append at end of line | A |
o (in vim) |
Open new line below | o |
O (in vim) |
Open new line above | O |
Esc (in vim) |
Switch to command mode | Esc |
:w (in vim) |
Save file | :w |
:wq (in vim) |
Save and quit | :wq |
:q! (in vim) |
Quit without saving | :q! |
:w! (in vim) |
Force save | :w! |
:wq! (in vim) |
Force save and quit | :wq! |
:w <file2> (in vim) |
Save to new file | :w file2 |
dd (in vim) |
Delete current line | dd |
dw (in vim) |
Delete word to right of cursor | dw |
x (in vim) |
Delete character at cursor | x |
X (in vim) |
Delete character before cursor | X |
D (in vim) |
Delete from cursor to end of line | D |
:6,12d (in vim) |
Delete lines 6 through 12 | :6,12d |
yy (in vim) |
Copy current line | yy |
yw (in vim) |
Copy current word | yw |
p (in vim) |
Paste below current line | p |
P (in vim) |
Paste above current line | P |
:1,3co6 (in vim) |
Copy lines 1-3 after line 6 | :1,3co6 |
:4,6m9 (in vim) |
Move lines 4-6 after line 9 | :4,6m9 |
u (in vim) |
Undo last change | u |
U (in vim) |
Undo all changes on current line | U |
Ctrl-r (in vim) |
Redo last undo | Ctrl-r |
. (in vim) |
Repeat last command | . |
:u (in vim) |
Undo last last-line command | :u |
:s/old/new (in vim) |
Replace first occurrence on line | :s/profile/Profile |
:%s/old/new/g (in vim) |
Replace all occurrences in file | :%s/profile/Profile/g |
/string (in vim) |
Search forward | /profile |
?string (in vim) |
Search backward | ?profile |
n (in vim) |
Find next occurrence | n |
N (in vim) |
Find previous occurrence | N |
gg (in vim) |
Go to first line | gg |
G (in vim) |
Go to last line | G |
h (in vim) |
Move cursor left | h |
j (in vim) |
Move cursor down | j |
k (in vim) |
Move cursor up | k |
l (in vim) |
Move cursor right | l |
w (in vim) |
Move to next word | w |
b (in vim) |
Move to previous word | b |
$ (in vim) |
Move to end of line | $ |
0 (in vim) |
Move to beginning of line | 0 |
Ctrl-f (in vim) |
Scroll down one page | Ctrl-f |
Ctrl-b (in vim) |
Scroll up one page | Ctrl-b |
cw (in vim) |
Change word at cursor | cw |
cc (in vim) |
Change entire line | cc |
r (in vim) |
Replace character at cursor | r |
J (in vim) |
Join next line to current | J |
~ (in vim) |
Toggle case at cursor | ~ |
9. User and Group Management
| Command | Description | Example |
|---|---|---|
su |
Switch to another user | su user1 |
su - |
Switch to root with login environment | su - |
sudo -i |
Open a root shell via sudo | sudo -i |
sudo <command> |
Execute command as root | sudo ls /root |
useradd <user> |
Create a new user | useradd user100 |
usermod -aG <group> <user> |
Add user to a group | usermod -aG sgrp user100 |
groupadd <group> |
Create a new group | groupadd sgrp |
groupadd -g <gid> <group> |
Create group with specific GID | groupadd -g 9999 sgrp |
passwd <user> |
Change user password | passwd user1 |
id <user> |
Show user ID and group info | id user1 |
whoami |
Display current username | whoami |
who |
Show who is logged in | who |
w |
Show who is logged in and what they are doing | w |
last |
Show last logged-in users | last |
10. System Information and Monitoring
| Command | Description | Example |
|---|---|---|
df |
Show disk space usage | df |
df -h |
Show disk space in human-readable format | df -h |
df -T |
Show disk space with file system type | df -T |
df -hT |
Show disk space with type, human-readable | df -hT |
df -m |
Show disk space in mebibytes | df -m |
du <file/dir> |
Show disk usage of file/directory | du /home |
du -h <dir> |
Show disk usage in human-readable format | du -h /home |
hostname |
Show or set system hostname | hostname |
uptime |
Show system uptime and load averages | uptime |
uname |
Show operating system name | uname |
uname -a |
Show all system information | uname -a |
uname -r |
Show kernel release version | uname -r |
tty |
Show current terminal device | tty |
ps |
Show current processes | ps |
ps aux |
Show all processes with details | ps aux |
ps -ef |
Show all processes in full format | ps -ef |
top |
Display real-time process information | top |
cat /etc/os-release |
Show OS release information | cat /etc/os-release |
cat /proc/cpuinfo |
Show CPU information | cat /proc/cpuinfo |
cat /proc/meminfo |
Show memory information | cat /proc/meminfo |
mount |
Show all mounted file systems | mount |
findmnt |
Show mounts in tree format | findmnt |
cat /proc/mounts |
Show kernel mount information | cat /proc/mounts |
11. System Boot, Shutdown, and Runlevel
| Command | Description | Example |
|---|---|---|
reboot |
Reboot the system | reboot |
systemctl reboot |
Reboot the system via systemctl | systemctl reboot |
halt |
Halt the system | halt |
systemctl halt |
Halt the system via systemctl | systemctl halt |
poweroff |
Power off the system | poweroff |
systemctl poweroff |
Power off the system via systemctl | systemctl poweroff |
systemctl |
Show status of all systemd units | systemctl |
systemctl status <service> |
Show status of a specific service | systemctl status sshd |
systemctl start <service> |
Start a service | systemctl start sshd |
systemctl stop <service> |
Stop a service | systemctl stop sshd |
systemctl restart <service> |
Restart a service | systemctl restart sshd |
systemctl reload <service> |
Reload service configuration | systemctl reload sshd |
systemctl enable <service> |
Enable service at boot | systemctl enable sshd |
systemctl disable <service> |
Disable service at boot | systemctl disable sshd |
systemctl is-active <service> |
Check if service is active | systemctl is-active sshd |
systemctl is-enabled <service> |
Check if service is enabled | systemctl is-enabled sshd |
systemctl list-units |
List all loaded units | systemctl list-units |
systemctl list-unit-files |
List all unit files | systemctl list-unit-files |
systemctl get-default |
Show default target (runlevel) | systemctl get-default |
systemctl set-default <target> |
Set default target | systemctl set-default multi-user.target |
runlevel |
Show current runlevel | runlevel |
echo b > /proc/sysrq-trigger |
Emergency reset (use with caution!) | echo b > /proc/sysrq-trigger |
12. Remote Access (SSH)
| Command | Description | Example |
|---|---|---|
ssh <user>@<host> |
Connect to remote system via SSH | ssh root@192.168.4.220 |
ssh <host> |
Connect to remote system (current user) | ssh server2 |
ssh -p <port> <host> |
Connect to non-default SSH port | ssh -p 2222 server2 |
ssh -v <host> |
Connect with verbose output | ssh -v server2 |
ssh -Y <user>@<host> |
Connect with X11 forwarding enabled | ssh -Y linda@server2 |
ssh <host> <command> |
Execute command on remote system | ssh server20 hostname |
ssh-keygen |
Generate SSH key pair | ssh-keygen |
ssh-keygen -t rsa |
Generate RSA key pair | ssh-keygen -t rsa |
ssh-keygen -q -N "" |
Generate key pair quietly without passphrase | ssh-keygen -q -N "" |
ssh-copy-id <user>@<host> |
Copy public key to remote system | ssh-copy-id user1@server20 |
systemctl status sshd |
Check SSH server status | systemctl status sshd |
systemctl restart sshd |
Restart SSH server | systemctl restart sshd |
13. File Transfer
| Command | Description | Example |
|---|---|---|
scp <local> <user>@<host>:<remote> |
Securely copy file to remote | scp /etc/hosts server2:/tmp |
scp <user>@<host>:<remote> <local> |
Securely copy file from remote | scp root@server2:/etc/passwd ~ |
scp -r <local> <user>@<host>:<remote> |
Securely copy directory recursively | scp -r server2:/etc/ /tmp |
scp -P <port> <src> <dest> |
SCP using non-default port | scp -P 2222 file.txt user@server:/tmp |
sftp <user>@<host> |
Start interactive SFTP session | sftp user1@server20 |
sftp> get <file> |
Download file from remote (in sftp) | get file.txt |
sftp> put <file> |
Upload file to remote (in sftp) | put file.txt |
sftp> ls |
List remote files (in sftp) | ls |
sftp> lls |
List local files (in sftp) | lls |
sftp> cd <dir> |
Change remote directory (in sftp) | cd /tmp |
sftp> lcd <dir> |
Change local directory (in sftp) | lcd /tmp |
sftp> pwd |
Show remote working directory (in sftp) | pwd |
sftp> lpwd |
Show local working directory (in sftp) | lpwd |
sftp> mkdir <dir> |
Create remote directory (in sftp) | mkdir mydir |
sftp> lmkdir <dir> |
Create local directory (in sftp) | lmkdir mydir |
sftp> rm <file> |
Remove remote file (in sftp) | rm file.txt |
sftp> rename <old> <new> |
Rename remote file (in sftp) | rename old.txt new.txt |
sftp> quit |
Exit sftp session | quit |
rsync <src> <dest> |
Synchronize files locally | rsync -av grub.cfg /tmp/ |
rsync -av <src> <dest> |
Sync with archive mode and verbosity | rsync -av /etc/rsyslog.conf /tmp/ |
rsync -avz <src> <user>@<host>:<dest> |
Sync to remote with compression | rsync -avz /etc/rsyslog.conf user1@server20:/tmp/ |
rsync -avP <src> <dest> |
Sync showing transfer progress | rsync -avP /etc/rsyslog.conf user1@server20:/tmp/ |
rsync -rv <user>@<host>:<src> <dest> |
Sync from remote recursively | rsync -rv user1@server20:/home/user1 /tmp/trans/ |
14. I/O Redirection and Pipes
| Command | Description | Example |
|---|---|---|
> |
Redirect STDOUT (overwrite) | ls > output |
>> |
Redirect STDOUT (append) | ls >> output |
2> |
Redirect STDERR | ls ilwehgi 2> /dev/null |
2>&1 |
Redirect STDERR to STDOUT | ls whuhiu > errout 2>&1 |
< |
Redirect STDIN | cat < file.txt |
| |
Pipe output to another command | ls | less |
> /dev/null |
Discard output | ls > /dev/null |
> /dev/tty1 |
Redirect output to specific terminal | ls > /dev/tty1 |
15. Shell and Environment
| Command | Description | Example |
|---|---|---|
echo $PATH |
Display PATH variable | echo $PATH |
echo $USER |
Display current username | echo $USER |
echo $HOME |
Display home directory | echo $HOME |
echo $LANG |
Display language setting | echo $LANG |
echo $PWD |
Display current working directory | echo $PWD |
echo $OLDPWD |
Display previous working directory | echo $OLDPWD |
export <VAR>=<value> |
Set environment variable | export MYVAR="hello" |
set |
Display all shell variables | set |
unset <VAR> |
Remove a variable | unset MYVAR |
!number |
Execute command by history number | !299 |
!sometext |
Execute last command starting with text | !ls |
Ctrl-r |
Reverse search in command history | Ctrl-r then type text |
Tab |
Bash completion (autocomplete) | Type partial command, press Tab |
16. Help and Documentation
| Command | Description | Example |
|---|---|---|
man <command> |
Display manual page for a command | man ls |
man 5 <file> |
Display manual page for config files | man 5 sshd_config |
man 7 file-hierarchy |
Display file system hierarchy documentation | man 7 file-hierarchy |
man -k <keyword> |
Search manual pages by keyword (same as apropos) | man -k user |
apropos <keyword> |
Search manual page descriptions | apropos user |
info <command> |
Display info documentation | info ls |
whatis <command> |
Show one-line description of a command | whatis ls |
mandb |
Update the manual page database | sudo mandb |
updatedb |
Update the locate/whereis database | sudo updatedb |
<command> --help |
Display command’s built-in help | ls --help |
/usr/share/doc/ |
Directory containing package documentation | ls /usr/share/doc/ |
17. Access Control Lists (ACLs)
| Command | Description | Example |
|---|---|---|
getfacl <file> |
Display ACL settings on a file | getfacl /tmp/aclfile1 |
setfacl -m u:<user>:<perms> <file> |
Set ACL for a named user | setfacl -m u:user1:rw- /tmp/aclfile1 |
setfacl -m g:<group>:<perms> <file> |
Set ACL for a named group | setfacl -m g:dba:rw- /tmp/aclfile1 |
setfacl -m u:<uid>:<perms> <file> |
Set ACL using UID | setfacl -m u:1000:r-- /tmp/aclfile1 |
setfacl -d -m u:<user>:<perms> <dir> |
Set default ACL on a directory | setfacl -d -m u:user1:rw- /tmp/dir1 |
setfacl -x u:<user> <file> |
Remove ACL for a named user | setfacl -x u:user1 /tmp/aclfile1 |
setfacl -b <file> |
Remove all ACL entries | setfacl -b /tmp/aclfile1 |
setfacl -k <dir> |
Remove default ACLs from directory | setfacl -k /tmp/dir1 |
setfacl --restore=<file> |
Restore ACLs from a file | setfacl --restore=acl_backup.txt |
getfacl -R <dir> |
Recursively display ACLs | getfacl -R /tmp/dir1 |
18. Process Management
| Command | Description | Example |
|---|---|---|
ps |
Display current processes | ps |
ps aux |
Display all processes with details | ps aux |
ps -ef |
Display all processes in full format | ps -ef |
ps -ef | grep <process> |
Search for specific process | ps -ef | grep sshd |
top |
Display real-time process monitor | top |
kill <PID> |
Send SIGTERM to a process | kill 1234 |
kill -9 <PID> |
Force kill a process (SIGKILL) | kill -9 1234 |
killall <process> |
Kill all processes by name | killall firefox |
pkill <process> |
Kill process by name (pattern match) | pkill firefox |
nice <command> |
Run command with modified priority | nice -n 10 long_task |
renice <priority> -p <PID> |
Change priority of running process | renice 10 -p 1234 |
bg |
Resume suspended job in background | bg |
fg |
Bring background job to foreground | fg |
jobs |
List background jobs | jobs |
Ctrl-z |
Suspend current foreground process | Ctrl-z |
Ctrl-c |
Terminate current foreground process | Ctrl-c |
Ctrl-d |
Send EOF / log out | Ctrl-d |
Ctrl-l |
Clear screen (same as clear) | Ctrl-l |
Appendix: Wildcards and Special Characters
| Pattern | Description | Example |
|---|---|---|
* |
Matches zero or more characters | ls *.txt |
? |
Matches exactly one character | ls c?t matches cat, cut |
[abc] |
Matches one character from the set | ls c[auo]t matches cat, cut, cot |
[^abc] |
Matches one character NOT in the set | ls c[^auo]t |
~ |
Home directory of current user | cd ~ |
. |
Current directory | cd . |
.. |
Parent directory | cd .. |
/ |
Root directory / path separator | cd / |
- |
Previous directory (with cd) | cd - |
Appendix: Regular Expressions
| Pattern | Description | Example |
|---|---|---|
^text |
Line starts with text | grep '^anna' /etc/passwd |
text$ |
Line ends with text | grep 'ash$' /etc/passwd |
. |
Any single character | grep 'r.t' file matches rat, rot, rut |
[abc] |
One character from set | grep 'r[aou]t' file |
* |
Zero or more of previous char | grep 'ab*c' file matches ac, abc, abbc |
\{n\} |
Exactly n of previous char | grep 're\{2\}d' file matches reed |
\{m,n\} |
m to n of previous char | grep 're\{1,3\}d' file |
? (extended) |
Zero or one of previous char | grep -E 'colou?r' file |
+ (extended) |
One or more of previous char | grep -E 'b.+t' file |
(group) |
Group characters | grep -E '/web(/.*)?' file |
Appendix: Common Key Combinations
| Key | Description |
|---|---|
Ctrl-c |
Terminate current process |
Ctrl-d |
Send EOF / log out |
Ctrl-l |
Clear screen |
Ctrl-r |
Reverse search in history |
Ctrl-z |
Suspend current process |
Tab |
Auto-complete command/filename |
Esc |
Exit insert mode (vim) |
Alt-F1 through Alt-F6 |
Switch virtual terminals |
Ctrl-Alt-F1 through Ctrl-Alt-F6 |
Switch virtual terminals from GUI |