0% found this document useful (0 votes)
7 views10 pages

RHCSA Course

This document provides a comprehensive guide on various Linux commands and their usage, including file manipulation, permissions, text processing, archiving, scripting, and process management. It covers commands for creating, copying, moving, and deleting files, as well as managing permissions and searching for files. Additionally, it includes instructions for creating scripts and managing system processes and logs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

RHCSA Course

This document provides a comprehensive guide on various Linux commands and their usage, including file manipulation, permissions, text processing, archiving, scripting, and process management. It covers commands for creating, copying, moving, and deleting files, as well as managing permissions and searching for files. Additionally, it includes instructions for creating scripts and managing system processes and logs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Tips

sudo cfdisk /dev/vdb #to begin the linux partition menu


FILES
Using apropos command, find out the the configuration file for NFS mounts
and save its name in /home/bob/nfs file. The apropos command searches
the manual page names and descriptions for a keyword. Here, we'll search
for "NFS mounts":
apropos "NFS mounts" #nfsmount.conf is the configuration file name for
NFS mounts
echo ‘nfsmount.conf’ > /home/bob/nfs file or use vi /home/bob/nfs
file to edit and add the line nfsmount.conf
echo "/swapfile" > /home/bob/swapfile #writes /swapfile into swapfile
Man pages not working use sudo yum update man-db then sudo mandb
to rebuild the man page db
ls -f /home/bob/data/ or ls –a <directory path> #list all the files in a
including hidden files, hidden files start with a dot eg .datafile, permissions,
last modified
ls –l #long listing format
ls –a –l <dir path> or ls –al <dir path> #displays long list, including
hidden files
ls –alh #adds human readable file size
absolute path always start with the root directory specified by /
cd .. always refer to the parent dir of our current dir, which is often the
home dir for most users
cd / #takes you from your previous or current dir to the root dir
cd - #takes you back to your previous or current dir
cd #always take you to the home dir
touch <file name> <file/dir path> # eg touch receipt /home/receipts
creates a file
cp –r <source path> <destination path>/ #copies a file from src to dst,
always end your dir path with /
cp –r /home/jane/receipt.pdf receipt/receipt_copy.pdf #will copy the
receipt.pdf to the receipt dir and rename it as receipt_copy.pdf
cp -p /home/bob/myfile.txt /home/bob/data/ #-p ensures that the file's
attributes (such as permissions, timestamps, and ownership) are preserved.
mv /home/receipt.pdf receipt/ #moves receipt to the receipt dir
mv receipt.pdf receipt_copy.pdf #renames the receipt.pdf file to
receipt_copy.pdf file
mv receipt/ oldreceipt/ #renames a dir and no need to use –r as in cp,
because mv auto does that
rm –r jane/ #to delete the jane dir
stat /dir_filepath/<filename> #info about the file, including its inode
number, inode stores metadata (permissions, access times etc) and storage
location details of all the scattered stored pieces of the file on the disk.
Inode points toall the block of data that make up the file
ln /path to target file /path to link file #hard links a single file to two
users or location, the hard link file will have 2 link when you run stat
<filename>
ln –s /path to target /path to link file # creates a soft/symbolic link in
the path to link file
ls –l or readlink <filename> #will show the soft link path, you soft link
to different file system unliks hard link

PERMISSIONS
chgrp <name of the group> <filename/Dir name> #change the group
of a file
chown <username> <filename> #change the ownership of a file to a
new user
chwon <username>:<groupname> <filename> #changes the user
and group of the file
chmod u+w <file_name or file/dir path> #set permission to a file for
the owner
chmod g+w <file_name or file/dir path> #set for group (chmod
g+rwx)
chmod o+r <file_name or file/dir path> #set for others
chmod u-w, g-r, o-x to remove permissions
find [dir/file path] [parameter, eg –name, -size] #search for file
find /home/darl –iname books #will find files named books with non-
case sensitive
find cmin -5 #file metadata changed in the last 5 mins
find mmin -5 #files modified in the last 5minutes
find mmin +5 #files modified in the last 5 minutes and beyond
find mmin 5 #files modified at exactly 5 minutes in the past
find –name ‘f*’ –o –size +5M #find files either beginning with f or larger
than 5M
find –not –name ‘f*’ or find \! –name ‘f*’ # will find files not beginning
with letter ‘f’
find –perm 664 #find files exactly with this permission
find –perm -664 #atleast
find –perm /664 #any of these permissions
Find files/directories under /var/log/ directory that the group can write to,
but others cannot read or write to it. Save the list of the files/directories
(with complete parent path) in /home/bob/data.txt file.
[bob@centos-host ~]$ sudo find /var/log -perm -g=w ! -perm -o=w ! -perm -
o=r > /home/bob/data.txt
[bob@centos-host ~]$ sudo find /var/log -perm -g=w -not -perm -o=w -not -
perm -o=r > /home/bob/data.txt
[bob@centos-host ~]$ chmod u+s /home/bob/datadir #set uid
[bob@centos-host ~]$ chmod g+s /home/bob/datadir #se gid
[bob@centos-host ~]$ chmod +t /home/bob/datadir #set sticky bit

VIEW AND MANIPULATe TEXT


cat /home/user.txt #to view brief text
tac /home/user.txt #inverts the numbering
tail –n 20 /var/log/dnf.log #displays the last 20 lines of a log
head –n 20 /var/log/dnf.log #displays the first 20 lines of a log
sed ‘s/canada/canda/g’ user.txt #stream editor (sed) replaces all
instance of canda with canada, s = substitute cmd, g = global and
means set will substitute canda globally i.e. all instances
sed –i ‘s/canada/canda/g’ user.txt #the cmd without i does change the
text but only shows us what the change will look like, to actually effect the
change add the –i for in place
#Replace all occurrence of string #%$2jh//238720//31223 with
$2//23872031223 in /home/bob/data.txt file.
sed -i 's|#%$2jh//238720//31223$|$2//23872031223|g'
/home/bob/data.txt
sed -i '500,2000s/enabled/disabled/g' /home/bob/values.conf
#changes from line 500 to 2000
sed –s ‘s/cut/cook/gi’ user.txt #the ‘I’ at the end will ignore case
sensitivity
cut –d ‘ ‘ –f 1 userinfo.txt #extracts the first character in each line –f 1
in the userinfo.txt document, where the character are separated with
spaces ie –d ‘ ‘ (delimiter)
cut –d ‘,’ –f 3 userinfo_geo.txt > /home/country.txt #extracts the
words/char in the 3rd field in a the userinfo_geo.text where words are
separated by commas
sort country.txt | uniq #to get unique entries in a document
diff –c file 1 file 2 #help compare 2 texts to see differences
diff –y file 1 file 2 and sdiff file1 file 2 #does side by side comparison,
highlighting differences
PAGERS
less /var/log/dnf.log #opens dnf.log in the less pager, use up/down
arrow key to navigate. To search use the /<search_word> and to make
the search non case sensitive use –i option, to move upwards to previous
results press shift+n
more /var/log/dnf.log
vim or vi /var/log/dnf.log #to type in text press i for insert, to save
press esc to be ensure you’re have left the insert mode and in cmd mode
press :wq, to exit without saving :q!
To search in vim use /<search_word> and to make the search non-case
sensitive use /<search_word>\c
To move to a particular line type :<line_number>
To copy a line of text in command mode go to the that line with
:<line_number> and then type yy (type y 2times), to paste type p
grep –i ‘centos’ /etc/os-release #’i’ option ignores case while
searching using grep
grep –r –i /etc #used the –r option to search a dir and sub-dir
grep –r ‘^sam’ /etc #returns lines that begin with sam
grep –r ‘sam$’ /etc #returns lines that end with sam
grep –r ‘c..t’ /home #will find all words with c__t and 2 char in btw, eg
coot, cuut, chut, parachute.
grep –r –w or –wr c.t /home will match single word c_t that are not part of
a bigger word with a single char btw them like parachute
grep ‘\.’ search.txt #will search for a period in the text, using the \
‘escape’ cmd will tell bash not to see period as a search operator
grep –Er ‘0+’ /etc = egrep –r ‘0+’ /etc #uses the extended regular
expression or egrep option to remove the need of escaping certain char like
‘.’ and ‘+’
egrep –r ‘0{3,}’ /var/log/sshd.log #matches atleat 3 zeros
egrep –r ‘10{3,}’ /var/log/sshd.log #start with one and at most 3
zeros
egrep –r ‘0{3}’ /var/log/dnf/log #matches exactly 3 zeros
egrep –r ‘0{3,5}’ /var/log/ #finds 3 or 5 zeros
egrep –r ‘cut|cup’ /home/ #matches cut or cup
egrep –ir ‘enabled?|disabled?’ /home/ #-i option means not case
sensitive, ? means d at the end is optional, it will match expressions with or
without d, meaning it will match enable and enabled
egrep –r ‘c[au]t’ /home/darl/ #matches cut or cat
egrep –r ‘/dev/ [a-z]*[0-9]?’ /var/log/ #matches /dev/ and letters then
numbers with the digit at the end optional bcoz of ?
egrep –r ‘http[^s]’ /etc/ #finds http

ARCHIVE
tar -cvf archive.tar file1 #v shows verbose, creates a tar file called
archive.tar for file1
tar -rf archive.tar file2 #appends file2 to the archive.tar
tar -tf archive.tar #always use the tar list cmd to check where your file
will be extracted
Create a file.tar archive of /home/bob/file directory under /home/bob
location.
[bob@centos-host ~]$ tar -cvf /home/bob/file.tar -C /home/bob file
tar -xvf archive.tar –C /tmp #tells tar to extract the file in the /tmp dir
tar -cvzf archive.tar.gz file1 #creates a compressed tar using gzip
tar -cjvf archive.tar.bz2 file1 #bzip tar
Create a bzip archive under bob's home named file.txt.bz2 out of
/home/bob/file.txt, but preserve the original file. At the end of the exercise
you should have both:
bzip2 -k /home/bob/file.txt
Create a compressed tar archive logs.tar.gz (under bob's home) of /var/log/
directory.
[bob@centos-host ~]$ sudo tar -cvzf /home/bob/logs.tar.gz /var/log/
tar cJf archive.tar.xz file1 #xz tar compressed file
tar caf archive.xz file2 #autocompress
Execute /home/bob/script.sh script and save all command output (both
errors/warnings and normal output) in /home/bob/output.txt file.
/home/bob/script.sh > /home/bob/output.txt 2>&1
Execute /home/bob/script.sh script and save all normal output (except
errors/warnings) in /home/bob/output_stdout.txt file
[bob@centos-host ~]$ /home/bob/script.sh
1>/home/bob/output_stdout.txt 2>/dev/null
sort userinfo.text > sorted.txt #one > will over write each input
sort userinfo.text >> sorted.txt #two >> will not overwrite the input
egrep –r ‘^The’ /etc/ 2>/dev/null #redirect all error msg from this cmd
bc <<<1+2+3+4 = 10 #the here string redirects the 1+2+3+4 to the
bash calc bc, instead of typing it on a keyboard
rsync –a /etc/ [email protected] :/home/jane/pictures #-a ensures
subdir and permissions are sync-ed too
sudo dd if=/dev/vda of=diskimage.raw bs =1M status=progress
#creates an image
sudo dd if=diskimage.raw of=/dev/vda bs=1M status=progress
#restores an image
scp [email protected] :/var/log/myremote.log /var/log/mylocal.log
scp [email protected]:<remote_path> <local_path> #secure copy from
remote to local
scp /home/darl/mylocal [email protected]:/home/darl/myremote
scp <local_path> [email protected]:<remote_path> #copy from local to
remote

SCRIPTS
How to create a script
touch script.sh #creates a script file
vim script.sh #open the file in vim, i to edit, esc+:wq to save,
esc+:q! to exit vim, add the following lines to create a log script
#! /bin/bash #must be the first line in the script file
date >> /tmp/script.log #redirects and appends the date info to your
script file
cat /proc/version >> /tmp/script.log #appends the system info to your
script file, esc+:wq to save and exit
chmod u+x script.sh #makes the script executable, u+x means only
the user can run the script
chmod +x script.sh #+x everyone can run the script
./script.sh #to run the script
[ ab ] && [ bc ] #and operator
[ab ] || [ cd ] #or operator
echo $? #is used to get the exit code of a cmd, run it immediately after a
cmd is run

PROCESSES AND LOGS


When searching inside man pages use /EXAMPLES to see different cmd
flags to use and how to use a cmd
ps –aux #will show all processes running including for other users
kill –L #lists signall options
jobs #checks if you have a program running in the background
egrep –r ‘ssh’ /var/log/ #can be used to search for ssh instances in the
lo g file
tail –F /var/log/secure/ #See real time log on what is happening
which sudo #shows you the path to the sudo log eg /bin/sudo
journalctl /bin/sudo #shows all log pertaining to sudo
journalctl –u sshd.service (journalctl –u <service_name) #lets you
view logs for a service
sudo lsof -u rtkit > /home/bob/files.txt
journalctl –f #live mode, follow log real time
ps lax #see processes with their nice value
ps aux | grep sshd

FILESYSTEMS
ls /mnt #/mnt dir used for temporarily mounting a random file systems
sudo mount /dev/sda1 /mnt #used to mount the file system
sudo umount -l /mnt #force unmount a file system mounted on /mnt
Superuser (root account): UID 0
System accounts (sshd, mail): home dir not under /home: UID < 100 and
500-100
Service account: (nginx)
Su <username> #to switch to another user
Su –c <cmd> #to run a cmd as root
Cat /ect/sudoers #only users listed here can use sudo
#to search for cmds that you can’t remember
Apropos <brief cmd phrase> eg apropos <director>
Su mandb
#to check local users or users logged on a system
W
Who #all logged in user
Cat /etc/password
Cat /etc/group #check groups
Red Hat removed the Docker container engine and the docker command
from RHEL 9.
#to confirm ownership of a directory
ls -ld /path/to/directory
#to check if a directory exist
test -d /path/to/directory && echo "Directory exists" || echo
"Directory does not exist"
# to navigate to localhost Home directory where you have desktop,
downloads, documents etc access it from the localhost session eg
[darl@localhost]# not from [root@localhost]#
Cd ~
#to access the desktop directory
cd ~/Desktop
# to create a directory called Alex in the Home directory
mkdir ~/Alex
#To access directories like Alex created in the user’s personal Home
directory eg like a Users Folder in windows use
cd ~/Alex
#To the general home directory like c drive in windows use, this is the
default home directory in bash
cd /Alex
#to check if an app is installed
rpm -qa | grep <name of the app or service>
#display installation logs
sudo tail -f /var/log/dnf.log
# monitor all package management activity (like apt, dnf, or rpm) by
checking the system journal.
sudo journalctl –f

You might also like