Zip/Unzip a file
To zip a file.
> gzip fileName.txt > After zipped, the file will have extension 'gz'. Such as fileName.txt.gz
To unzip a file.
> gunzip fileName with the extention 'gz' , this will unzip the file.
In Progress....
Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts
find command in UNIX
find command in UNIX ---
searches a file or folder
find -name fileName.txt
--- will
search the file in the current directory/folder and its sub folders.
or
find . -name
fileName.txt
or
find ./ -name
fileName.txt
Output:
./ fileName.txt
find ./folder1 -name
fileName.txt --- will search the
file in folder1 and its sub folders.
Output:
./folder1/ fileName.txt
find ./ -name '*.txt' --- displays all the ".txt " files in the
present folder and its sub folders.
or
find -name '*.txt'
Output:
Output will be something like this :
./file1.txt
./folder1/file2.txt
./folder1/folder2/file3.txt
. ..
find -empty --- search empty files and empty directories,
including hidden ones, from present folder and sub folders.
Output:
Output will be something like this :
./file1
./folder1/file2.txt
./folder1/folder2/file3.txt
./folderEmpty
. ..
find -perm 777 --- search file with
the permission 777 in present folder and sub folders.
Output :
Output will be something like this :
./file1
./folder1/file2.txt
./folder1/folder2/file3.txt
.. ..
ignore case sensitivity :
find -name file1
Output ------ ./file1
find -name File1
Output ------ ./File1
find -iname File1
Output ------
./file1
./File1
find -type d ---- finds directories
only in present folder and sub folders, includes hidden also.
find -type d -name 'dir*' --- finds directories
in present folder and sub folders , name starting with "dir" ,
includes non hidden directories inside hidden directories also but not hidden folder alone.
find -type d
-name '. *' --- finds hidden directories only in present folder and
sub folders.
To create hidden
directory : mkdir .folderName
To switch to
hidden directory : cd .folderName
To switch back
from the hidden folder : cd ..
find -type f --- find only files in present folder and sub folders.
find . -type f -name "FileName1" -exec rm -f {} \; --- deletes the file
FileName1 from present directory and its sub directories.
find -user Ram --- finds files and folders belonging to the user Ram in
present folder and sub folders, use
" -type f" if you want only files.
find -group AAA --- finds files and folders belonging to the group AAA in
present folder and sub folders, use
" -type f" if you want only files.
file modified/accessed
n days before :
find . -type
f -mtime 5 --- displays files modified
5 days before.
find . -type
f -atime 5 --- displays files accessed
5 days before.
AND operation:
find . -type f -mtime 5 -atime
10
OR operation:
find . -type f -mtime 5 -o -mtime 10
find -name '*.txt'
-o -name '*.out'
--- find files with extensions ".txt"
or ".out" from current directory or sub directories
find -newer fileName1
--- find
files newer than fileName1
sed command in UNIX
sed command in UNIX
sed (Stream Editor):
To convert/replace the character or string , to print the
expected lines in the output and more :
Let's create a file
named fileName1 with below contents :
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed -e 's/This/That/g' fileName1 --- converts all the "This"
to That from the file fileName1.
or
sed -e 's/This/That/' fileName1
--- This will convert first
occurance only from each line.
Output:
That fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
That is my town.
echo This This
This This | sed -e 's/This/That/' ---- This command will replace the first
occurance of the word "This" from the line , if more than one line
exits then 1st occurance from each line.
Output:
That This This This
echo This This
This This | sed -e 's/This/That/g' ---- This command will replace all "This" , "g" stands for "global"
Output:
That That That That
echo This This
This This | sed -e 's/This/That/2' ---- This command will replace the second
occurance of the word "This" from
the line , if more than one line exits then 2nd occurance from each line.
Output:
This That This This
Let's take the file :
fileName1:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed -e 's/s/S/' fileName1
--- replaces all the first occurance of "s" from each line to
"S"
Output:
ThiS fruit is Red.
Sunday is weekend.
fruit and vegetable are healthy.
We have work on week dayS.
ThiS is my town.
sed -e 's/e/E/g'
fileName1 ---- replaces all "e" to "E"
This fruit is REd.
sunday is wEEkEnd.
fruit and vEgEtablE arE hEalthy.
WE havE work on wEEk days.
This is my town.
echo We are enjoying the weekend. | sed -e 's/e/E/5g'
--- make changes from 5th
occurance from the line , if more
than one line exits then from 5th occurance in each line.
Output:
We are enjoying the wEEkEnd.
Divert the sed output to a file , if the output file
exits, it appends. If the output file does not exit, then it creates.
echo Seven days in a week | sed 's/Seven/7/g' > sedFile.txt
No output will be displayed.
Lets open the file
sedFile.txt :
7 days in a week
sed '4 s/e/E/' fileName1
--- replace "e" with
"E" , first occurance on 4th line only.
Output:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
WE have work on week days.
This is my town.
sed
'4 s/e/E/g' fileName1
--- replace all "e"
with "E" on 4th line only.
Output:
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
WE havE work on wEEk days.
This is my town.
sed '2 , 4 s/e/EE/g' fileName1
--- replace "e" with
"EE" from lines 2, 3, 4 .
This fruit is Red.
sunday is wEEEEkEEnd.
fruit and vEEgEEtablEE arEE hEEalthy.
WEE havEE work on wEEEEk days.
This is my town.
sed
'3 , $ s/h/HH/g'
fileName1 --- --- replace
"h" with "HH" from
line 3 to last line.
This fruit is Red.
sunday is weekend.
fruit and vegetable are HHealtHHy.
We HHave work on week days.
THHis is my town.
sed
-n 3p fileName1
--- prints only 3rd line.
fruit and vegetable are healthy.
sed
-n 2,4p fileName1
--- prints lines 2 to 4, ie, 2, 3, 4
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
sed
-n '2,$p' fileName1
--- prints lines 2 to last.
sed
4p fileName1 ---
prints line 4 twice
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
We have work on week days.
This is my town.
sed -e 's/week/WWWW/p' fileName1
--- prints the affected lines twice.
This fruit is Red.
sunday is WWWWend.
sunday is WWWWend.
fruit and vegetable are healthy.
We have work on WWWW days.
We have work on WWWW days.
This is my town.
sed -n 's/h/H/p'
fileName1 --- prints affected
lines only.
THis fruit is Red.
fruit and vegetable are Healthy.
We Have work on week days.
THis is my town.
sed
-i 's/t/TTT/ ' --- when used "i"
like this , the sed command makes
changes inside the file , does not print the
output.
sed -i 's/t/TTT/' fileName1
--- does not show the output, let's see inside the file :
cat fileName1
This fruiTTT is Red.
sunday is weekend.
fruiTTT and vegetable are healthy.
We have work on week days.
This is my TTTown.
sed
-e 's/t/WW/gi' fileName1
--- using "i" like
this, you can apply the expected change
irrespective of the case , "i" removes the case sensitivity in this case.
WWhis fruiWW is Red.
sunday is weekend.
fruiWW and vegeWWable are healWWhy.
We have work on week days.
WWhis is my WWown.
sed
3d fileName1 --- deletes
3rd line from the
output not from the file.
This fruit is Red.
sunday is weekend.
We have work on week days.
This is my town.
sed
$d fileName1 ---
deletes last line
from the output not from the file.
sed
'3,$d' fileName1 ---
deletes 3rd to last line
from the output not from the file.
Let's update fileName1 , blank/empty
lines have been added as below :
This fruit is Red.
sunday is weekend.
fruit and vegetable are healthy.
We have work on week days.
This is my town.
sed
's/^$/**Empty Line**/g' fileName1
--- inserts the given text in the
empty lines.
This fruit is Red.
sunday is weekend.
**Empty Line**
fruit and vegetable are healthy.
**Empty Line**
We have work on week days.
This is my town.
Let's create a new
file : fileName2 with below contents:
This this Theese thys this
thes that ssys this gum iss ssyy
ssa thya syha this this ibm this shss sbss
sed -e 's/this//g'
fileName2 ---
deletes the given word in the output
This Theese thys
thes that ssys gum
iss ssyy
ssa thya syha
ibm shss sbss
>> Kill Job or Process
Kill Job or Process :
you can kill a running process/job by using "kill" command alongwith
its PID :
Let's run sleep 1000& and then kill it :
sleep 1000& click ENTER , after you click ENTER you get
below information :
[1] 9019 where [1]
is the job number and 9019 is the process ID - PID
You can also get the PID using : ps or ps -e
or ps -ef
such as ,
if you click "ps" , you get below informaiton :
PID TTY
TIME CMD
4443 pts/1 00:00:00 bash
9019
pts/1 00:00:00 sleep ---- this the job/process which we need to
kill.
9048 pts/1 00:00:00 ps
"
kill " or " kill
-9 "
Syntax :
kill PID click ENTER
kill 9019 click ENTER
now check the status of the job sleep 1000&
by
using : jobs or ps or ps -e
or ps -ef
if you check the status with the command : "jobs',
it will show " Terminated " :
[1]+ Terminated sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
In many situations , the job will not be terminated/killed with only "kill" command. In such
situation use "-9" with "kill".
kill -9 PID
click ENTER --- this is killing
the job/process forcefully.
kill -9 9019
click ENTER
Now, if you check the status with the command job, it shows "killed" :
[1]+ Killed sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
>> ps command
"ps"
command
ps - process status is used to see the status of running processes.
when noting is running and we enter "ps" then it displays :
PID TTY
TIME CMD
4443 pts/1 00:00:00 bash
4551 pts/1
00:00:00 ps
Where ,
PID : is process ID
TTY : is the name of the console
or terminal into which the user is logged .
TIME : TIme in minute and seconds the process is
being running in the CPU.
CMD is the name of the command that initiated the process .
Let's run below jobs and check with "ps" :
Job1:
sleep 100 & click ENTER , it displays below information :
[1] 7642 ---- These are job number[1] and process ID (PID-7642)
job2:
sleep 105 &
click ENTER
[2] 7643
Check the status of the jobs by ENTERING the command "jobs" :
job click ENTER
[1]- Running sleep 100 &
[2]+ Running sleep 105 &
Now let's check process status with
the command "ps" :
ps click ENTER.
OutPut:
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
7642 pts/1 00:00:00 sleep --- first sleep command
7643 pts/1 00:00:00 sleep --- second sleep command
7657 pts/1 00:00:00 ps
To
see all the running/active process : ps -e or ps -A
ps -e click ENTER
Output :
PID TTY TIME CMD
1 ? 00:00:03 init
2 ? 00:00:00 migration/0
.
.
3153 ? 00:00:00 hcid
3159 ? 00:00:00 sdpd
3181 ? 00:00:00 krfcommd
3219 ? 00:00:02 pcscd
.
.
4440 ? 00:00:04
gnome-terminal
4442 ? 00:00:00 gnome-pty-helpe
4443 pts/1 00:00:00 bash
7642 pts/1 00:00:00 sleep
7643 pts/1 00:00:00 sleep
7658 pts/1 00:00:00 ps
To see the full list of all running processes : ps -ef
OutPut:
UID PID PPID C
STIME TTY TIME CMD
root 1 0 0
Mar09 ? 00:00:03 init [5]
root 2 1 0
Mar09 ? 00:00:00 [migration/0]
68 3
1 0 Mar09 ? 00:00:00 [ksoftirqd/0]
.
.
xfs 4437
1 0 Mar09 ? 00:00:00 gnome-screensaver
ram 4440 1 0
Mar09 ? 00:00:04 gnome-terminal
ram 4442 4440 0
Mar09 ? 00:00:00 gnome-pty-helper
ram 4443 4440 0
Mar09 pts/1 00:00:00 bash
ram 7642 4443 0
00:26 pts/1 00:00:00 sleep 100
ram 7643 4443 0
00:26 pts/1 00:00:00 sleep 105
ram 7659 4443 0
00:26 pts/1 00:00:00 ps -ef
To
search the process with a filter , you can use any filter from :
UID , PID , PPID , C ,STIME TTY TIME
CMD
such as :
ps -ef | grep ram
ps -ef | grep 4440
ps -ef | grep screensaver
ps -ef | grep 00:00:04
Kill
Process/Job
you can kill a running process/job by using "kill" command alongwith
its PID :
Let's run sleep 1000& and then kill it :
sleep 1000& click ENTER , after you click ENTER you get
below information :
[1] 9019 where [1]
is the job number and 9019 is the process ID - PID
You can also get the PID using : ps or ps -e
or ps -ef
such as ,
if you click "ps" , you get below informaiton :
PID TTY
TIME CMD
4443 pts/1 00:00:00 bash
9019
pts/1 00:00:00 sleep ---- this the job/process which we need to
kill.
9048 pts/1 00:00:00 ps
"
kill " or " kill
-9 "
Syntax :
kill PID click ENTER
kill 9019 click ENTER
now check the status of the job sleep 1000&
by
using : jobs or ps or ps -e
or ps -ef
if you check the status with the command : "jobs',
it will show " Terminated " :
[1]+ Terminated sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
In many situations , the job will not be terminated/killed with only "kill" command. In such
situation use "-9" with "kill".
kill -9 PID
click ENTER --- this is killing
the job/process forcefully.
kill -9 9019
click ENTER
Now, if you check the status with the command job, it shows "killed" :
[1]+ Killed sleep 1000
if you check with the command "ps",
it will not show the job/process like below :
PID TTY TIME CMD
4443 pts/1 00:00:00 bash
9218 pts/1 00:00:00 ps
Subscribe to:
Posts (Atom)