Linux Common Commands and Vi Editor Shortcuts



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

CommandWhat it does
pwdPrint the current directory
cd /u01/app/oracleGo to a directory
cd -Jump back to the previous directory
cd ~Go to your home directory
ls -lLong listing: permissions, owner, size, time
ls -laInclude hidden files (names starting with .)
ls -lrtSort by time, newest at the bottom. The DBA classic for finding the latest trace or log
ls -lhSSort by size, largest first, human-readable
tree -L 2Show a directory tree two levels deep (install the tree package if missing)

2. Working with files

CommandWhat it does
touch file.txtCreate an empty file, or update its timestamp
mkdir -p /u02/stage/patchCreate a directory, including any missing parents
cp -p fileA fileBCopy, keeping permissions and timestamps
cp -r dirA dirBCopy a directory recursively
mv old.log old.log.bakMove or rename
rm file.txtDelete a file
rm -i *.trcDelete with a confirmation prompt for each file
rm -r dirnameDelete a directory and its contents
ln -s /u01/app/oracle/diag diagCreate 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

CommandWhat it does
cat filePrint the whole file
cat -n filePrint with line numbers
less filePage through a large file. / to search, q to quit
head -50 fileFirst 50 lines
tail -100 alert_ORCL.logLast 100 lines
tail -f alert_ORCL.logFollow a file live as new lines are written (Ctrl+C to stop)
grep ORA- alert_ORCL.logFind lines containing a string
grep -i error fileCase-insensitive search
grep -n ORA- fileShow line numbers with matches
grep -v '^#' init.oraHide matching lines (here: comments)
grep -c ORA-00600 fileCount matching lines
grep -rl 'LISTENER' $ORACLE_HOME/network/adminSearch recursively, list file names only
grep -B2 -A5 ORA-04031 fileShow 2 lines before and 5 after each match
wc -l fileCount lines
diff file1 file2Show 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

CommandWhat it does
find / -name sqlnet.ora 2>/dev/nullFind a file by name anywhere, hiding permission errors
find . -name '*.trc' -mtime +7Trace files older than 7 days
find . -name '*.trc' -mtime +7 -delete…and delete them (check without -delete first!)
find /u01 -size +1G -type fFiles larger than 1 GB
which sqlplusShow which binary will run
locate tnsnames.oraFast search from a prebuilt index (run updatedb first)

5. Text processing: sort, cut, awk, sed

CommandWhat it does
sort fileSort alphabetically
sort -n fileSort numerically
sort -rn fileNumeric, largest first
sort -u fileSort and remove duplicates
uniq -cCount repeated adjacent lines (use after sort)
cut -d: -f1 /etc/passwdPick field 1 using : as the separator
awk '{print $1, $5}' filePrint columns 1 and 5
sed -n '100,200p' filePrint only lines 100 to 200
sed -i.bak 's/old/new/g' fileReplace text in place, keeping a .bak copy

6. Users, groups and permissions

CommandWhat it does
whoamiCurrent user
id oracleUser ID, primary group and all groups
sudo commandRun one command with root privileges
su - oracleSwitch to the oracle user with its full login environment
sudo useradd -m user1Create a user with a home directory
sudo passwd user1Set or change a password
sudo usermod -aG dba user1Add a user to an extra group (-a append is essential, or other groups are removed)
sudo usermod -g oinstall user1Change the user's primary group
sudo usermod -aG wheel user1Grant sudo on RHEL / Oracle Linux (the group is sudo on Ubuntu)
sudo userdel -r user1Delete a user and their home directory
sudo groupadd dba / groupdel dbaCreate or delete a group
chmod 750 script.shOwner: rwx, group: r-x, others: none
chmod u+x script.shMake a script executable for its owner
chown -R oracle:oinstall /u01/appChange 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

CommandWhat it does
df -hFree space per filesystem, human-readable
df -h /u01Free space for one mount point
du -sh *Size of each item in the current directory
du -sh * | sort -rh | headThe 10 largest items: find what is filling a disk
free -gRAM and swap usage in GB
lsblkBlock devices, partitions and mount points
cat /proc/meminfo | grep -i hugeHugePages status, important for a large Oracle SGA

8. Processes and performance

CommandWhat it does
ps -ef | grep [p]monAll running Oracle instances (the [p] trick hides the grep itself)
ps -ef | grep [t]nslsnrRunning listeners
ps aux --sort=-%mem | headTop memory consumers
topLive CPU and memory view. 1 = per-CPU, M = sort by memory, q = quit
htopA friendlier top (install if missing)
vmstat 5 5CPU, memory and swap every 5 seconds, 5 times
iostat -xm 5Disk throughput and latency per device (sysstat package)
sar -u 5 5CPU history and live samples (sysstat package)
uptimeLoad average for 1, 5 and 15 minutes
kill PIDAsk a process to stop cleanly
kill -9 PIDForce-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 / bgManage 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

CommandWhat it does
ip aShow IP addresses (replaces the older ifconfig)
ip rRouting table (replaces netstat -r)
ss -tlnpListening TCP ports and which process owns them (replaces netstat -tlnp)
ss -tnp | grep 1521Active connections to the Oracle listener port
ping -c 4 dbhostBasic reachability test
nc -zv dbhost 1521Check whether a port is open (telnet is often not installed)
nslookup dbhostDNS lookup
hostname -fFully qualified host name
curl -O https://example.com/file.zipDownload a file over HTTP(S)
wget https://example.com/file.zipDownload a file (alternative to curl)

10. Copying between servers and SSH

CommandWhat it does
ssh oracle@dbhost2Log in to a remote server
ssh-keygen -t ed25519Create a modern SSH key pair (use -t rsa -b 4096 for older systems)
ssh-copy-id oracle@dbhost2Install 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

CommandWhat it does
tar -cvf backup.tar dir/Create a tar archive
tar -xvf backup.tarExtract a tar archive
tar -czvf backup.tar.gz dir/Create a gzip-compressed archive
tar -xzvf backup.tar.gz -C /targetExtract into a specific directory
tar -cjvf backup.tar.bz2 dir/Create a bzip2 archive (smaller, slower)
tar -tvf backup.tar.gzList the contents without extracting
gzip file / gunzip file.gzCompress or decompress one file
unzip -q patch.zip -d /u02/stageExtract a zip quietly into a directory

12. System information and services

CommandWhat it does
uname -aKernel version and architecture
cat /etc/os-releaseLinux distribution and version
nprocNumber of CPUs
date / calCurrent date and time / a calendar
history | tail -20The last 20 commands you ran
ulimit -aShell resource limits (open files, processes) for the current user
sudo systemctl status firewalldStatus of a service
sudo journalctl -xeRecent system log messages
crontab -l / crontab -eList or edit scheduled jobs
sudo dnf install sysstatInstall 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

KeysAction
:wSave
:wqSave and quit (same as ZZ)
:qQuit (only if there are no changes)
:q!Quit and throw away changes
:w newnameSave as a new file
:shOpen a shell; type exit to come back to vi

Entering insert mode

KeysAction
iInsert before the cursor
aAppend after the cursor
AAppend at the end of the line
IInsert at the start of the line
oOpen a new line below
OOpen a new line above

Moving around

KeysAction
h j k lLeft, down, up, right
w bNext word / previous word
0Start of line
^First non-blank character of the line
$End of line
ggFirst line of the file
GLast line of the file
:150Go to line 150
H M LTop, middle, bottom of the screen
Ctrl+d Ctrl+uScroll half a page down / up

Editing

KeysAction
xDelete the character under the cursor
dwDelete to the end of the word
DDelete to the end of the line
ddDelete (cut) the whole line
5ddDelete 5 lines
dGDelete from here to the end of the file
yyCopy (yank) the current line
5yyCopy 5 lines
p PPaste after / before the cursor
SClear the line and start typing
uUndo
Ctrl+rRedo
.Repeat the last change

Search and replace

KeysAction
/ORA-Search forward
?ORA-Search backward
n NNext / previous match
:s/old/new/Replace the first match on the current line
:s/old/new/gReplace all matches on the current line
:%s/old/new/gReplace in the whole file
:%s/old/new/gcReplace in the whole file, asking each time

Handy settings

KeysAction
:set nuShow line numbers (:set nonu to hide)
:set icCase-insensitive search
:syntax onSyntax highlighting (vim)
:set listShow 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