A Comprehensive Guide on RHEL Unix Commands 1748232014
A Comprehensive Guide on RHEL Unix Commands 1748232014
Contents
1
2.9.3 Redirection & Pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.9.4 Word Count & String/Text Processing . . . . . . . . . . . . . . . . . . . . 9
2.10 Filesystem, Package Management, Search, Archiving . . . . . . . . . . . . . . 9
2.10.1 Filesystem & Inodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10.2 Disk Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10.3 RPM Package Management . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10.4 YUM Package Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10.5 File Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10.6 Archiving & Compression . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
This document provides a concise reference of commonly used UNIX commands and
their most frequently used flags, with a focus on Red Hat Linux environments.
2
grep -rin ”error” /var/log
3
1.8 mkdir — Make Directories
Description: Create new directories.
Common Flags:
• -p: Create parent directories as needed
mkdir -p /var/www/myapp/{logs,tmp}
4
1.13 kill — Send Signals to Processes
Description: Send signals to terminate or control processes.
Common Flags:
• -9: SIGKILL (force kill)
• -15: SIGTERM (default, graceful)
kill -15 1234
kill -9 $(pidof my_app)
5
1.17 wget — Non-interactive Network Downloader
Description: Download files from the web.
Common Flags:
• -O file: Save output as file
• -c: Continue incomplete download
• -r: Recursive download
• –no-parent: Don’t ascend to parent directory
wget -c http://example.com/large.iso
6
1.22 awk — Pattern Scanning and Processing
Description: Process and analyze text files, especially columnar data.
Common Flags:
• -F: Field separator
• ’{ print $1, $3 }’: Inline script to select fields
awk -F: ’$3 > 1000 { print $1 }’ /etc/passwd
7
2.4 Linux Components
• Kernel: Core OS component
• Shell: Interface between user and kernel (bash is most common)
• Terminal: Interface to type and view commands
• Shell Types: sh, ksh, csh, bash
8
• !!: Repeat last command
• !string: Repeat last starting with ”string”
• !n: Run command by number
• !-n: Run n commands back
• ^old^new: Replace text in previous command
9
• bzip2 file – Compress � .bz2
• bunzip2 file.bz2 – Decompress .bz2 file
• bzcat file.bz2 – View .bz2 file content
10
Command Explanation
uname Displays system name
uname -n Hostname of the system
uname -a All available system information
cal Calendar for current month
cal 5 2004 Calendar for May 2004
date Displays current date and time
Ctrl + c Interrupts a running command
Ctrl + d Ends input or logs out
man -k keyword Search manual pages by keyword
man -s keyword Manual section for keyword
whatis command One-line description of a command
command –help Show help for command
pwd Show current directory
cd /path Change to specified directory
cd .. Go up one directory
cd ~ Go to home directory
cd - Go to previous directory
ls List directory contents
ls -a Show all files including hidden ones
ls -l Long listing with permissions, size, etc.
ls -F Adds symbols to indicate file types
ls -ld dir Show details of the directory itself
ls -R Recursively list subdirectories
cat filename Show file content
more filename Scroll through file content
head -n filename Show first n lines of file
tail [-n +n] filename Show last n lines of file
touch filename Create an empty file
mkdir dir Create a new directory
mkdir -p dir/dir2 Create nested directories
rm filename Delete file
rm -i filename Ask before deletion
rm -r dirname Delete directory and contents
rmdir dirname Delete an empty directory
cp source target Copy file
cp -i Ask before overwriting file
cp -r Copy directory recursively
mv source target Move or rename file
11
Command Explanation
useradd username Add a new user
passwd username Set the user’s password
useradd -D Show default useradd settings
newusers filename Create multiple users from file
usermod -l newname oldname Change a username
usermod -L username Lock the user’s password
usermod -U username Unlock the user’s password
userdel [-r] username Delete user (optionally home dir)
chage [options] username Manage password aging policies
groupadd groupname Create a new group
groupmod [options] groupname Modify an existing group
groupdel groupname Delete a group
find / -nogroup List files with no valid group
gpasswd Manage group members/admins
newgrp group Switch to another group
groups List groups you belong to
su [-] [username] Switch user
su [-] [username] -c command Execute command as another user
whoami Show current effective user
id Show UID, GID, groups
id username Show UID, GID, groups of user
who Show who is logged in
w Show system usage and active users
finger Show user info
sudo Execute a command as another user
visudo Edit sudoers file safely
chown user file Change file ownership to user
chown user:group file Change file owner and group
chmod [modes] file Change file permissions
chmod u+x file Add execute permission to user
chmod a=rw file Set read/write for all
chmod 755 file Set permissions in octal
umask Show current default permission mask
umask 002 Set default file permission mask
Ctrl+Alt+F1 to F6 Switch to virtual consoles
shutdown -k now Send shutdown warning only
shutdown -h now Shut down and halt system
poweroff Power off system
init 0 Shut down the system
12
Key/Command Action
vi filename Open file in vi
vi -r filename Recover unsaved file
view filename Open in read-only mode
i Insert before cursor
a Append after cursor
o Open new line below
O Open new line above
A Append at end of line
I Insert at beginning of line
h, <-, Backspace Move left
l, ->, Space Move right
j, Down Arrow Move down
k, Up Arrow Move up
w Next word
b Previous word
e End of word
0 Beginning of line
G End of file
nG / :n Go to line number n
Ctrl+F/B Scroll forward/back
Ctrl+L Refresh screen
s Replace character
x Delete character
dw Delete word
dd Delete line
D Delete to end of line
n,nd Delete lines n through n
/text Search forward
?text Search backward
n, N Next/previous match
:%s/old/new/g Global replace
yy Yank line
p, P Paste after/before line
n,n co n Copy lines to line n
n,n m n Move lines to line n
:w Save
:w new_file Save as new file
:q! Quit without saving
:wq, :x, ZZ Save and exit
:set nu / nonu Show/hide line numbers
:set ic / noic Case insensitivity/sensitivity
:set showmode Show mode info
13
Command Explanation
nice [-n adjustment] Start a process with a specific niceness
renice priority -p PID Change priority of a running process
ps [options] Show process status
ps -e List all system processes
ps -f Full details
ps -u UID Processes of a specific user
top Live view of running processes
pgrep pattern Find processes matching a pattern
pgrep -l pattern List PID and process name
kill PID Send SIGTERM to a process
kill -SIGNAL PID Send specific signal
pkill process_name Kill processes by name
pkill -9 process_name Force kill process
Command Explanation
sleep 500 & Run process in background
jobs List background jobs
fg %job_number Bring job to foreground
bg %job_number Resume job in background
kill -STOP %job_number Pause background job
kill %job_number Kill background job
Command Explanation
command > file Redirect output to file (overwrite)
command » file Redirect output to file (append)
command < file Use file as input
2> file Redirect standard error to file
command 2> errs > results Error to errs, output to results
command1 | command2 Output of one command to another
ls -lR / | more Example: piped output to pager
ls -lR / | tee file Output to both file and screen
14
Command Explanation
wc [options] filename Word count utility
wc -c filename Character count
wc -l filename Line count
wc -w filename Word count
diff file1 file2 Compare two files
grep [options] pattern files Search for patterns
grep -i pattern Case insensitive search
grep -l pattern files List matching files
grep -n pattern files Matching lines with line number
grep -v pattern files Invert match (show non-matching lines)
grep -c pattern files Count matches
grep -w pattern files Match whole word only
tr [options] string1 string2 Translate characters
echo ”Hello” | tr ’A-Z’ ’a-z’ Convert to lowercase
cut -f3 -d: /etc/passwd Cut field 3 from /etc/passwd (colon-separated)
cut -c1-5 filename Cut characters 1 to 5
sort [options] file Sort file
sort -t: -k1 /etc/passwd Sort by first field (:)
sort -t: -k3 /etc/passwd Sort by third field
sort -t: -n -k3 -o out in Numeric sort by 3rd field (output to out)
Command Explanation
ls -i fname Show inode number of a file
ls -id / Show inode number of a directory
cp f1 f2 Creates a new inode for the copied file
mv f1 f2 Keeps same inode if in same filesystem
ln -s file linkname Create symbolic (soft) link
ln file linkname Create hard link (same inode)
Command Explanation
df -h Free space on mounted filesystems (human readable)
du -sh [dir] Space used by a directory
Command Explanation
rpm -i file.rpm Install RPM package
rpm -e package Remove package
rpm -U file.rpm Upgrade (remove old, install new)
rpm -F file.rpm Freshen (update if installed)
rpm -qa | grep x Search installed packages
rpm -qa –last List packages by install time
rpm –import key Import GPG key for package verification
15
Command Explanation
yum search keyword Search for packages
yum list package Show versions available/installed
yum list installed List installed packages
yum list available List repo packages
yum grouplist ”string” Show groups matching string
yum install package Install package and dependencies
yum localinstall /path/file.rpm Install local RPM file
yum remove package Uninstall a package
yum upgrade package Upgrade and remove old version
yum update package Update but keep old
yum provides file Find which package owns a file
yum repolist all Show all repos
yum clean all Clear YUM cache
Command Explanation
locate filename Fast search using database (needs updatedb)
updatedb Update locate database
find path -name ”filename” Find files by name
find path -size +10 Files larger than 10 blocks
find path -atime -7 Accessed within last 7 days
find path -mtime +5 Modified more than 5 days ago
find path -user user Owned by user
find path -type f Find regular files
find path -perm 644 Find files with permissions 644
Topic Description
Linux History Origins, kernel, distributions
Why Red Hat? Enterprise use, security, support
Installation Types Kickstart, graphical, text-based
System Components Kernel, shell, terminal
General Commands Basic navigation and file operations
User/Group Management Adding, modifying, removing users/groups
Permissions & Ownership chown, chmod, umask
Vi Editor Modes, navigation, editing, saving
Shell/Bash Environment Env variables, aliases, history
Process Management ps, top, nice, kill, job control
Redirection/Pipelines Output/input/error, chaining commands
Text Processing wc, grep, cut, sort, diff
Filesystem/Inodes Links, inodes, disk usage
RPM/YUM Package Management rpm/yum operations, searching, cleaning
File Search find, locate, updatedb
Archiving/Compression tar, gzip, bzip2, compress
16