UNIX/Linux OS Commands

OS Commands


What happens when a user logs in?
  1. Authentication - verifying that user name and password provided are valid
  2. .profile file present in the user's home directory will be executed to configure the environment
  3. User will land in user's home directory
What happens when a command is executed?
  • Whenever a command is executed, an attempt will be made to locate the binary/executable file for that command in all the path(s) associated with that user in addition to PWD.
  • If the binary is located in any of the path(S) or PWD and user has execute permission on that command/binary/program, then that command/binary/program will be executed. (It does this by searching for an executable or script in the directories listed in the environment variable PATH)
Some special characters we should be aware of while dealing with commands
~    tilda
!    exclamation or not
@    at the rate 
#    hash
$    dollar
%    percent or modulo
^    carat
&    and or ampersand
*    star or wildcard
( )    round/parentheses brackets
[ ]    square brackets or arrays
{ }    curly/flower brackets
<>    Angle brackets
-    minus or hypen or dash
_    underscore
+    plus or addition or sum or concatenate
=    equal to
;    semi colon
:    colon
" "    double quotes
' '    single quotes
` `    back tics
<    less than
>    greater than
,    comma
?    question mark
/    forward slash or division
\    backward slash
|    pipe
.    dot or period
\t    tab
\n   new line

#################################################################################
Notes for Commands begins here


A Command is a specific instruction given to computer to perform a specific task.
Command can be executed in multiple ways depending on the situation/requirement. At times, we don't specify an option or argument and system will take an option or argument by itself and it is called default option or argument.
  1. Command
  2. Command with option(s)
  3. Command with argument(s)
  4. Command with option(s) and arguments
Option: Command and option are separated by space. An option would usually follow command and start/prefixed with -(usually called as hyphen or minus or dash). A command can have 0(zero) or more options and it totally depends on the situation.
I think it's better to explain with example.
command -option(s)
command -option1 -option2...-optionn

>id (Here "id" is command. 1 command, 0 or no option)
>id -u (Here "id" is command and "u" is option. 1 command, 1 option)
>id -u -n (or) id -un (Here "id" is command and "u" and "n" are options. 1 command, 2 options)

Argument: Command and argument are separated by space. A command can have 0(zero) or more arguments and it totally depends on the situation.
I think it's better to explain with example.
command argument(s)
command -argument-argument2...-argumentn

id (Here "id" is command. 1 command, 0 or no argument)
id jamesbond (Here "id" is command and "jamesbond" is argument. 1 command, 1 argument)
touch f1 f2 (Here "touch" is command and "f1" and "f2" are arguments. 1 command, 2 arguments)


To summarize, 
  • options starts with -
  • both option and argument follow command
  • both option and argument are separated by space
Before we take a deep dive, as per my understanding commands can be broadly categorized into following. Let's also understand what a command is.
  1. Navigation - these will help in navigating around the servers
    1. pwd
    2. cd
    3. ls
  2. Generic commands
    1. date
    2. uname
    3. uptime
    4. clear
    5. man
    6. hostname
    7. history
    8. fc -l -100
    9. tty
    10. whoami (id -un)
    11. who
    12. w  (uptime + who)
    13. init (init 0 - shutdown, init 6 - reboot)
    14. finger
    15. whereis
    16. which
    17. su
    18. sleep
    19. ifconfig
    20. exit
    21. echo  ($SHELL, $HOME, $USER, $PWD, $PATH, $$, $?, $#, $*, )
    22. useradd
    23. passwd
    24. userdel
    25. groupadd
    26. groupdel
    27. usermod
    28. id
    29. nohup
    30. jobs
    31. env
    32. export
    33. Ctrl + c
    34. Ctrl + z
  3. File handling commands (In UNIX/Linux, both File and Directory will be referred as file) - these will help to take some specific action on files
    1. mkdir
    2. touch - Create a blank file or change file’s last modified time
    3. rmdir
    4. rm (we have to be very careful with this command)
    5. chmod
    6. chown
    7. chgrp
    8. cp
    9. mv
    10. ln (>ln -s <originalfile> <linkname>)
    11. gzip
    12. gunzip
    13. unzip
    14. tar (-t, -v, -f, -x, -c, -C)
    15. file
    16. umask (Files - 0666 and Directory 0777)
    17. stat
    18. mount
    19. scp
  4. To view contents of files - these will help to view, filter the content of file or output of commands
    1. cat
    2. more
    3. less
    4. head
    5. tail
    6. grep (just like Ctrl + f in Windows)
    7. sort
    8. uniq
    9. cut
    10. tr
    11. awk
    12. diff
    13. sdiff
    14. paste
    15. wc (-l, -w, -c)
    16. vi editor
  5. Monitoring related commands - these will help to monitor the performance of Server
    1. top in Linux /topas in AIX
    2. ps
    3. du
    4. df
    5. free
    6. vmstat
    7. ping
    8. ssh
    9. nslookup
    10. kill
Let's look at each command in detail
######################

pwd command begins here

Commandpwd

Purpose: display/print the current/present directory on standard output(screen); 

Options:
  • NONE
Arguments
  • NONE
Syntax:
  • >pwd
Example:
  • >pwd
pwd command ends here

######################

cd command begins here

Commandcd

Purpose: change from current directory to target directory

Options:
  • NONE
Arguments
  1. <target_directory>
Syntax:
  • >cd <target_directory>
Example:

Let's take "/tmp" as directory here 
  • >cd /tmp
Notes:

Root Directory:  / (root or slash)
top-most directory in any UNIX file system structure. Some call it slash and some call it as root

Current Directory:   . (to be read as dot)
default location for working with files

Parent Directory:   ..
directory immediately above the current directory

Previous Working Directory:   - (hyphen)
Navigates to the previous working directory

Home Directory: ~ (to be read as tilda)
  • directory owned by a user
  • default location when user logs in
  • default home directory for a user will be /home/<username>
  • User’s home directory can also be denoted by ~<username>

Root user’s home directory:  /root
contains files related to root user

Normal user’s home directory by default is /home/<username> 
However, it is always best to use ~<username>  to refer a home directory of user
contains files related to that particular user

cd command ends here

######################

ls command begins here

Commandls

Purpose:
to display the contents of a given directory (or)
to display the details of a given file (or)
to display the contents of current directory by default (if no argument is given)

Options:

  • -l long listing
  • -t display as per last modified timestamp
  • -r display the output in reverse order
  • -R display the contents of a directory "recursively" 
  • -i display the inode
  • -A display hidden/system files excluding . and ..
  • -a display hidden/system files including . and .. 
  • -h human readable form (works only in Linux)

Arguments
  1. file (or)
  2. directory (or)
  3. Default argument will be "pwd"

Syntax:

>ls
>ls -[options]
>ls [filename or directoryname]
>ls -[options] [filename or directoryname]

Example:
  • ls
  • ls -l 
  • ls -ltr  (or) ls -l -t -r  (or) ls -l -r -t
  • ls -ltr /etc/passwd
  • ls -ltr /home/db2inst1
Example 1

db2inst2@db2v10P:~> ls -ltr Desktop/
total 62604
-rw-r--r-- 1 db2inst2 dbginst2 64025748 Jul 23 22:57 firefox-68.0.1.tar.bz2
drwxr-xr-x 2 db2inst2 dbginst2     4096 Aug 12 20:41 test
drwxr-xr-x 2 db2inst2 dbginst2     4096 Aug 12 20:41 test1
db2inst2@db2v10P:~>

Example 2 - 
In this case, all the options have been used; contents of sub directories, hidden files (.temp_Jay, . and .. ), and file size is 62M (human readable form) are all displayed

db2inst2@db2v10P:~> ls -ltrRaih Desktop/
Desktop/:
total 62M
1237686 -rw-r--r--  1 db2inst2 dbginst2  62M Jul 23 22:57 firefox-68.0.1.tar.bz2
1581113 drwxr-xr-x 26 db2inst2 dbginst2 4.0K Aug  9 22:14 ..
1663142 drwxr-xr-x  2 db2inst2 dbginst2 4.0K Aug 12 20:41 test
1663140 drwxr-xr-x  2 db2inst2 dbginst2 4.0K Aug 12 20:41 test1
1638564 -rw-r--r--  1 db2inst2 dbginst2    0 Aug 12 20:45 .temp_Jay
1638704 drwxr-xr-x  4 db2inst2 dbginst2 4.0K Aug 12 20:45 .

Desktop/test:
total 8.0K
1663145 -rw-r--r-- 1 db2inst2 dbginst2    0 Aug 12 20:41 test_F1
1663142 drwxr-xr-x 2 db2inst2 dbginst2 4.0K Aug 12 20:41 .
1638704 drwxr-xr-x 4 db2inst2 dbginst2 4.0K Aug 12 20:45 ..

Desktop/test1:
total 8.0K
1663147 -rw-r--r-- 1 db2inst2 dbginst2    0 Aug 12 20:41 test1_F2
1663146 -rw-r--r-- 1 db2inst2 dbginst2    0 Aug 12 20:41 test1_F1
1663140 drwxr-xr-x 2 db2inst2 dbginst2 4.0K Aug 12 20:41 .
1638704 drwxr-xr-x 4 db2inst2 dbginst2 4.0K Aug 12 20:45 ..
db2inst2@db2v10P:~>

Notes
In the output of  "ls -ltr" command,
  • if the first line is "total 0", then given argument is "empty dir"
  • if the first line is "total n" (where is n is any positive number), then given argument is "non empty dir"
  • if the first line doesn't contain word "total", then given argument is "file"
ls command ends here

######################

Command: man
Purpose: To know details of a command
Options: To know details of a command
Arguments To know details of a command
Syntax: To know details of a command
Example: To know details of a command

######################

cat command begins here
Commandcat

Purpose: to display content of file on standard output(screen) or concatenate contents of files

Options:
  • -n to print line number against each line printed on screen
  • -b to print line number against each non empty line printed on screen
  • -evt to display special characters in the file
Arguments
  1. <filename>
  2. <filename1>  <filename2>
Syntax:
  • cat <filename>
  • cat -[options] <filename1>
  • cat -[options] <filename1>, <filename2>...
  • cat <filename1> <filename2>
  • <standard_output> | cat -[options]
Example:

Let's take "animals" as filename here 
  • cat animals
  • cat -n animals
  • ls -ltr | cat -n   #this will assign a line number to each line in the output of command
cat command ends here

######################

more command begins here

Commandmore

Purpose: display content of file page by page on standard output(screen); this will not return the prompt and will have to hit “q” key to return to prompt

Options:
  • NONE
Arguments
  1. <filename>
Syntax:
  • >more <filename>
Example:

Let's take "animals" as filename here 
  • >more animals
Notes:
Ctrl+f (or) Space bar to move forward page by page
Ctrl +b to move backward page by page
Return key to move forward line by line

more command ends here

###################### 

less command begins here
Commandless (works only in Linux)

Purpose: display content of file page by page on standard output(screen); this will not return the prompt and will have to hit “q” key to return to prompt

Options:
  • NONE
Arguments
  1. <filename>
Syntax:
  • >less <filename>
Example:

Let's take "animals" as filename here 
  • >less animals
Notes:
Ctrl+f (or) Space bar to move forward page by page
Ctrl +b to move backward page by page
Return key to move forward line by line
Down arrow to move forward line by line
Up arrow to move backward line by line

less command ends here

###################### 

head command begins here

Commandhead

Purpose: display top n lines of file on standard output(screen)

Options:
  • By default, head will print first 10 lines of file
  • <number> to display top <number> lines of file
Arguments
  1. <filename>
Syntax:
  • >head <filename>
  • >head –<number> <filename>
  • output | head –<number>
Example:

Let's take "animals" as filename here 
  • head animals  -This will display top 10 lines of "animals" file
  • head -4 animals - This will display top 4 lines of "animals" file
head command ends here

######################

tail command begins here

Commandtail

Purpose: display bottom n lines of file on standard output(screen)

Options:
  • By default, tail will print bottom10 lines of file
  • <number> to display bottom <number> lines of file
Arguments
  1. <filename>
Syntax:
  • >tail <filename>
  • >tail -<number> <filename>
  • output | tail -<number>
Example:

Let's take "animals" as filename here 
  • tail animals  -This will display bottom 10 lines of "animals" file
  • tail -4 animals - This will display bottom 4 lines of "animals" file
tail command ends here

######################

cp command begins here

Commandcp

Purpose: similar to copy and paste in Windows OS. Creates copy of file in target location with a copy in source location

Options:
  • -p to preserve the permissions
  • -R to copy directories reecursively
Arguments
  1. <source_filename> – name of file to be copied
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>.)

Syntax:
  • >cp  <source_filename>  <target_location>
  • >cp  <source_filename >  <target_location>/<target_filename>  (just like Save As in Windows
  • >cp  -R <source_dirname>  <target_location>
  • >cp  -R <source_dirname>  <target_location>/<new_dirname>
Example:

Let's take "animals" as filename here 
  • cp animals . -to copy "animals" file to current directory with same name
  • cp animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name “animals1”
Notes:
  • During cp and mv, usually file will be copied with same name unless a new name is specified
cp command ends here

######################

mv command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

mv command ends here

######################

grep command begins here

Command: grep

Purpose: to search for a string (or) pattern in a file and print the line containing the same

Options:
  • -i case insensitive search
  • -n print the line number along with line
  • -v print all the lines which doesn’t contain matching pattern
  • -c print the count of lines which contain matching pattern/word
  • -w search for  exact match of the pattern/word
  • -E Search for multiple patterns/words  (-E “<pattern1>|<pattern2>”  include pattern in double quotes)
Arguments
  1. <pattern or word or string> -word or string or pattern to be searched
  2. <filename> or <stream of data> 
Syntax:
  • >grep –option(s) <pattern> <filename>
  • >grep –option(s) <pattern> <filename1> <filename2> <filename3>
  • >Stream of data | grep –option(s) <pattern>
Example:
  • >grep db2inst1 /etc/passwd  -this will search for "db2inst1" in /etc/passwd file
  • >ls –ltr | grep ^d -to print all the lines which begin with d (only directories will be listed) in the output of >ls
  • >cat /etc/passwd | grep –i james  -this will  perform a case insensitive search for james in /etc/passwd file. Similar outcome can also be achieved using >grep –i james etc/passwd
grep command ends here

######################

sort command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

sort command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

| pipe
cmd1 | cmd2
Output of cmd1 will be given as input to cmd2



Command
mkdir
Purpose
To create a directory
Options
-p --> to create parent directory if it doesn't exist
Arguments
directory_name
Syntax
mkdir <directory_name>
mkdir <directory_name1> <directory_name2> ... (we can create more than 1 directory in 1 go
Example
mkdir a1  --> This will create a directory a1
mkdir a1 a2 --> This will create two directories a1 and 2
mkdir -p a1/b1 --> This will create a1 followed by b1 if a1 doesn't exist
 


File handling commands (In UNIX/Linux, both File and Directory refer to a file)

1.       

To view contents of files

1.       

 

Monitoring related commands

  1. ps
  2. du
  3. df
  4. free
  5. vmstat
  6. ping
  7. ssh
  8. nslookup
  9. kill
######################

top in Linux /topas in AIX command begins here

Command: top

Purpose: this is analogous to Task Manager in Windows where 

Options:
  • NA
Arguments
  • NA
Syntax:
  • >top or >topas
Notes:
We don't user Options or Arguments

top in Linux /topas in AIX command ends here

######################

ps command begins here

Commandps

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

ps command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

uniq command begins here

Commandmv

Purpose: similar to cut and paste in Windows OS. Creates copy of file in target location with out a copy in source location

Options:
  • NA
Arguments
  1. <source_filename> – name of file to be moved
  2. <target_location> (or) <target_location>\<target_filename> 
(If <target_filename> is not specified, then <source_filename> would be considered as <target_filename>)

Syntax:
  • >mv <source_filename>  <target_location>
  • >mv <source_filename >  <target_location>/<target_filename>
Example:

Let's take "animals" as filename here 
  • >mv animals .  -to copy animals file to current directory with same name
  • >mv animals /tmp/animals1 -to copy animals file in current directory to /tmp with different name "animals1"
Notes:
During cp and mv, usually file will be copied with same name unless a new name is specified

uniq command ends here

######################

kill command begins here

Commandkill

Purpose: to stop a process when it couldn't be stopped gracefully

Options:
  • -9
Arguments
  1. <PID>  (process ID)
Syntax:
  • >kill <PID>
  • >kill -9 <PID>
Example:

>kill -9 1234
here 1234 is the Process ID (PID) of the process to be stopped

kill command ends here

######################

1.     

No comments:

Post a Comment