Oracle, PostgreSQL, MongoDB and most other databases in production run on Linux. Whatever database you manage, a big part of the day happens at the shell prompt: checking the alert log, finding what filled a disk, copying a patch between servers, or editing a parameter file in vi.
This post is the cheat sheet I wish I had when I started. The commands are grouped by task, each one has a one-line explanation, and the examples use real DBA situations. The last section is a complete vi editor reference.
Tested on: RHEL / Oracle Linux 8 and 9 with the bash shell. Almost everything works the same on other distributions. Where a command differs, the table says so.
Contents: navigation · files · viewing & searching · finding files · text processing · users & permissions · disk & memory · processes · networking · SSH · archives · system & services · vi editor
1. Navigating files and directories
| Command | What it does |
|---|
pwd | Print the current directory |
cd /u01/app/oracle | Go to a directory |
cd - | Jump back to the previous directory |
cd ~ | Go to your home directory |
ls -l | Long listing: permissions, owner, size, time |
ls -la | Include hidden files (names starting with .) |
ls -lrt | Sort by time, newest at the bottom. The DBA classic for finding the latest trace or log |
ls -lhS | Sort by size, largest first, human-readable |
tree -L 2 | Show a directory tree two levels deep (install the tree package if missing) |
2. Working with files
| Command | What it does |
|---|
touch file.txt | Create an empty file, or update its timestamp |
mkdir -p /u02/stage/patch | Create a directory, including any missing parents |
cp -p fileA fileB | Copy, keeping permissions and timestamps |
cp -r dirA dirB | Copy a directory recursively |
mv old.log old.log.bak | Move or rename |
rm file.txt | Delete a file |
rm -i *.trc | Delete with a confirmation prompt for each file |
rm -r dirname | Delete a directory and its contents |
ln -s /u01/app/oracle/diag diag | Create a symbolic link |
Careful with rm -rf. There is no recycle bin. Run ls with the same path first, and never run rm -rf with a variable that could be empty (for example rm -rf $ORACLE_HOME/ when the variable is not set).
3. Viewing and searching file contents
| Command | What it does |
|---|
cat file | Print the whole file |
cat -n file | Print with line numbers |
less file | Page through a large file. / to search, q to quit |
head -50 file | First 50 lines |
tail -100 alert_ORCL.log | Last 100 lines |
tail -f alert_ORCL.log | Follow a file live as new lines are written (Ctrl+C to stop) |
grep ORA- alert_ORCL.log | Find lines containing a string |
grep -i error file | Case-insensitive search |
grep -n ORA- file | Show line numbers with matches |
grep -v '^#' init.ora | Hide matching lines (here: comments) |
grep -c ORA-00600 file | Count matching lines |
grep -rl 'LISTENER' $ORACLE_HOME/network/admin | Search recursively, list file names only |
grep -B2 -A5 ORA-04031 file | Show 2 lines before and 5 after each match |
wc -l file | Count lines |
diff file1 file2 | Show differences between two files |
A real example: the 20 most frequent ORA- errors in the alert log.
grep -o 'ORA-[0-9]*' alert_ORCL.log | sort | uniq -c | sort -rn | head -20
4. Finding files
| Command | What it does |
|---|
find / -name sqlnet.ora 2>/dev/null | Find a file by name anywhere, hiding permission errors |
find . -name '*.trc' -mtime +7 | Trace files older than 7 days |
find . -name '*.trc' -mtime +7 -delete | …and delete them (check without -delete first!) |
find /u01 -size +1G -type f | Files larger than 1 GB |
which sqlplus | Show which binary will run |
locate tnsnames.ora | Fast search from a prebuilt index (run updatedb first) |
5. Text processing: sort, cut, awk, sed
| Command | What it does |
|---|
sort file | Sort alphabetically |
sort -n file | Sort numerically |
sort -rn file | Numeric, largest first |
sort -u file | Sort and remove duplicates |
uniq -c | Count repeated adjacent lines (use after sort) |
cut -d: -f1 /etc/passwd | Pick field 1 using : as the separator |
awk '{print $1, $5}' file | Print columns 1 and 5 |
sed -n '100,200p' file | Print only lines 100 to 200 |
sed -i.bak 's/old/new/g' file | Replace text in place, keeping a .bak copy |
6. Users, groups and permissions
| Command | What it does |
|---|
whoami | Current user |
id oracle | User ID, primary group and all groups |
sudo command | Run one command with root privileges |
su - oracle | Switch to the oracle user with its full login environment |
sudo useradd -m user1 | Create a user with a home directory |
sudo passwd user1 | Set or change a password |
sudo usermod -aG dba user1 | Add a user to an extra group (-a append is essential, or other groups are removed) |
sudo usermod -g oinstall user1 | Change the user's primary group |
sudo usermod -aG wheel user1 | Grant sudo on RHEL / Oracle Linux (the group is sudo on Ubuntu) |
sudo userdel -r user1 | Delete a user and their home directory |
sudo groupadd dba / groupdel dba | Create or delete a group |
chmod 750 script.sh | Owner: rwx, group: r-x, others: none |
chmod u+x script.sh | Make a script executable for its owner |
chown -R oracle:oinstall /u01/app | Change owner and group recursively |
Reading permission numbers: each digit is the sum of 4 (read) + 2 (write) + 1 (execute), for owner, group and others in that order. So 750 = rwx / r-x / ---.
Avoid chmod 777. It lets every user on the server modify the file. For scripts, 750 or 755 is almost always enough.
7. Disk and memory
| Command | What it does |
|---|
df -h | Free space per filesystem, human-readable |
df -h /u01 | Free space for one mount point |
du -sh * | Size of each item in the current directory |
du -sh * | sort -rh | head | The 10 largest items: find what is filling a disk |
free -g | RAM and swap usage in GB |
lsblk | Block devices, partitions and mount points |
cat /proc/meminfo | grep -i huge | HugePages status, important for a large Oracle SGA |
8. Processes and performance
| Command | What it does |
|---|
ps -ef | grep [p]mon | All running Oracle instances (the [p] trick hides the grep itself) |
ps -ef | grep [t]nslsnr | Running listeners |
ps aux --sort=-%mem | head | Top memory consumers |
top | Live CPU and memory view. 1 = per-CPU, M = sort by memory, q = quit |
htop | A friendlier top (install if missing) |
vmstat 5 5 | CPU, memory and swap every 5 seconds, 5 times |
iostat -xm 5 | Disk throughput and latency per device (sysstat package) |
sar -u 5 5 | CPU history and live samples (sysstat package) |
uptime | Load average for 1, 5 and 15 minutes |
kill PID | Ask a process to stop cleanly |
kill -9 PID | Force-kill a process (last resort) |
nohup ./job.sh > job.log 2>&1 & | Run in the background; it keeps going after you log out |
jobs / fg / bg | Manage background jobs in the current shell |
Never kill -9 an Oracle background process such as pmon, smon or lgwr. That crashes the instance. To end a user session, use ALTER SYSTEM KILL SESSION from SQL*Plus instead.
9. Networking
| Command | What it does |
|---|
ip a | Show IP addresses (replaces the older ifconfig) |
ip r | Routing table (replaces netstat -r) |
ss -tlnp | Listening TCP ports and which process owns them (replaces netstat -tlnp) |
ss -tnp | grep 1521 | Active connections to the Oracle listener port |
ping -c 4 dbhost | Basic reachability test |
nc -zv dbhost 1521 | Check whether a port is open (telnet is often not installed) |
nslookup dbhost | DNS lookup |
hostname -f | Fully qualified host name |
curl -O https://example.com/file.zip | Download a file over HTTP(S) |
wget https://example.com/file.zip | Download a file (alternative to curl) |
10. Copying between servers and SSH
| Command | What it does |
|---|
ssh oracle@dbhost2 | Log in to a remote server |
ssh-keygen -t ed25519 | Create a modern SSH key pair (use -t rsa -b 4096 for older systems) |
ssh-copy-id oracle@dbhost2 | Install your public key for password-less login |
scp file oracle@dbhost2:/u02/stage/ | Copy a file to another server |
rsync -avP /u02/stage/ dbhost2:/u02/stage/ | Copy a directory with progress; resumes if interrupted |
11. Archives and compression
| Command | What it does |
|---|
tar -cvf backup.tar dir/ | Create a tar archive |
tar -xvf backup.tar | Extract a tar archive |
tar -czvf backup.tar.gz dir/ | Create a gzip-compressed archive |
tar -xzvf backup.tar.gz -C /target | Extract into a specific directory |
tar -cjvf backup.tar.bz2 dir/ | Create a bzip2 archive (smaller, slower) |
tar -tvf backup.tar.gz | List the contents without extracting |
gzip file / gunzip file.gz | Compress or decompress one file |
unzip -q patch.zip -d /u02/stage | Extract a zip quietly into a directory |
12. System information and services
| Command | What it does |
|---|
uname -a | Kernel version and architecture |
cat /etc/os-release | Linux distribution and version |
nproc | Number of CPUs |
date / cal | Current date and time / a calendar |
history | tail -20 | The last 20 commands you ran |
ulimit -a | Shell resource limits (open files, processes) for the current user |
sudo systemctl status firewalld | Status of a service |
sudo journalctl -xe | Recent system log messages |
crontab -l / crontab -e | List or edit scheduled jobs |
sudo dnf install sysstat | Install a package on RHEL 8/9 and Oracle Linux (yum on older releases, apt on Ubuntu) |
13. vi / vim editor cheat sheet
vi has two main modes. In Normal mode keys are commands. In Insert mode keys type text. Press Esc at any time to return to Normal mode.
Save and quit
| Keys | Action |
|---|
| :w | Save |
| :wq | Save and quit (same as ZZ) |
| :q | Quit (only if there are no changes) |
| :q! | Quit and throw away changes |
| :w newname | Save as a new file |
| :sh | Open a shell; type exit to come back to vi |
Entering insert mode
| Keys | Action |
|---|
| i | Insert before the cursor |
| a | Append after the cursor |
| A | Append at the end of the line |
| I | Insert at the start of the line |
| o | Open a new line below |
| O | Open a new line above |
Moving around
| Keys | Action |
|---|
| h j k l | Left, down, up, right |
| w b | Next word / previous word |
| 0 | Start of line |
| ^ | First non-blank character of the line |
| $ | End of line |
| gg | First line of the file |
| G | Last line of the file |
| :150 | Go to line 150 |
| H M L | Top, middle, bottom of the screen |
| Ctrl+d Ctrl+u | Scroll half a page down / up |
Editing
| Keys | Action |
|---|
| x | Delete the character under the cursor |
| dw | Delete to the end of the word |
| D | Delete to the end of the line |
| dd | Delete (cut) the whole line |
| 5dd | Delete 5 lines |
| dG | Delete from here to the end of the file |
| yy | Copy (yank) the current line |
| 5yy | Copy 5 lines |
| p P | Paste after / before the cursor |
| S | Clear the line and start typing |
| u | Undo |
| Ctrl+r | Redo |
| . | Repeat the last change |
Search and replace
| Keys | Action |
|---|
| /ORA- | Search forward |
| ?ORA- | Search backward |
| n N | Next / previous match |
| :s/old/new/ | Replace the first match on the current line |
| :s/old/new/g | Replace all matches on the current line |
| :%s/old/new/g | Replace in the whole file |
| :%s/old/new/gc | Replace in the whole file, asking each time |
Handy settings
| Keys | Action |
|---|
| :set nu | Show line numbers (:set nonu to hide) |
| :set ic | Case-insensitive search |
| :syntax on | Syntax highlighting (vim) |
| :set list | Show tabs and line endings |
Bonus: a quick DBA server check in one go
Paste this when you log in to an unfamiliar database server to get the big picture in a few seconds:
hostname -f; uptime; nproc; free -g
df -h | grep -v tmpfs
ps -ef | grep [p]mon
ps -ef | grep [t]nslsnr
cat /proc/meminfo | grep -i hugepages_total
Conclusion
You do not need to memorise all of these at once. Start with the ones you use daily (ls -lrt, tail -f, grep, df -h, du -sh, ps -ef) and the rest will follow. Bookmark this page and come back when you need it.
Which Linux command saves you the most time as a DBA? Share it in the comments.
Tamilselvan Perumal · Lead Product Engineer & Senior DBA