UNIX-1.10 Customize Command Prompt


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

set -o vi – puts your command prompt into VI mode, allows you to execute VI commands
/command – search for command history, n for next match found, N for previous match found
PS1 – variable that holds text of your prompt
PS1=”\H” – displays host name
PS1=”\s” – name of shell
PS1=”\u” – current username
PS1=”\w” – current working directory
PS1=”\@” – current time
PS1=”[\\u@\\H \\W \\@]\\$” – try this
PS1=”\e[x;ym$PS1\e[m” – \e[ starts the color mode, x;ym specifys color, \e[m ends color mode. x;y can be 0;30 to 0;37
case $(id -u) in
0) PS1=”${PS1}# “;;
*) PS1=”${PS1}$ “;;
esac
alias name=”command -options” – creating alias/shortcut commands
alias vi=’vim’
alias ls=’ls -F’
alias ll=’ls -l’
alias la=’ls -la’
alias cp=’cp -ip’
alias mv=’mv -i’

Advertisements

UNIX-1.8 ps, netstat, kill, pidof, fuser, awk commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

ps -fu username – displays all processes started by user
ps -ef – display all processes running
ps -ef | grep ‘pname’ – look for specific process
netstat -t – see all tcp connections
netstat -an – see all network connections
netstat -c – netstat will execute every second
netstat -p – display processes that use these connections
kill pid – kills process by pid
kill -9 pid – force kill process by pid
& – execute process in background
free – show memory usage, alternatively use top
pidof command – shows process id for running process/command
fuser -vu dir – displays all processes that are using dir, along with owner id of processes
echo “hello world” | awk ‘{ print length($1) }’ – prints length of field
echo “hello world” | awk ‘{ print substr($1, 3) }’ – prints llo, substring function
echo “hello world” | awk ‘{sub(regex, replacement); print}’ – replace pattern on line and print
echo “HELLO world” | awk ‘{ print tolower($1) }’ – lower case field 1
echo “hello world” | awk ‘{ print toupper($1) }’ – upper case field 1

UNIX-1.6 grep, useradd, usermod commands


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

last -10 username – get last 10 logins of specific user
passwd – change password
diff -y file1 file2 – find differences between 2 files
grep ‘regex’ file – search for pattern
grep -i ‘regex’ file – case insensitive search
grep -P ‘regex’ file – full support of Perl regex
grep -e ‘regex1’ -e ‘regex2’ file – search for multiple patterns
grep -v ‘regex’ file – inverse of search pattern (return non-matching lines)
grep -n ‘regex’ file – display line number with search result
grep -B4 ‘regex’ file – also display 4 lines before match
watch -n5 command – executes command every 5 second
adduser username – add a new user
usermod username -d /home/newhome – change home for new user
usermod username -d /home/newhome -m – change and move home content to a new home
usermod username -e 2022-05-11 – set account expiration date
usermod username -g group_name – set user group name/id
usermod username -l new_username – change username for user
usermod username -p new_password – change password for user
usermod username -s shell_name – change default shell for user
sudo -u username -i – run future commands as another user

UNIX-1.5 Awk, Cut, Last, Wc Commands Video Tutorial


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

awk -F’|’ ‘BEGIN{ initialization } { code block } END{ finalization }’ – general syntax
default delimiter is space ‘ ‘
VAR=”value” – define a variable
print VAR – print a variable
NF is number of fields
$NF is value of last field
print “‘$EXTERNAL_VAR'” – double single $EXTERNAL_VAR single double
VAR~/pattern/ – pattern patching
VAR!~/pattern/ – does not match
FS returns value of field separator, same as -F
OFS returns output field separator
print $1,$2 is separated
NR returns line number
FILENAME returns current filename
awk -v var1=value1 – pre-assign values to variables in awk
awk -F, ‘conditions { code block }’ filename – condition must be right before code block

cut – get columns from file
cut -d, -f1-2 filename – print 1st to 2nd columns
cut -c1-5 filename – print first 5 characters
cut -d, -f2- filename – print from 2nd column to last column
cut -d, -f1,4,5 filename – print 1st, 4th and 5th column
cut -d, -f-3 filename – print from 1st to 3rd column
cut -d, -f1,3-5 filename – print 1st, 3rd to 5th column

wc – word count
wc -l – line count
wc -c – print number of bytes
wc – print number of newline, word and bytes
wc -m – character count
wc -w – word count

last – check login history
last -10 – show last 10 logins
last -F – show full login information

UNIX-1.3 UNIX Commands and Options


Make sure to subscribe to my channel for more videos on UNIX, Perl and SQL.
facebook: facebook.com/fuzicast
twitter: @fuzicast
youtube: youtube.com/yuejdesigner85

du – data usage
du -sh – find how much disk space used in current directory

df – data file system
df -h – find disk space usage

cp – copies files
cp file1 file2 – copy file1 to file2
cp file1 dir/file2 – copy file1 to file2 in dir
cp file1 dir/ – copy file1 to dir with same filename
cp -rf dir1 dir2 – recursively copy dir1 to dir2
cp -i file1 file2 – prompt before overwriting file2
cp file1 file2 file3 dir1 – copy multiple files to a directory
cp -p file1 file2 – by default, permission is not preserved, use -p to do that
cp -s file1 link1 – create soft link to file1

mv – change/move files
mv file1 file2 – change filename from file1 to file2
mv -i file1 file2 – prompt before overwriting
mv file1 dir1 – move file1 to dir1
mv dir1 dir2 – move dir1 to dir2 if dir2 exists, else change name from dir1 to dir2

head – display lines in file from beginning
head file1 – display first 10 lines of file1
head -100 file1 – display first 100 lines of file1

tail – display lines in file from end
tail file1 – display last 10 lines of file1
tail -100 file1 – display last 100 lines of file1

rm – remove file or directory
rm file1 – remove file1
rm -rf dir1 – remove entire directory, never prompt
rm -i dir1 – prompt before removing

mkdir – create directory
mkdir dir1 – create dir1
mkdir -m777 dir1 – create dir1 with specified permission
mkdir -p dir1 – ignore error if dir1 already exists

tar – archive file
tar zcvf tarfilename.tar file1 file2 file3 – archive all these files into one tar ball, compressed
tar xvf tarfilename.tar – extract files from a tar ball
tar rf tarfilename.tar extra.txt – append extra.txt to existing tarfilename.tar (tarfilename.tar is not compressed)
tar tvf tarfilename.tar – list out files in tar ball

gzip – compress file
gzip file1 – compress file1, new filename will have .gz extension
gzip -d file1.gz – decompress file1.gz

chmod – change permission
chmod 777 file1 – change permission for file1 (rwxrwxrwx)
chmod +x file1 – give executable permission for all to file1 (+r, +w)
chmod u+x file1 – give executable permission for owner to file1 (g+x, o+x)
chmod -R 777 dir1 – change permission for all contents in dir1