UNIX COMMAND MADEBYDharmesh
UNIX COMMAND MADEBYDharmesh
UNIX COMMAND MADEBYDharmesh
readable format.
Lsof: It will list down pid of all the process which using a particular
file.
ls -t : Lists all non-hidden files in the order they were last modified
cd ~ : Both move you to your home directory (the directory you start
from initially).
rm -fR name : rm -Rf name : This combination will force the removal of any
file and any directory including anything inside of it
cp file1 newname: Copies a file (file1) and names the copy the new name
(newname)
cp -R directory target : Copies a directory and names the copy the new name
(target)
mv script.exe ~/bin Moves the script.exe file to the bin directory that is
in the home (~) parent directory and it keeps its initial name
ps -u user_id : Shows all processes that are owned by the person with the
pertinent user_id
Tail -10 Filename: Show the last 10 lines from the file.
Kill -2 - first checks if the process can be stopped and then kill's
l -c string file : Searches and prints only the number of matches to the
screen
grep -n string file : Prints to the screen preceded by the line number
grep -v string file : Print lines that do not match are printed
Grep -H Pettern *
It will print the lines that have matching pattern along with filename.
chmod octal file : change the permissions of file to octal, which can be
found separately for user, group, and world by adding:
● 4 – read (r)
● 2 – write (w)
● 1 – execute (x)
Examples:
chmod 777 – read, write, execute for all
chmod 755 – rwx for owner, rx for group and world.
Symbolic Method
ssh-copy-id user@host : add your key to host for user to enable a keyed or
passwordless login
IMP: Find will not search the type or file-type in compressed file like
.tar, .gz, .bzip2 and also grep will not search pattern inside the .tar or
.gz or .bzip2
Network
ping host : ping host and output results
echo $* : show the parameters or argument name which you have define.
Syntax
rcp [-p] [-r] filename ….. directoryname
[-p] : Attempt to give each copy the same modification times, access times,
modes, and ACLs if applicable as original file.
[-r] : Copy each sub-tree rooted at filename; in this case the destination
must be a directory.
Example:
Above command copies the file name “myfile” from the local path “ /mydir”
to the remote computer named “hope” and placing into the directory “/
otherdir”
IMP:
RCP doesn’t properly handle symbolic file. Use tar or cpio piped
to rsh to obtain remote copies of directory containing symbolic file.
RCP doesnt support ACLs.
In our scripts we have the below line as the first line in every script
#!/bin/ksh/ u know what this line means?
UNIX Process and how its works? And which 2 files are running while login
in UNIX?
Process :
1./ SSH D process will run first.
2./ INIT Process will run after SSH D
3./ Once appears log-in screen on that time Getty process will run. Than
you log-in with user name and password.
Steps as below:
I have one file with name “abc.ksh”. I want to check the file current
location?
Utility
Utilities are programs you can run or execute. The programs who and
date.
Every Programme consider a utility.
The term utility refers to the name of a program
Command
Term command refers to the program and any arguments you specify to
that program to change its behavior
$ /bin/sh
$ /bin/sh Login
Ex: Put this 2 command in file Login and type as above. (Login is filename)
How to make shell script executable?
What are the two files used by the shell to initialize itself?
./password
./user-profile file
PATH is used to set the default Path to search for when any program/binary
is executed.
MANPATH is used to set the path to search when man command is executed
MANPATH=/usr/man:/usr/share/man
You can also check all the shell options with the following
set -o ( to enable and disable shell option like monitor, keyword,vi on off
etc)
We can run a process in background using & (an ampersand) at the end of the
command you run the job.
$:s/Dharmesh/Citi/g
$:100,200s/Dharmesh/citi/g
$: %s/DHARMESH/dharmesh/gI or Ic
$: %s/UnIx/dharmesh/gi or ic
$: %s/Unix/dharmesh/gc
#!bin/sh/
NAME=”ABC”
SURNAME=”BCD”
echo $NAME
echo $SURNAME
How to define Array and Values in Array?
array_name[index]=value
like as below
NAME[0]=”Dharmesh”
NAME[1]=”Sanjay”
To initialize Array
set -A array_name value1 value2 ...valuen (for KSH)
output
Dharmesh
Sanjay
Output:
Dharmesh Sanjay
How do i remove the file whose name begins with a "-"(File in current
directory)
rm ./"-"filename
Kill $!
ls -la
To differentiate between devise file and ordinary file kernal check the
file 'type' field in file structure.
a. True
b. False
nice
Option causes the command to continuously print additional line as they are
written.
tar zcvf tar_files command will-
a. Extract the content of the specified tar file. The z option specified
that the tar file has been compressed.
b. concatenate tar file with the specified name, containing the specified
file and their sub-directory the z option specified that the file will be
compressed.
c. Create tar file with the specified name, containing the specified file
and their sub-directory the z option specified that the file will be
compressed.
use >>
Write a command to display the second and fourth character from each line
of a file?
Write a command to display the second and fourth Word from each line of a
file?
How to list the files that are accessed 1 day ago in the current directory?
How to list the files that were modified 1 days ago in the current
directory?
How to list the files whose status is changed 2 days ago in the current
directory?
The Yes utility can be used to repeatedly output a line with the specified
string or 'y'.
$ grep MANAGER *
$ grep -i manager * ( I : without regard letter case)
Write a command to print the file names in a directory that contain the
word "MANAGER"?
How to create an alias for the complex command and remove the alias?
The alias utility is used to create the alias for a command. The below
command creates alias for "ls -l" command
$ unalias ll
$ date '+%Y-%m-%d'
2012-07-12
To find the no of blank line in a file?
FTP Command
Example:
Sftp dr12889@myservername
lcd <dir>
cd <remote server dir>
get <files> (from myserver)
put <files> (to myserver)
bye/disconnect/quit (disconnect from the FTP server)
FIND
IMP:
find / -mtime -1
This start the search at the root directory and looks for all file which
were modified 1day ago
find / -ctime -1
This start the search at the root directory and looks for all file which
were changed 1day ago
find / -atime -1
This start the search at the root directory and looks for all file which
were accessed 1day ago
find / -atime +5
This start the search at the root directory and looks for all file which
were accessed MORE THEN 5 DAYS ago
This starts the search at the root directory and looks for all file which
were modified 1day ago and sending all 'permission denied' message to the
bit bucket.
What command will find a directory named nethelp starting in the /var
directory?
Find /var -type d -name nethelp
What command will suppress the display of the “permission denied” messages
that are displayed when you use the find command?
Find / -name filename 2> /dev/null
How to find all the files or directories which holds 777 permission in UNIX
Box?
How to find all text file which contain word exception using find command
in UNIX?
Find . -name *.txt -print | xargs grep “Exception”
How to find the file some days older and above certain size?
Find . -mtime +10 -size 50000c -exec ls -l {} \;
Find everything in your home directory that has NOT been modified in the
last year:
find $HOME -mtime +365
To find html files that have been modified in the last seven 24-hour
periods (days), I can use -mtime with the argument -7 (include the
hyphen):
To find those html files that I haven’t touched for at least seven 24-hour
periods (days), I use +7:
find . -mtime +7 -name "*.html" -print
SED :
Sed is stand for Stream editor. One of the most common use of sed is text
subsitution. Eg: Replacing certain text with something else.
Which following command will subsitute (Find and Replace) 'FOO' to 'BAR'
globally.
sed 's/foo/bar/g'
Which is the following command will print 10lines of file using 'sed'
sed 10p
$ sed -n '11p' Filename | cut -f2 -d'|' ( if | thn -d'|' or if ' ' than -d'
')
How to get the last word from 11th line of a file in Unix file?
***APPEND LINES***
sed provides the command “a” to append line after every line with the
address or pettern.
*****SED in VI EDITOR*****
%s/dharmesh/dharmesh123/g
%s/dharmesh/dharmesh123/c
%s/dharmesh/Dharmesh123/gi
%s/DHARMESH/dharmesh123/gI
:100,200s/dharmesh/dharmes123/g
*****DEBUGGING IN UNIX*****
There is two technique for debugging shell scripting
1./ Syntax checking
2./ Shell Tracing
#!bin/sh opt : these methods for enabling debugging modes take effects when
script is invoked. so its refereed as invocation activated mode
example:
case $- in
*v*) : #verbose mode
;;
*x*) : #Shell trace Mode
;;
esac debugging modes
1./ You can use the set command to disable a debugging mode as follows: set
+opt
2./ To enable shell tracing in debug mode : set -x
3./ To Disable Shell tracing in debug mode : set +x
4./ To disable any and all the debugging mode that have been enabled : set
-
5./ To enabling dubbing for single function : set -x ; toDate; set +x
(toDate is function name)
6./ To see where the script produce the error : Sh -x script argument
Example:
#!/bin/sh
YN=y
if [ $YN = "yes" ]
echo "yes"
fi
Common question:
Echo $SHELL
result : /bin/bash
IMP : If you type only SHELL or $SHELL it gives you an error state command
is not found.
What is inside the /bin?
Inside the /bin folder we can find all the commands.exe which we are
usually executing on UNIX terminal.
If I don’t write #!/bin/sh and execute the script file then what will
happen ? It gives me an error or it will successfully execute.
#!/bin/sh its called shebang in UNIX speak # is called hash or sharp and !
Is called bang. So when you put together you will get hash bang or shebang
The after #! tells unix what program to use to run it. If it will not
specified then it will try with bash,sh,ksh or zsh or whatever shell
variable's is but it its there it will use that program. Plus # is comment
in most of the language so the line gets ignored in future execution.
Or
netstat command ?
Its provide socket level and port no information.
How to find which process are taking more memory? Or How will you get top
consuming process?
or
People : Considering the people that work in IT Services. also ensure the
following.
Their staff have the skill to match the role, Their Support,
Responsibility, communications, Customer Relationship etc...
1. Process Strategy
2. Process Design
3. Process Documentation
4 .Resource Training
How will you find a file in current directory without searching the file in
sub directory?
Example : I have directory called Dharmesh and inside the Dharmesh I have
many sub-directories so what I want is to find the file in only Dharmesh
directory instead of all sub-directory.
Its depends upon the how much space is available in either /var/tmp (VI
Temp location)also I had a case where I had to split the file into serval
once like
split -l 5 file tf
then editing them using
vi tf*
then rejoining them using '>>'operators
How do you find which remote hosts are connecting to your host on a
particular port say 10123?
Netstat
ex : netstat -a | grep 'port'
If once process is inserting data into your mssql database? How will you
check how many rows inserted in every second?
Watch
Your Application home directory is full? How will you find which directory
is taking how much space?
Du -sh | grep G will list down all the files which have GIGS in size.
>echo $SqlReturnMsg
Link across to file system: If you want to link file across the file system
Link to Directories: If you want to link directories than you must be using
soft-link as you cant create hard-link to directories.
Storage Space: Hardlink takes very low amount of space as there are no new
nodes created. Softlink takes the space of 4KB or depends upon the
filesystem.
Moving File Location: If you move the source file to some other location on
the same filesystem than hardlink will work softlink will not.
Redundancy: If you want make sure saftly of your data you should use
hardlink. As in hardlink data is safe until all links to the files are
deleted. In softlink you will lose the data if master instance of file is
deleted.
Softlink will have different inode number than the source file and hardlink
using the same inode number as the source file.
Now you can use the above calculation file permission for exmple: umask set
to 077 so how it will calculate as below
0 – Owner – r,w,x
7 – group – no permission
7 – others – no permission
umask 022 for Normal user: Used for normal user. With this mask default
directory permission are 775 and default file permission 664
umask 022 for Root user: Used for root user. With this mask default
directory permission are 755 and default file permission 644
umask 022 for directories: Base permission are 0777 (rwxrwxrwx) and for
file is 0666 (rw-rw-rw)
in short:
umask 022 : allow to write a date but anyone can read the data.
Umask 077: its completely provide system like no one can read or write the
data it umask setted 077.
TOP
Top command is used to see the information about all the active process.
CPU Usage, Memory Usage, Running time and program name etc…
ROW1
ROW2
Task: 123 Total == Total process in active mode.
2 running == currently running process.
3 stopped == Currently Stopped process.
TOP Command
ZZ == same as wq.
:n == Move to the nth line (:10 it will move to the 10th line)
HTML Code:
ls -lrt -- will sort the file based on list and recent time modified
sort +4n -- will sort the ls -lrt output based on the column 4 which is size
tail -- will give you the last 10 lines , if you use tail -1 then you will
get the largest file alone
Pkill -TERM -P 27888 (Parent ID) it will kill all the process in PS 27888
How to find the process group ID using grep. For example, to kill my-script.sh's process group.
What is the use of .profile file in UNIX? And I have a login with my username and password.
Where would my .profile file reside. Can i edit it?
First: the ".profile" is the user run control file (hidden file) that contain the list of the environment
Variables for your session as well as any commands you wanna to run upon your login.
Second: this file is loaded every time you login to your system using telnet/ssh and it is located
Under your home directory and you can edit it as well.
If you just use a pipe, it receives data on STDIN (the standard input stream) as a raw pile of data
that it can sort through one line at a time. However some programs don't accept their commands
on standard in, they expect it to be spelled out in the arguments to the command. For
example touch takes a file name as a parameter on the command line like so: touch file1.txt.
If you have a program that outputs filenames on standard out and want to use them as
arguments to touch, you have to use xargs which reads the STDIN stream data and converts each
line into space separated arguments to the command.