ShellScripts Flashcards

1
Q

My system is running very slow. What do i do ?

A

top command to show CPU utilization per core

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How many files are there in my current directory?

A

ls -lrt to see if . and .. come up and then accordingly ls -lrt|wc -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

I am getting an out of space email in one of my mounts, what do i do ?

A

du -ah |sort -rh|head -n 20 to see top 20 files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

I want to see how much space is left in a directory

A

df -ah

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

My directory has multiple sub directories, but i want to show much space is used only for my first 2 directories

A

du -ah –max-depth=2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

I want to find all files which contain the line “Published items “

A

grep ‘Published’|grep -v 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

I need to find a file named pwd.txt and i cant remember where it was. How do find it’s location?

A

find / -type f -name pwd.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

I don’t like the default command prompt, and want to change it to show the hostname/current directory

A

PS1=”[\h \w]”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do I see status of my last command ?

A

echo $? #0 means success

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do i force delete all files under a directory ?

A

rm -rf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do i run a long running command so that its not killed ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If i dont want to see a command’s error output , what do i do?

A

2>/dev/null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do i append a line to a file?

A

echo “line”&raquo_space; filename. #> to insert

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do i put a comment in a shell script

A

Use # like a shebang script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do i find PID of a process named mysql ?

A

ps -ef|grep mysql|cut -f4 -d” “ # 4th column separated by space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you find all processes started by a user ?

A

-u

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

You have a directory contqaining many files and directories.
You want to search for a line say “CRIT”. How will you search it ?

A
#cd 
grep -r CRIT .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to search for a tag in binary files ?

A

grep -a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to show only filenames ?

A

grep -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to read compressed files in a RO shell

A

zgrep

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

I want to shutdown/decommission a host. I want to see which files are still being written on the server.

A

find . -mtime -7 -ls. #-ls to give more details

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

If my system is showing OoM exceptions, what do i do ?

A

Can check free memory by free -h, then if more memory is available, can provide more memory to the script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What command should i use to scroll throgh production log ?

A

less +F

Control C to search mode
Control F to watch mode , F = follow

24
Q

How do i go to first line using less

A

g / G : first / lass line

/searchfoward. ?searchbackward

space b to move one page down

25
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'
26
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
27
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
28
``` 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
29
``` 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
30
Which command gives me details about all users who are logged in ?
finger | finger
31
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
32
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
33
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
34
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
35
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
36
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
37
What is diff. b/w absolute and relative path?
Ab. starts with / root | Relative is relative to your current location
38
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.
39
How do i check all IPs used by a host ?
use ifconfig -a or ipconfig -all (windows)
40
How do i copy files from one host to another ?
scp or ftp cp to copy in the current host
41
How do i rename a file ?
mv
42
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
43
How do i remove a directory ?
rmdir
44
How do i get 99th line of file ?
tail -n +99 filename|head -1 cat filename|cut -c 99-100
45
How do i identify blank lines ? How do i not show them ? How do i count them
grep '^$' | grep -v '^$' | grep -v '^$' |grep -c
46
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
47
How do i show match + frequency for CRID in log files
grep -c CRID
48
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 %
49
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
50
``` $* 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 ```
51
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
52
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
53
How do i check for multiple patterns ?
egrep '|' |'
54
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
55
If i want to print any metacharacters, how will i do it ?
either enclose it in quotes or escape it using a \