ShellScripts Flashcards
My system is running very slow. What do i do ?
top command to show CPU utilization per core
How many files are there in my current directory?
ls -lrt to see if . and .. come up and then accordingly ls -lrt|wc -l
I am getting an out of space email in one of my mounts, what do i do ?
du -ah |sort -rh|head -n 20 to see top 20 files
I want to see how much space is left in a directory
df -ah
My directory has multiple sub directories, but i want to show much space is used only for my first 2 directories
du -ah –max-depth=2
I want to find all files which contain the line “Published items “
grep ‘Published’|grep -v 0
I need to find a file named pwd.txt and i cant remember where it was. How do find it’s location?
find / -type f -name pwd.txt
I don’t like the default command prompt, and want to change it to show the hostname/current directory
PS1=”[\h \w]”
How do I see status of my last command ?
echo $? #0 means success
How do i force delete all files under a directory ?
rm -rf
How do i run a long running command so that its not killed ?
nohup &
nohup will keep running until it’s done, even if the user that started it logs out.
& to run a process to the background
NO HANGUP - nohup
If i dont want to see a command’s error output , what do i do?
2>/dev/null
How do i append a line to a file?
echo “line”»_space; filename. #> to insert
How do i put a comment in a shell script
Use # like a shebang script
How do i find PID of a process named mysql ?
ps -ef|grep mysql|cut -f4 -d” “ # 4th column separated by space
How do you find all processes started by a user ?
-u
You have a directory contqaining many files and directories.
You want to search for a line say “CRIT”. How will you search it ?
#cd grep -r CRIT .
How to search for a tag in binary files ?
grep -a
How to show only filenames ?
grep -l
How to read compressed files in a RO shell
zgrep
I want to shutdown/decommission a host. I want to see which files are still being written on the server.
find . -mtime -7 -ls. #-ls to give more details
If my system is showing OoM exceptions, what do i do ?
Can check free memory by free -h, then if more memory is available, can provide more memory to the script
What command should i use to scroll throgh production log ?
less +F
Control C to search mode
Control F to watch mode , F = follow
How do i go to first line using less
g / G : first / lass line
/searchfoward. ?searchbackward
space b to move one page down
My file has 1010 many times. i want to replace all of them with 0101, how do i do it ?
I want to replace only those occurrences where the line starts with 1010.
So if the line is
1010client1 -> this i want to replace
21010clietn2 -> this i dont want to replace
sed -i.bak -r 's/1010/0101/g' #g to replace all / gloably #-i.bak to take a backup, if you just give -i, no backups are taken.
sed -i.bak -r ‘s/^1010/^0101/g’
What do the following regular expressions signify ?
^ $ /t /r /n . * \+
^ beginning of line $ end of line /t tab /r : carriage return /n : new line usually based on OS - it uses a combinatin of /r/n for new lines
. Matches exactly 1 character
* Matches 0 or more
+ matches 1 or more
What is diff bw more and less
both used to scroll page by page
more is old utility, loads whole file at once , hence slower
If a file dummy.txt has the following lines : amrita anirban amrita anjan
and i want to remove duplicates, what will i do ?
sort dummy.txt|uniq
or sort -u dummy.txt
Uniq only removes adjacent duplicates, so need to sort first
uniq -c shows frequency , -d shows dupes only , -u shows unique ones only
What if the file has amrita 3 anirban 2 ketan 4 anjan 1
and i want to sort by the 2nd column such that ketan 4 amrita 3 anirban 2 anjan 1
sort -k 2 -nr
2nd column numerically reverse
Which command gives me details about all users who are logged in ?
finger
finger
How do i login to a remote server ?
I can use putty which is a client for SSH and telnet.
I will use SSH as its encrypted
Control + ] , close on telnet command prompt
How do i repeat the last command
How do i go to the last directory ?
Home directory ?
!!
cd -
cd ~. or just cd will also take me to home directory
How do i see all environment variables?
env
Usually they are all defined in caps
so if i see echo $NAME it usually means NAME is an ev variable.
An environment variable is available to any child process of the shell
i can run cat or ls or any inbuilt command without giving their locatin. How ?
Because these are inbuilt executables and their path /locaiton is already configured in PATH environment variable
If i have my location of scripts, and i don’t want to always invoke them using their full path, what do i do ?
I can put all scripts in a directory say utilityscripts
and i can add the directory to the PATH
echo $PATH # /bin:/usr/bin
PATH=$PATH:
directories on path are separated by : in linux, by ; in windows
If i deploy a package, and i dont want to constantly change the location, what will i do ?
I will create a symlink or a symbolic link
for eg. ln -s
Use unlink to unlink
What is diff. b/w absolute and relative path?
Ab. starts with / root
Relative is relative to your current location
Client complains too slow, cant connect what do i do ?
Ask them to share
traceroute -d or tracert
Maybe they are connecting to incorrect data centers
or there is a high latency hop in between.
How do i check all IPs used by a host ?
use ifconfig -a or ipconfig -all (windows)
How do i copy files from one host to another ?
scp
or ftp
cp to copy in the current host
How do i rename a file ?
mv
How do i check how many cores my host has ?
lscpu
Will tell me how many cores(CPUs) means how many tasks can run in parallel.
Also tells processor’s clock speed in GHz.
A computer’s processor clock speed determines how quickly the central processing unit (CPU) can retrieve and interpret instructions
How do i remove a directory ?
rmdir
How do i get 99th line of file ?
tail -n +99 filename|head -1
cat filename|cut -c 99-100
How do i identify blank lines ?
How do i not show them ?
How do i count them
grep ‘^$’
| grep -v ‘^$’
| grep -v ‘^$’ |grep -c
What is the diff b/w
tail -n 5
and tail -n +5
tail -n 5 shows the last 5 lines
tail -n +5 shows from the 5th line
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] cat testless.txt
1 2 3 3 4 5 6
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] tail -n 5 testless.txt
2 3 4 5 6
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] tail -n +5 testless.txt
5
6
How do i show match + frequency for CRID in log files
grep -c CRID
What is a shell ?
What is a kernel ?
Shell is an interface where we can write commands to communicate with the kernel.
Kernel is the OS , manages resources.
$ or % is the command prompt Different types of shell like Bourne (sh) Bourne Again (bash) Korn (ksh)
C shell - will have %
If i have some common commands like PS1 etc. where do i keep them so that i dont have to write them everytime?
.profile OR .bashrc OR … which is executed during shell initialization
$* S? $1 $2 $0 $# \$\$ $!
$* All arguments S? Return status , 0 is successful $1 $2 First , 2nd argument $0 File name $# Number of arguments \$\$ Current PID $! shows the process id of the process that recently went into background
If you have to find how many files in a directory how will you find it ?
ls -lrta | wc -l will give you 2 more
$(($(ls -lrta|wc-l)-2))
$(()) to run arithmetic expressions : expression substitution
$() to run command : command substituion
How do i check equality ?
Check string
Check numbers
if [[ $1 == 1 ]]
= and == are for string comparisons, -eq is for numeric ones, but if you use [[ can use < etc
[[ is bash’s improvement to the [ command
How do i check for multiple patterns ?
egrep ‘|’ |’
How do i check for or or and conditions ?
aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] [[ 1 > 0 || 2 < 1 ]]; echo $?
0
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] [[ 1 < 0 || 2 < 1 ]]; echo $?
1
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] [[ 1 > 0 && 2 < 1 ]]; echo $?
1
[aniamritapc ANIAMRITAPCs-MacBook-Pro /tmp $ ] [[ 1 > 0 && 2 > 1 ]]; echo $?
0
If i want to print any metacharacters, how will i do it ?
either enclose it in quotes or escape it using a \