Advance Unix Command Part1.
Hello Everyone,
In Previous blogs I discussed about basic Unix command which is commonly used. Today I will discuss some advance command of Unix which is mostly used in organization. Please see my below blogs related to Basic Unix command for your reference.
https://dheeraj60.blogspot.com/2020/08/basic-and-some-advance-command-in-unix.html
https://dheeraj60.blogspot.com/2020/08/basic-and-commonly-used-command-in-unix.html
Let’s Start with Unix command.
Find Command to find the modified files 30 days ago.
Note : We can give any number of days
Important : -mtime +30 means you are looking for a file modified 30 days ago and -mtime -30 means less than 60 days.
Command is as below:
find /home -iname "*.txt" -mtime - 30 –print
Find Command to Display content of file on screen that were last modified 30 days ago.
find /home/ -iname "*.txt" -mtime -30 -exec cat {} \;
Below is the output of above commands in putty Terminal.
Before finding the word count of any file we need to understand below command first .
wc : wc command is used to find the word count
$ wc - l dheeraj.txt
Above command will give you the count of text
Using Find command we will see count total number of files using wc command.
find /home -iname "*.txt" -mtime -30 | wc –l
Find command to get List all mp4s that were accessed exactly 30 days ago:
find /home/ -iname "*.mp4" -atime 30 -type –f
Note : -type f – Only search for files not directories.
Below is the output of above command in putty terminal.
Let discuss about SED command in Unix : SED command in UNIX is known as stream editor and it can perform lot’s of function on file like, searching, find and replace, insertion or deletion. SED command in UNIX is for substitution or for find and replace by using SED command we can edit file without opening it . We can use it both in vi editor or by using cat command .
Let’s take example I am writing simple text by using sed command as below :
cat > dheeraj.txt
Welcome to Diksha Oracle Training Centre.
Welcome to Diksha Oracle Training Centre.
Welcome to Diksha Oracle Training Centre.
File created .
Now we can use sed command to replace the text in a file from Diksha to Sakshi by using sed command .
Below is the command :
$ sed 's/Diksha/Sakshi/' dheeraj.txt
After issuing this command text is replaced from Diksha to Sakshi .
Below is the output in putty terminal for your reference.
If we want to replace the character of 2nd occurrence in text by using sed command we use below :
cat > dheeraj1.txt
Welcome to Diksha Oracle Training Centre. Diksha Training
Welcome to Diksha Oracle Training Centre
Now we need to replace 2nd occurrence of Diksha to Sakshi by using below sed command.
$ sed 's/Diksha/Sakshi/2' dheeraj1.txt
After issuing this command the 2nd appearance of Diksha will be replaced to Sakshi. You can put any number as per your requirement.
Please see the output of above command in putty terminal.
Replacing all the occurrence by using sed command .
cat > dheeraj2.txt
Welcome to Diksha Oracle Training Centre
Welcome to Diksha Oracle Training Centre
Welcome to Diksha Oracle Training Centre
Sed command
$ sed 's/Diksha/Sakshi/g' dheeraj2.txt
Here g means global . It will replace to Diksha to Sakshi wherever it finds in text file.
Below is the output in putty terminal for your reference.
Head and tail command in Unix : This command is commonly used in Unix when you check the long file to see the status.
Note : This command basically used to check the log file for any processes or also you can use to see some pieces of code .
Head command : $ head -10 dheeraj2.txt
It will give the first 10 line from the text file
Tail command : $ tail -10 dheeraj2.txt
It will give the last 10 line from the text file
Note : I use 1 and 2 only since my text file is small .
Below is the output of command in putty terminal.
Grep is a Unix command-line tool used to search for a string of characters in a specified file. The text search pattern is called a regular expression. When it finds a match, it prints the line with the result. Grep command can be used in find and other command such as for process checking in Unix .
Basic Grep Command is as below :
$ grep Training dheeraj2.txt
$ grep Training *.txt
Here * means all .
Below is the output in Putty Terminal.
Through grep command to find whole word only we use below command from home directory , just append –w to the grep command
$ grep –w Diksha *
Note : –w is omitted, grep displays the search pattern even if it is a substring of another word.
To Ignore Case in Grep Searches
$ grep –i Diksha *
Note : -i operator to search files in the current directory for Diksha
Below is the output of above grep command in Putty Terminal for your reference.
Using Grep command to include all subdirectories in a search, add the -r operator to the grep command.
$ grep -r Diksha *
$ grep - rw Diksha *
We know -w operator to show whole words
Below is the output of above grep command in Putty Terminal.
To find Particular Process through Grep command we can use ps –ef and through pipe we can use grep command to search that particular process
$ ps –ef
Above command will give all the process ID which is running in the system.
If I want to see particular script like abc.sh and it’s process id then we use below command by using grep
$ps –ef |grep abc.sh
Below is the output of above commands in Putty Terminal.
Note : Still Lot’s of commands needs to be discussed as we can use grep in find commands and also we have lots of other commands as well which I will cover in next blog .
Please go through this blog carefully and let me know if you have any questions . In next blog I will try to cover all the important commands in Unix as well as some small shell scripts. All this command from this blog and my previous blogs are important when you try to start writing shell script . Still so many commands are missing which i will include in next blog.
Thanks.
It is very useful for me. Thanks...
ReplyDeleteAzure Data Factory Online Training
Azure Data Factory Live Online Training