Today I
think to post some linux commands which we are using daily. I hope it will be
helpful to you. I am posted some examples how to do and what actually command
do.
Note :
Linux command can take a number of options. These options can be passed in
both shorthand form or expanded form. with shorthand form use single hyphen –
and with expanded form use double hyphen - -, say you can get help option
to a command as -h or – -h
1 -> history - this command will
show you all the command you have typed in past
Example:
$ history – this
will list all command used in past
$ history | grep netbeans - given
that we have piped the result of history to grep it will return only those
command , which contain word history in them
2 -> man – this
command show complete detail of a command
Example:
$ man man - it
will tell about how to use man itself
$ man sudo -
will tell you all about sudo command
$ man dpkg -
will tell you all about dpkg command
3 -> help -
show the available options for a command
Example:
$ cd /netbeans-6.9.1/bin - move to netbeans bin
directory
$ ./netbeans – -help - you want to start
netbeans and want to know available options. It has listed down all options for
you
General
options:
–help -
show this help
–jdkhome <path> -
path to Java(TM) 2 SDK, Standard Edition
-J<jvm_option> -
pass <jvm_option> to JVM
–cp:p <classpath> - prepend <classpath> to classpath
–cp:a <classpath> -append <classpath> to classpath
4 -> ls - Listing files and
folders
Examples:
$ ls - will list all folder
and files but not hidden files or folder
$ ls -a - will list hidden files
and folders also
$ ls -B - will not
list the backup file ending with ~
$ ls -l - will do
complete listing with all details like owner, date, permission etc. but will not
show hidden file
$ ls -l -a -B - will do all the above
5 -> mkdir - creating directory
Example:
$ mkdir myfolder – to make directory called myfolder
$ mkdir -p
myfolder1/myfolder2/myfolder3 – the -p
option tell to create nested directory with parent child relation. Thus
myfolder3 will be created in myfolder2 a
$ mkdir -p myfolder - the p option means parent , if the directory already
exist do not throw error but use the existing directory as parent
$ mkdir -pv -m 777 myfolder - it will not complain if directory exist, set the
permission to 777 and also print the task it performing on terminal
6 -> cd - changing directory
Examples:
$ cd myfolder - take you inside
myfolder or it will show inside the folder
$ cd .. - take
you out from current folder to the previous folder
$ cd ~ - take you out of every
previous folder to the root of terminal
7-> touch - it will create a file
if not present and if already present will modify its modification or access
time depending on argument you pass to it, but not overwrite its content
Examples
:
$ touch test1 - will create test1 file
$ touch test1 test2 test3 test4 - will create test2
test3 test4 as new file. Since test1 is already there it will just modify its access time
$ touch -a test1 - will change access
time of test1 file
$ touch -m test1 - will change
modification time of file test1
$ touch -am test1 -
will change both access as well as the modification time
$ touch -d
'1 May 2005 10:22' test1 - with d option you can
set any date you want but maintain the formate of string, say '1 05 2005
10:22' otherwise '2005 1 May 10:22' will give
error. however, you can ignore time if you want
$ touch -d
'1 May 2005' test1 - it will set only the
date, if you do not pass year then it will take current year i,e '1 May' will
also work
$ touch -r test2 test1 – the -r is reference
option i,e it will set datetime of file test2 to test1 file
8 -> rm - removing file or folder, by default it remove only
files to remove a folder, you need to pass additional options
Example :
$ rm my_file - will remove
my_file
$ rm -f my_file - will not throw error
if my_file do not exist i,e it will ignore the removal
$ rm -fi my_file - Will ignore
if file is not there and if here ask you to press Y to delete the file i,e it
will become interactive
$ rm -r my_folder - The -r option must be
passed to delete a folder other while it will not delete it
$ rm -rf my_folder - will delete the folder
and will not complain if the folder do not exist
9 -> rmdir - it will list empty
directory
Examples:
$ rmdir dir1 dir2 dir3 - it will delete the
empty directory dir1 dir2 dir3
$ rmdir -p dir1/dir2/dir3/dir4 - It will delete the
parent also if deleting the child make it empty, so first dir4 will be deleted
then dir3 then dir2 and so on
10-> mv - It will move file or
directory to other location. if the location of source destination is same the
file or directory will be get renamed
Examples
:
$ mv myfile.txt myfile.rb - it will rename myfile.txt to myfile.rb i,e the
extension name is changed.
$ mv -bvi myfile.txt myfile.rb - here we are passing
some option b is to backup the file before renaming, i is to ask your yes no before renaming and v means
print what going on the console
$ mv test1 test_folder
myfile.rb /home/charmal/my_folder - it will move the file
test , myfile.rb and the test_folder to /home/charmal/my_folder
$ mv * /home/charmal/my_folder - it will move
everything in current folder to /home/charmal/my_folder
11-> grep - It is used to search a
pattern specified by user in the given text or files or block passed from first
command to it through the pipe symbol |
$ grep “my works” file1 file2
file3 - It will list all lines of these three file containing “my
woks” in it in separate line, each line will be preceded with name of the file
in which they appear. If you do not want the file name you can use -h option as
below
$ grep -h “my works” file1
file2 file3 - It will not precede the matched line with the file in
which they are found
$ grep -i “my works” file1
file2 file3 - it tell to ignore the case i,e “my woks” will also
match
$ grep “my works” * - will search the text “my woks” in all the text file in
the current directory. Search for a
given string in all files recursively.
$ grep -r “my works” * - the -r option will
make it to search the text “Linux is good” in all the text file recursively in
the current directory as well as all it child directory
$ grep -c “my works” file1
file2 - It will not print each line but just return the count
of match in each file
$ grep -n “my works” file1 file2 - It
will also print the line number of the line containing the pattern in each file
$ grep -rni “my works” * - will recursively search each directory, print the line
no and will ignore the case
$ ls -l | grep “foo” - it will list only
those file and directory which containg the word foo
$ history | grep rails - will list history of
only those command which contain the word rails
$ history | grep -in rails - will ignore
case, and print line no.
$ grep -i
"the" demo_file - Search for a given string in a file (case
in-sensitive search).
$ grep -A 3
-i "example" demo_text - Print the matched line,
along with the 3 lines after it.
12 -> less - can be used to see
content of a file .It allow you to display a large data in block and let you to
scroll down, basically, it keep the screen at first line and let you scroll
down. If you do not use this, screen will set to the last line and you have to
scroll up to see the first line. you can exit the content by pressing q .
Examples:
$ less myfile.txt - It will show content of file from the top, need to
scroll down to see more content. press q to exit the content
$ ps -ef | less - will show the result
of ps -ef command from the top
13 -> more - It can be also used to display content of a file on
terminal.
Examples:
$ more myfile - will show that much content of file which fit in screen
$ more -100 + 50 myfile - will show 100 lines of
file starting from line 50
$ more +/”Iam charmal” -100 - will show 100 line of
the file from the line which contain “Iam charmal”
$ more -s -u -100 +50
myfile - s will cause to remove blank line from display
and u will remove underlines
$ ps -ef | more -10 + 20 - will display 10 lines
of the result of command ps -ef starting from the 20 line
14 -> cat - will be used to create a file, read a file, concenate
content of many files
Example:
$ cat >> test_file1 - it will create test_file1 and when you press
enter it will take you to the next line where you write to it, to exist press
enter then press ctrl and d.I have pressed enter on last line to reach here again
pressed eneter to come to next line and keep writing.All content Iam writing
will get written to test_files1to exist writing we will press Enter followed by
Ctrl D
$ cat test_file1 - it will show you content of file test_file1,
so you can see the content you have written on the terminal. I have pressed
enter on last line to reach here again pressed enter to come to next line and
keep writing. All content I am writing will get written to test_files1 to exist
writing we will press Enter followed by Ctrl D
charmal@charmal-kavirathna:~$ cat -n test_file1 #the -n option will print the
line no also
·
I have
pressed enter on last line to reach here
·
again
pressed eneter to come to next line and keep writing
·
All content
Iam writing will get written to test_files1
·
to exist
writing we will press Enter followed by Ctrl D
$ cat >> test_file2 - will create a new file
This is the second file you
added this line to test_file2
$ cat test_file1 test_file2 >>
test_file3 - will create
file test_file3 and append content of test_file1 test_file2 to it
$ cat test_file3 - it show the content of test_file3, you can
see that this file contain content of both the files
I have pressed enter on
last line to reach here again pressed enter to come to next line and keep
writing. All content writing will get written to test_files1 to exist writing
we will press Enter followed by Ctrl D. This is the second file
15 -> echo - you can use it to write a string to file or
terminal itself
Example:
$ echo Iam Charmal - it will print the output to terminal Iam Charmal
$ echo Iam Arun to test_file >>
test_file - it will
append the string to the test_file, if test_file is not present >>
operator will create it and the append to it
$ cat test_file - we will see the content of test_file
$ echo Iam adding one more line >>
test_file - one more
line is appended
$ cat test_file - you can see both the line
$ echo this “>” operator is dangerous as it
will erase and write > test_file # see the danger of using >, if the file
exist it will erase its content before appending
arun@arun-yadav:~$ cat test_file
this >
operator is dangerous as it will erase and write # see earlier line is erased
and only single line present
16 -> pstree - will return all the running
process in tree structure with there name and process id
Example:
$ pstree - will list all the
running process with all the user
$ pstree root - will show all the
process running with root user
$ pstree arun - will list all the
process running with the user arun
$ pstree -p arun - will print the PID i,e
process id also with the name
$ pstree -pn - will print the PID and also in the sorted order
$ pstree -pnh - the h option will highlight the current running process
$ pstree -pnhu - the u option will also
print the user owning that process
$ pstree -pnhul - l option will not
allow to truncate the process detail if it is too long
17 -> pgrep - It will return PID of the process
Examples:
$ pgrep firefox - will return PID of
firefox
$ pgrep ruby - will return PID of
ruby
$ pgrep skype - will return PID of skype
18 -> ps - it means process status and return all the running
process with name and PID
Examples
:
$ ps - will return
currently active process
$ ps -e - will return all the running process
$ ps -ef - will return all running process with full detail
$ ps -ef | less - as process list is
very long, we have piped the output of ps -ef to less
command. It help you to display result in small blocks and let you to
scroll down to see more
19 -> pkill - will kill the process passed to it
Examples:
$ pkill ruby
$ pkill firefox
20 -> kill - will kill the process whose PID is passed to it
Examples:
$ kill 485 - will terminate the
process with PID 485. here the default signa 15 get passed, there may be chance
that it fail to kill the process, say if it is making some system call
$ kill -9 485 - it will be guaranteed
that the process will be called. If the process is making any system call, it
will wait and kill it when it return from the call
21 -> killall - will kill the process
along with its child whose name passed to it. helpful if you know the process
name
Examples:
$ killall nautilus -will kill the nautilus
process
$ killall -e nautilus - it will tell to kill
whose name exactly match with nautilus. It will be helpful if your process name
is more that 15 character, since killall by default match only upto that, so
will delete all other process also whose first 15 character matches. the -e
option will prevent this.
$ killall -ew nautilus - the w option will tell
to wait till all the process are killed, otherwise killall check it every
second by default and resend the signal if found some is still running, but
since some has already get killed, some signal become unresponsive. Also
killall can kill all the process, including other killall process, except
itself
22 -> pwd - will print the current
working directory
23 -> clear - It will remove all
previous command and output from the console
24-> find command
Examples:
$ find -iname
"MyCProgram.c" - Find files using file-name ( case in-sensitve find)
$ find -iname
"MyCProgram.c" -exec md5sum {} \; - Execute
commands on files found by the find command
$ find ~ -empty - Find all empty files in home directory
25->SSH Command
Examples:
$ ssh -l jsmith remotehost.example.com -
Login to remote host
$ ssh -v -l jsmith
remotehost.example.com - Debug ssh client
$ ssh -V OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003 – to Display ssh client version
26-> unzip command
Examples:
$ unzip test.zip
- To extract a *.zip compressed file
$ unzip -l
jasper.zip - View the contents of *.zip file (Without unzipping it)
Archive: jasper.zip
Length Date
Time Name
-------- ----
---- ----
40995 11-30-98 23:50 META-INF/MANIFEST.MF
32169 08-25-98 21:07 classes_
15964 08-25-98 21:07 classes_names
10542
08-25-98 21:07 classes_ncomp
Later i will post another commands.
THANK YOU