FAQ_UNIX

Q1- What is the difference between grep & find?
grep is used to search for string in a file.
find is used to search files or directories.

Q2: How to create a file system quota in HP-UX?
newfs /dev/vg01/rmytmp

Q3: What are the mount and unmount system calls?
The privileged mount system call is used to attach a file
system to a directory of another file system; the unmount system call detaches a file system.

Q4: How to print/display the first line of a file?
head -1 file.txt
or
sed '2,$ d' file.txt

[Sed] is a very powerful text editor which can be used for various text manipulation purposes.

The 'd' parameter basically tells [sed] to delete all the records from display output from line no. 2 to
last line of the file (last line is represented by $ symbol).
Of course it does not actually delete those lines from the file,
it just does not display those lines in standard output screen.
So you only see the remaining line which is the first line.

Q5: How to print/display the last line of a file?

tail -1 file.txt

or

sed -n '$ p' file.txt

From our previous answer, we already know that '$' stands for the last line of the file.
So '$ p' basically prints (p for print) the last line in standard output screen.
'-n' switch takes [sed] to silent mode so that [sed] does not print anything else in the output.

Q6: How to display n-th line of a file?

sed –n '<n> p' file.txt

If you need to replace <n> with the actual line number.
So if you want to print the 4th line, the command will be

sed –n '4 p' test

or

head -4 file.txt | tail -1


Q7: How to get the nth word of a line in Unix?

Assuming the words in the line are separated by space, we can use the [cut] command.
[cut] is a very powerful and useful command and it's real easy.
All you have to do to get the n-th word from the line is issue the following command:

cut –f<n> -d' '

'-d' switch tells [cut] about what is the delimiter (or separator) in the file, which is space ' ' in this case.
If the separator was comma, we could have written -d',' then.
So, suppose I want find the 4th word from the below string: “A quick brown fox jumped over the lazy cat”,
we will do something like this:

$> echo “A quick brown fox jumped over the lazy cat” | cut –f4 –d' '


Q8: How to reverse a string in unix?

$> echo "unix" | rev
xinu

Q9: How to zip a file in Linux?

gzip filename

Q10: How to unzip a file in Linux?

gunzip filename

Q11: How to connect to Oracle database from within shell script?

You will be using the same [sqlplus] command to connect to database that you use normally even outside the shell script.
To understand this, let's take an example. In this example, we will connect to database,
fire a query and get the output printed from the unix shell. Ok? Here we go –


$>res=`sqlplus -s username/password@database_name <<EOF
SET HEAD OFF;
select count(*) from dual;
EXIT;
EOF`
$> echo $res
1


Q12: How to execute a database stored procedure from Shell script?

$> SqlReturnMsg=`sqlplus -s username/password@database<<EOF
BEGIN
Proc_Your_Procedure(… your-input-parameters …);
END;
/
EXIT;
EOF`
$> echo $SqlReturnMsg


Q13: How to fail a shell script programmatically?

Just put an [exit] command in the shell script with return value other than 0.
this is because the exit codes of successful Unix programs is zero. So, suppose if you write

exit -1

Q14: How to list down file/folder lists alphabetically?

Normally [ls –lt] command lists down file/folder list sorted by modified time.
If you want to list then alphabetically, then you should simply specify: [ls –l]


Q15: How to check if the last command was successful in Unix?

To check the status of last executed command in UNIX, you can check the value of an inbuilt bash variable [$?].

See the below example:

$> echo $?


Q16: How to check if a file is present in a particular directory in Unix?

Using command, we can do it in many ways. Based on what we have learnt so far,

we can make use of [ls] and [$?] command to do this. See below:

$> ls -1 file1.txt; echo $?  # not L  numeric 1

If the file exists, the [ls] command will be successful. Hence [echo $?] will print 0.
If the file does not exist, then [ls] command will fail and hence [echo $?] will print 1.

Q17: How to check all the running processes in Unix?

The standard command to see this is [ps]. But [ps] only shows you the snapshot of the processes at that instance.
If you need to monitor the processes for a certain period of time and need to refresh the results in each interval,
consider using the [top] command.

$> ps –ef

f you wish to see the % of memory usage and CPU usage, then consider the below switches
$> ps aux

If you wish to use this command inside some shell script, or if you want to customize the output of [ps] command, you may use “-o” switch like below. By using “-o” switch, you can specify the columns that you want [ps] to print out.
$>ps -e -o stime,user,pid,args,%mem,%cpu


Q18: How to tell if my process is running in Unix?

You can list down all the running processes using [ps] command. Then you can “grep” your user name or process name to see if the process is running. See below:

$>ps -e -o stime,user,pid,args,%mem,%cpu | grep "opera"

13:27 root      2554 grep opera                   0.1  0.0


Q19: How to get the CPU and Memory details in Linux server?

In Linux based systems, you can easily access the CPU and memory details from the /proc/cpuinfo and /proc/meminfo,
like this:

$>cat /proc/meminfo
$>cat /proc/cpuinfo

Q20: How to remove the first line / header from a file?

sed -i '1 d' file1.txt


Q21: How to remove the last line/ trailer from a file in Unix script?

sed -i '$ d' file1.txt

Q22: How to remove certain lines from a file in Unix?

If you want to remove line <m> to line <n> from a given file,
you can accomplish the task in the similar method shown above. Here is an example:

sed -i '3,4 d' file.txt

The above command will delete line 3 to line 4 from the file file.txt

Q23: How to remove the last n-th line from a file?

This is bit tricky. Suppose your file contains 100 lines and you want to remove the last 5 lines.
 Now if you know how many lines are there in the file, then you can simply use the above shown method
 and can remove all the lines from 96 to 100 like below:

$> head -95 file.txt


Q24: How to check the length of any line in a file?

We already know how to print one line from a file which is this:

$> sed –n '<n> p' file.txt
Where <n> is to be replaced by the actual line number that you want to print.

Now once you know it, it is easy to print out the length of this line by using [wc] command with '-c' switch.

$> sed –n '35 p' file.txt | wc –c
The above command will print the length of 35th line in the file.txt.


Q25:How to get the n-th field from a Unix command output?

We know we can do it by [cut]. Like below command extracts the first field from the output of [wc –c] command

$>wc -c file.txt | cut -d' ' -f1
109
But I want to introduce one more command to do this here. That is by using [awk] command.
[awk] is a very powerful command for text pattern scanning and processing.
Here we will see how may we use of [awk] to extract the first field (or first column) from the output of another
command. Like above suppose I want to print the first column of the [wc –c] output. Here is how it goes like this:

$>wc -c file.txt | awk ' ''{print $1}'
109
The basic syntax of [awk] is like this:

awk 'pattern space''{action space}'
The pattern space can be left blank or omitted, like below:
$>wc -c file.txt | awk '{print $1}'
109
In the action space, we have asked [awk] to take the action of printing the first column ($1).


No comments:

Post a Comment