You are on page 1of 10

ASSIGNMENT -LINUX COMMANDS

1. move files from one folder to respective folders


-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder
$ nano folder

#!/bin/bash -x
for file in 'ls*.txt';
do
folder='echo $file | awk -F. '{print $1}''
if[-d $folder]
then
rm -r $folder;
fi
#echo creating folder ($folder);
mkdir $folder;
cp $file $folder;
done

$ chmod +x folder
$ nano folder
$ ls

OUTPUT :

abc/ abc.txt def/def.txt ghi/ghi.txt jkl/jkl.txt


-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
2.check if a folder exists or not. if it is not present, create it

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder


$ nano test1
#!/bin/bash
if [-d ~/currentfolder ]
then
echo "the folder exists"
else
echo "the folder doesnot exists"
mkdir currentfolder/
fi
$ chmod +x test1
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder
$ . /test1

OUTPUT:
the folder doesnot exists
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
3.append current dates to all log files name which has extension .log.1 from a
folder

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder


$ touch abc.log.1 def.log.1 ghi.log.1 jkl.log.1 mno.log.1
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder
$ ls -l
total 1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:18 abc/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:30 abc.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 def/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:30 def.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 ghi/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:30 ghi.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 jkl/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:30 jkl.log.1
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:30 mno.log.1
-rwxr-xr-x 1 vamshi krishna 197121 118 Oct 15 14:10 test1*

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder


$ touch -am abc.log.1 def.log.1 ghi.log.1 jkl.log.1 mno.log.1

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder


$ ls -l
total 1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:18 abc/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:33 abc.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 def/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:33 def.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 ghi/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:33 ghi.log.1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 15 13:19 jkl/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:33 jkl.log.1
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 14:33 mno.log.1
-rwxr-xr-x 1 vamshi krishna 197121 118 Oct 15 14:10 test1*

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/currentfolder


$ for file in 'ls *.log.1'
> do
> filename=echo $file |awk -F. '{print $1}'
> echo $filename"'date +%Y%m%d'"
> done

OUTPUT:

abc.20201017.log.1
def.20201017.log.1
ghi.20201017.log.1
jkl.20201017.log.1
mno.20201017.log.1
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

4. print list of last FOUR unique sorted client IP


$ awk ````` {print $1}` access.log | sort | uniq | tail -4
OUTPUT:

10.56.19.3
10.56.0.3
10.56.21.2
10.56.1.3
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--

5.set environment user secret ="dH34xJaa23"


$export usersecret=$(echo "dH34xJaa23")
$printenv usersecret
$dH34xJaa23
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
6.execute command "hello" and 'ls"
$ hello
bash: hello: command not found

$ ls
access.log debug.log linux_problem_sheet.pdf
data.csv linux_chit_sheet.pdf README.md

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ echo $? ls
0 ls
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---

7.find a word "systemd" from all log files in the folder /var/log
$ cd /var/log
$ ls
$ls *.log | grep systemd
0
--------------------------------------------alternate
way--------------------------------------------------------------------------------
----------------------------
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)
$ grep -o -i system access.log | wc -l //using
access.log//
6

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ grep -c 'system' access.log
6
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--

8.find the differences between original file and the updated file

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ touch originalfile.sh

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ cp originalfile.sh original

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ touch updatedfile.sh

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ cp updatedfile.sh updated

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ mv original original-backup
$diff -q -r original updated

only in original : originalfile.sh


only in updated: updatedfile.sh

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ mkdir original-backup
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~
$ cp original original-backup

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


diff -q -r updated originalbackup

//SINCE THERE IS NO DIFFERENCE BETWEEN UPDATED AND ORIGINALBACKUP,HENCE OUTPUT IS


NOT GENERATED//

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------

9.DATA ANALYSIS PART

printing aggregate base pay

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ grep -i CAPTAIN data.csv | awk '{sum+= $4} END {print sum}'
468427

aggregate total pay OF CAPTAIN

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ grep -i CAPTAIN data.csv |awk '{sum+= $7} END {print sum}'
1171796
AVERAGE BASE PAY
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)
$ cat data.csv | awk '{sum+=$4} END {print sum/NR}'
157972

PRINT JOB TITLE AND OVERTIME PAY BETWEEN 7000 AND 10000

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat data.csv | awk '{if($5>7000 && $5<10000) print $3" "$5}'
DEPUTYCHIEF 9737
ASSTDEPUTY 8601

print employee name and total pay whose base pay >10000

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat data.csv |awk '{if($4>1000) print $2" "$7}'
EmployeeName TotalPay
NATHANIEL 567595
GARY 538909
ALBERT 335279
CHRISTOPHER 332343
PATRICK 326373
DAVID 316285
ALSON 315981
DAVID 307899
JOANNE 302377
PATRICIA 297608
EDWARD 294580
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------

10. print list of last 10 unique sorted client IP from access.log

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat access.log |awk '{print $1}' |sort| uniq | tail -10
10.56.2.2
10.56.22.3
10.56.19.3
10.56.19.3
10.56.4.2
10.56.3.4
10.56.4.2
10.56.0.3

PRINT UNIQUE CLIENT IPS

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat access.log |awk '{print $1}' | sort | uniq | tail -10
10.56.0.3
10.56.1.3
10.56.10.2
10.56.19.3
10.56.2.2
10.56.21.2
10.56.22.3
10.56.3.4
10.56.34.4
10.56.4.2
10.56.44.4
10.56.46.2
10.56.5.2
10.56.6.4
10.56.9.3

PRINT UNIQUE CLIENT IPS

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat access.log |awk '{print $1}' | sort | uniq | tail -4
10.56.46.2
10.56.5.2
10.56.6.4
10.56.9.3
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------

11. PRINT LIST OF WEB RESPONSE CODE COUNT IN THE UNIQUE SORTED ORDER AT SPECIFIC
HOURS

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat access.log |awk '{print $9}' | sort -nr| uniq -c | tail -4
3176 200
26 304
8 206
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------

12. PRINT LIST OF LAST 4 FREQUENTLY ACCESS UNIQUE URLS AT PARTICULAR HOURS
$ cat access.log |awk '{print $20}' | cut -d" " -f 9,10 |sort| uniq -c | tail
-4
455 Chrome/77.0.3865.90
177 like
33 Firefox/69.0"
7 Ubuntu/10.04
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------
13. archive the files that were modified 7 days ago and move them to the backup
folder

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content


$ find . -iname "*.java" -atime -1 -type f -exec mv {} backup \;
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content/backup
$ ls -al
total 6
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 17 17:46 ./
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 17 17:46 ../
-rw-r--r-- 1 vamshi krishna 197121 98 Oct 16 20:08 hello.java
-rw-r--r-- 1 vamshi krishna 197121 256 Oct 16 20:11 Helloworld.java
----------------------------------alternate way
------------------------------------------------------------------
vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands
$ ls -l
total 10
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 8 16:29 abc.txt
-rw-r--r-- 1 vamshi krishna 197121 31 Oct 12 16:37 catlearning.txt
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 16 19:35 done
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 16 19:35 echo
-rw-r--r-- 1 vamshi krishna 197121 188 Oct 15 19:38 grepcommandfile.txt
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 12 16:01 hereiwillmove/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 12 15:59 hereiwillremove
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 18 15:13 linux-content/
-rw-r--r-- 1 vamshi krishna 197121 3838 Oct 12 16:15 myhistory.txt
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 13 15:39 SS/
-rw-r--r-- 1 vamshi krishna 197121 0 Oct 15 19:46 temp1
drwxr-xr-x 1 vamshi krishna 197121 0 Oct 7 20:46 terminalcommands/

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands


$ find . -iname "*.txt" -mtime -7 -type f -exec tar -cvf test.tar {} \;
./catlearning.txt
./grepcommandfile.txt
./hereiwillmove/move.txt
./linux-content/abc.txt
./linux-content/def.txt
./linux-content/etcpassword.txt
./linux-content/krishna.txt
./linux-content/vamshi.txt
./myhistory.txt

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands


$ ls
abc.txt grepcommandfile.txt myhistory.txt test.tar
catlearning.txt hereiwillmove/ SS/
done hereiwillremove temp1
echo linux-content/ terminalcommands/

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands


$ mv test.tar archive
$ ls
test.tar

-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------

14. CREATE A PROCESS LIST TABLE DISPLAYS PID PPID AND STIME

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ ps
PID PPID PGID WINPID TTY UID STIME COMMAND
1021 1 1021 4800 ? 197610 14:12:32 /usr/bin/mintty
1022 1021 1022 2524 pty0 197610 14:12:33 /usr/bin/bash
1131 1022 1131 7156 pty0 197610 15:05:02 /usr/bin/ps

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~


$ ps |awk '{print $1" "$6" "$7}' | head -3
PID UID STIME
1021 197610 14:12:32
1022 197610 14:12:33
1126 197610 15:03:48

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------

15. get user info from etcpassword.txt and change the user

$ cat > etcpassword.txt


vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)
$ cat etcpassword.txt |awk | '{print $1}'
//print first field//

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content (master)


$ cat etcpassword.txt |awk '{if($4>1000) print $4}' //print used ids >1000

Service:/var/db/appstore:/usr/bin/false
Server:/Library/WebServer:/usr/bin/false
Helper:/var/db/lockdown:/usr/bin/false
Ticket:/var/empty:/usr/bin/false
Service:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Anonymous:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Daemon:/var/db/ondemand:/usr/bin/false
Service:/var/empty:/usr/bin/false
Daemon:/var/db/findmydevice:/usr/bin/false
User:/var/db/cmiodalassistants:/usr/bin/false
Daemon:/var/db/nearbyd:/usr/bin/false

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content


$ cat etcpassword.txt |awk '{if($3>200) print $3}' //print userids>200//

Daemon:/var/empty:/usr/bin/false
Server:/var/spool/postfix:/usr/bin/false
Service:/var/empty:/usr/bin/false
Service:/var/empty:/usr/bin/false
Daemon:/var/db/geod:/usr/bin/false
Desktop:/var/empty:/usr/bin/false
User:/var/empty:/usr/bin/false
separation:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Service:/var/db/softwareupdate:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Agent:/var/empty:/usr/bin/false
Agent:/var/empty:/usr/bin/false
Service:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Kerberos:/var/empty:/usr/bin/false
Service:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Account:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
User:/var/db/hidd:/usr/bin/false
Daemon:/var/db/timed:/usr/bin/false

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content


$ cat etcpassword.txt |awk '{print $2}'
//print the second field//

User:/var/empty:/usr/bin/false
Administrator:/var/root:/bin/sh
Services:/var/root:/usr/bin/false
Services:/var/networkd:/usr/bin/false
Assistant:/var/empty:/usr/bin/false
Services:/var/spool/cups:/usr/bin/false
AppLaunch:/var/empty:/usr/bin/false
Daemon:/var/empty:/usr/bin/false
Documentation:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Server:/var/empty:/usr/bin/false
Administrator:/var/imap:/usr/bin/false
Server:/var/empty:/usr/bin/false
Daemon:/var/virusmails:/usr/bin/false
Daemon:/var/virusmails:/usr/bin/false
Owner:/var/empty:/usr/bin/false

vamshi krishna@DESKTOP-VPAS95C MINGW64 ~/terminalcommands/linux-content


$sudo chown vamshi etcpassword.txt //changing the ownership of etcpassword.txt//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------

./evenoddseries
+ NUMS='1 2 3 4 5 6 7 8 9 10'
+ for NUM in $NUMS
++ expr 1 % 2
+ q=1
+ '[' 1 -eq 0 ']'
+ echo 'odd number'
odd number

#! /bin/bash -x
b=`expr 9 \* 3 + 8 - 5`
z=$(( b ))
echo $z

You might also like