Showing posts with label Linux Commands. Show all posts
Showing posts with label Linux Commands. Show all posts

28 April 2013

Some LS Command Practical Examples

65 comments


ls command is one of the most frequently used command during a Linux Session. Here are some ls command practical examples to make your Linux Command line experience more enjoyable and faster. Iam assuming that you know basic ls usage like ls -l, ls -a, ls -R etc.


Trick 1:

List Only directories in a Current Directory using ls -d */

$ ls -d */
bootstrap/        Documents/     Music/      sandeep/      VirtualBox VMs/

Trick 2:

List Only hidden directories in a Current Directory using ls -d .*/

$ ls -d .*/
./             .gimp-2.8/          .mplayer/          .ssh/ 
../            .gnome2/            .mysql/            .subversion/ 
.adobe/        .gnome2_private/    .nbi/              .swt/ 
Read More...

17 April 2013

Navigate faster with these CD Command Tricks

12 comments

If you are a Terminal freak or use terminal to do most of your task, then it is obvious that you are frequently using cd Command to navigate from one directory to another. Here are some cd tricks which helps you to navigate much more faster and will boost your productivity.

Trick 1:

Move to your home directory from anywhere: cd (without any argument) and cd ~ (Tilda) commands are used to move to user's home directory from any directory in the file system.

$ pwd
/etc/apache2
$ cd
$ pwd
/home/sandeep

$ pwd
/etc/apache
$ cd ~
$ pwd
/home/sandeep

Trick 2:

Toggle between last two directories: cd - (minus) command is used to toggle between last two visited directories.

$ pwd
/home/sandeep
$ cd /etc/apache2
$ pwd
/etc/apache2

$ cd -
$ pwd
/home/sandeep
$ cd -
$ pwd
/etc/apache2
Read More...

07 March 2012

Difference Between External and Internal Command in Linux

3 comments

Linux commands can be broadly classified into Internal and External commands.

Internal commands are those commands which are shell built-in commands. These commands are loaded at the time of booting. Shell does not start separate process to run this commands.

Example: cd , pwd , echo etc.


External commands are those command which are stored as a separate binaries. Shell starts separate sub-process to execute them. Most external commands are stored in the form of binaries in /bin directory. To execute external command shell check $PATH variable . If command present in the   location mentioned in $PATH variable shell will execute it , otherwise it will give error.

How External Commands Located from PATH variable

Suppose output of your PATH variable is like this , if you type any external command , shell search directories specified in the PATH variable like /usr/local/sbin ,/usr/local/bin , /usr/sbin etc. If shell find executable of that command then it will execute it otherwise it will give error.

Note: If a command exist as internal and external command then internal command with same name get top priority.

How to check for External and Internal command

type utility can be used to check whether a command is internal or external.



All those commands which gives "shell builtin" message are internal commands and commands for which some path is displayed are external commands.

That's it.
Enjoy :)

Read More...

01 March 2012

tar Command in Linux and Unix

2 comments

how to compress file in linux using tar command
tar command in Linux/Unix used to create archive on floppy , hard disk or tape. tar command does not delete original files or directories. Lets do some example to understand the functionality of tar command

1. Create an archive using tar command

$ sudo tar -cvfp tar_file.tar directory_name

In the above command

tar : command name
c: create or overwrite archive
v: verbose
f: file name
p: preserve permission
tar_file.tar: Archive name
directory_name: directory to be archived

Example

We want to archive entire Linux except /media directory in / directory.

$ sudo tar -cvpf backup.tar --exclude=/media /

2.Create tar gzipped archive

$ sudo tar -cvfpz backup.tar.gz directory_name

z: archive through gzip.
* .tar.gz is same as .tgz.

3.Create tar bzipped archive

$ sudo tar -cvfpj backup.tar.bz2 directory_name

j: archive through bzip2.
* .tar.bz2 is same as .tbz and .tb2.

Difference between gzip and bzip2

bzip2 take more time in compression and decompression  compare to gzip , but size of bzip2 archive is smaller than gzip.

Note: You can use --exclude option with gzip and bzip2 also.

How to Extract files from different tar archives

Syntax

$ sudo tar -options archive_name -C dest_folder

1. Extracting from *.tar archive

$ tar -xvpf backup.tar -C /home

-x: Extract from archive
-C: Used when you want to specify destination.
/home: extract backup.tar to /home folder.

2. Extracting from *.tar.gz archive

$ tar -xzvpf backup.tar.gz -C /home

-z: uncompressing a gzip archive

3. Extracting from *.tar.bz2 archive

$ tar -xjvpf backup.tar.bz2 -C /home

-j: uncompressing a bzip2 archive

How to list the content of different tar archives


1. List the content of *.tar archive

$ tar -tvf backup.tar

-t: used for listing

2. List the content of *.tar.gz archive

$ tar -tvfz backup.tar.gz

3. List the content of *.tar.bz2 archive

$ tar -tvfj backup.tar.bz2

That's it.
Enjoy :)

Read More...

25 January 2012

Play with history Command in Linux/Unix

3 comments

history command shows the list of commands you had executed in your terminal. This command is useful when you want to execute same command again and again , in that case you can recall that command and save your precious time. Here are some examples of history command

1. Simply Display history of my commands
$ history

1  ls
2  reset
3  cat abc.lst
4  reset
5  cat abc.lst xyz.lst

Note: View command history page by page using following command
$ history|more

1  ls
2  reset
3  cat abc.lst
4  reset
5  cat abc.lst xyz.lst
6  reset
7  cat > pqr.lst
8  cat pqr.lst
Read More...

15 October 2011

grep command in Linux/Unix

2 comments
grep command in Linux/Unix


                               grep (global regular expression parse) is use for searching a pattern in a files or files . grep scan its input for a pattern and display the lines containing the pattern, line number , files which contain the pattern etc.


syntax:


grep [option] pattern file-name(s)


let we have file  sandeep.lst contains following data
$ cat > sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
^C
$


Example


1.  grep "sandeep" sandeep.lst   or   grep sandeep  sandeep.lst     or   grep 'sandeep' sandeep.lst


output:  


$ grep sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000

$ grep "sandeep" sandeep.lst
sandeep | 028 | delhi cantt | 5000

$ grep 'sandeep' sandeep.lst
sandeep | 028 | delhi cantt | 5000
$




*important pattern can be enclosed within single quotes or double quotes . One thing should be remember that if we are using special symbols (like *,?,[]) then double quotes should be used other wise if single quotes used then these symbol has no meaning.

* if possible always use double quotes.


grep options


1.  ignoring case (-i) :  searches for the pattern by ignoring the case .


Example
  
                       grep -i "sandeep" sandeep.lst


output:  


$ grep -i sandeep sandeep.lst
sandeep | 028 | delhi cantt | 5000
SANDEEP|098|rajghat|7000
$





2. deleting lines (-v) : select those lines which does not contain pattern


Example 


                    grep -v "vaibhav" sandeep.lst


output:  
$ grep -v "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
SANDEEP|098|rajghat|7000
$





3. displaying line number (-n) : This option display the line number of the line containing the pattern.


Example
     
                          grep -n "laxmi nagar" sandeep.lst


output:   
$ grep -n "laxmi nagar" sandeep.lst
2:vaibhav | 034 | laxmi nagar | 8000
7:anki | 051 | laxmi nagar | 5000
$





4. counting lines containing pattern (-c) : This option display the number of times pattern occur.


Example  


                         grep -c "laxmi nagar" sandeep.lst


output: 
$ grep -c "laxmi nagar" sandeep.lst
2
$




if -c option used with multiple files then 

Example  


                     grep -c "sandeep" sandeep*.lst

output: 
$ cat > sandeep2.lst
sandeep
vaibhav
^C

$ grep -c "sandeep" sandeep*.lst
sandeep.lst:1
sandeep2.lst:1
$



*above command searches all the files starting with sandeep in the current directory if that files contains the pattern then it display the result.




5. Matching multiple patterns (-e) : Multiple patterns can be matched using this option.


Example


                         grep -e "sandeep" -e "vaibhav" sandeep.lst


output: 
$ grep -e "sandeep" -e "vaibhav" sandeep.lst
sandeep | 028 | delhi cantt | 5000
vaibhav | 034 | laxmi nagar | 8000
$





grep can be used by combining different options together


Example


                grep -vi  "sandeep" sandeep.lst


*This command output all  lines except lines containing sandeep whether it is in lower case , upper case or mixed.


output:  
$ grep -vi "sandeep" sandeep.lst
vaibhav | 034 | laxmi nagar | 8000
anurah | 033 | sahadra | 9000
ankit | 014 | sahadra | 12000
parth | 024 | ip extension | 14000
udit | 007 | ip extension | 2000
anki | 051 | laxmi nagar | 5000
$

Read More...

08 October 2011

cat command in Linux/Unix

Leave a Comment
cat command in Linux/Unix


cat command is one of the command which is used frequently in UNIX/Linux. cat command is used mainly for :-


1. To display content of text file on screen.


2. To concatenate text files .


3. To copy the content of text file.


4. To create new text file.


Syntax :


1 . cat xyz.lst       -- display content of file xyz.lst


2.  cat xyz.lst  abc.lst   -- concatenate the content of two files and display on screen


3.  cat xyz.lst > def.lst   --- copy the content of xyz.lst to def.lst


4.  cat > xyz.lst    -- create new file xyz.lst.


let we have two files abc.lst and xyz.lst


content of abc.lst


my name is xyz
iam a good boy


content of xyz.lst


my name is abc
iam also good boy


now execute commands one by one and see what happen..


1.  cat  abc.lst


output: 
$ cat abc.lst
my name is xyz
iam a good boy
$


2. cat abc.lst xyz.lst


output:
$ cat abc.lst xyz.lst
my name is xyz
iam a good boy
my name is abc
iam also a good boy
$

3. cat abc.lst > def.lst   ---create new file def.lst , copy the content of abc.lst to def.lst . This time no output visible on screen .


To see content of def.lst


-->cat def.lst


output:
$ cat abc.lst > def.lst
$ cat def.lst
my name is xyz
iam a good boy
$


4. cat > pqr.lst [press enter and write what ever you want]
my name is pqr 
iam also a good  person
^C


*press ctrl+c to end file


To see content of pqr.lst


-->cat pqr.lst


output:
$ cat > pqr.lst
my name is pqr
iam also a good person
^C
$ cat pqr.lst
my name is pqr
iam also a good person
$

*cat is very useful when it is used with input/output redirection operators like | ,>>,<,>. This will be discussed later.


Options used with cat command:


let we have file cut1 which contain following lines :

$ cat > cut1
sandeep

vaibhav
anurag
ankit
parth
udit
panwar
^C
$


1. -n --  used to number all lines


       cat -n cut1


output:
$ cat -n cut1
1 sandeep
2
3 vaibhav
4 anurag
5 ankit
6 parth
7 udit
8 panwar
$


     


1. -b --  used to number only non-blank lines.


cat -b cut1


output:
$ cat -b cut1
1 sandeep

2 vaibhav
3 anurag
4 ankit
5 parth
6 udit
7 panwar
$



Read More...

07 October 2011

echo command in Linux/Unix

Leave a Comment
echo command in Linux/Unix


echo command in Linux/UNIX is primarily used to :


 1. To display message ( it is used in shell scripts).
 2. To evaluate shell variables (like echo $PATH)


Examples


1. echo "my name is xyz"     -- double quotes is not mandatory



output : 
$ echo "my name is xyz"
my name is xyz

$ echo my name is sandeep
my name is sandeep
$





2. echo $SHELL


output:

$echo $SHELL
/bin/bash
--this output can be different depending upon the shell you are using.


echo can also be combined with different commands like


1. echo " today date is `date`"


output: 
$ echo "today date is `date`"
today date is Sun Jan 8 13:16:04 IST 2012
$




echo used different escape sequence which are listed below :


* note that all escape sequence in Linux is use with "-e" option other wise it is not recognized by Linux


1. /b  - use for backspace

a. echo " my name is\b xyz"  --- used without -e option


 output : 
$echo "my name is/b xyz"
my name is/b xyz

b. echo -e " my name is\b xyz"    --used with -e option


 output : 
$ echo -e "my name is\b xyx"
my name i xyx




2.  /c - No new line (cursor appear in same line)

 echo -e " my name is xyz \c"


 output: 
$ echo -e "my name is xyz\c"
my name is xyz$

* note that prompt appear in same line along with the output.


3. \n - Newline(same as enter)

echo -e " my name is xyz \n"


 Output:
$ echo -e "my name is xyz\n"
my name is xyz

$




4 . \t - Tab

 echo -e " my\tname\tis\txyz"


 output: 
$ echo -e "my\tname\tis\txyz"
my name is xyz
$



5 .  \f - formfeed

 echo -e " my\fname\fis\fxyz"


output :
$ echo -e "my name\fis\fxyz "
my name
is
xyz
$



other options  are :

 \a - bell
 \r - carriage return
 \\  - backslash





Read More...