Unix Commands - alaghusundhar/RHCSA GitHub Wiki

Welcome to the RHCSA wiki! Unix Commands

Command use to get the hostname

hostname or hostnamectl

File to change the hostname

sudo vi /etc/hostname

Command to check the previously access directory

Cd -

Command to check the user home directory

Cd ~

Command to create the parent and child Subdirectores

Mkdir -p 1/2/3/4/5/6

Command to delete a empty directory

rmdir

Command to forcefully delete the directory and its subdirectories

rm -rf

Command to safely delete a file since the below command will delete only after our confirmation rm -i file1.txt

Command to check the history of the commands used

history

Command to execute a command from the history

!40 (40 is the line number from the history command )

Command to check the flavor of machine installed uname -a

Command to check the system date

date

Command to check the dates in different formats

date +%D. (Instead of D , we can give anywhere from A to z for different options)

eg date +%d-%m-%Y will return in format 03-04-2020 for April 3rd

Command to create a file name with the date appended

touch sample-date +%d-%m-%Y. (Please note its not a single quotes, its the quotes in the ~ key)

Command to create files with series of numbers

touch countfile-{1..10}.txt

Command to Open the file with the LineNumber

cat -n catfile.txt

Command to Open the file with the LineNumber without the empty lines

cat -b catfile.txt

Command to Copy the list of extension to a directory

cp *.txt directorytwo

Command to Copy the list of extension files to a directory with persisting the timestamp and file data

cp -pv *.txt directorytwo

Command to Copy the complete directory content to another directory cp -Rv directorythree directoryfour

Command to delete the history

History -c

Command

who, who -d, who -r , who -a, w

Ps -aux

Command to check the process running by a specific user

ps -U vagrant

-rw-rw-r-- 1 vagrant vagrant 0 Apr 4 12:45 countfile-5.txt

rwx - User rwx - Group rwx - Others

chmod u+rwx countfile-10.txt chmod g+rwx countfile-10.txt chmod o+rwx countfile-10.txt

Command to change the group of the file name sudo chgrp root countfile-10.txt

Command to Zip / Tar the file name

tar -cvzf etc.tar.gz /etc*

Command to Extract tar -xvf etc.tar.gz

Command to find the files throughout the server

find / -iname etc.tar.gz

stdin - 0, stdout -1, stderr - 2

Command to Redirect the output to a file . THe same can be done for redirecting input and error as well with respective param ie 0/2 find /home/vagrant -iname catfile.txt 1> abcd.txt

Command to redirect both output and error to a file find /home/vagrant -iname catfile.txt > abcd.txt 2>&1

Command to Search for a keyword and print next 2 lines in the file

grep First -A 2 grepexercise.txt

Command to search for a keyword and print 1 line above and below from the file

grep THIS -C 1 grepexercise.txt

grep THIS -B 1 grepexercise.txt

grep THIS -A 1 grepexercise.txt

Command to search recursively

grep -ril /home/vagrant

Command to change the Color of the grep results

export GREP_COLOR='1;30;43'

Command to grep for the number of occurrences of keyword in the file

grep -ci is grepexercise.txt

grep -l this grep*