Monday, January 18, 2010

Learning some commands makes Linux life easier

Redirecting inputs and outputs
It's about these two operator '<' and '>'. For example,

$ ls -l > fileList.txt

The output of this command is not printed on the screen, it is written to a file named fileList.txt.

cat command

Purpose of this command is equivalent to "type" command. You can view contents of a text file using cat command. Example given below,

$ cat fileList.txt 
total 36 
drwxrwxrwx   4 ar       root         512 Dec  1 16:45 HarvardPackages 
-rwxrwxrwx   1 ar       root        1098 Dec  1 16:49 IS_OS161_v1.3.1.sh~ 
-rwxrwxrwx   1 ar       root        1140 Dec  1 16:49 IS_OS161_v1.3.2.sh 
drwxrwxrwx   2 ar       root         512 Dec  1 16:44 Package 1.4 
-rwxrwxrwx   1 ar       root         244 Dec  1 16:49 Scripting.txt 
-rwxrwxrwx   1 ar       root         189 Dec  1 16:49 Scripting.txt~ 
drwxrwxrwx  12 ar       root         512 Dec  1 16:47 cs161-binutils-1.4 
drwxrwxrwx   6 ar       root         512 Dec  1 16:48 cs161-gcc-1.4-2 
drwxrwxrwx  14 ar       root         512 Dec  1 16:44 cs161-gdb-1.4-2 
-rw-r--r--   1 ar       other          0 Dec 26 20:03 fileList.txt 
-rwxrwxrwx   1 ar       root         148 Dec  1 16:44 input.sh 
-rwxrwxrwx   1 ar       root         294 Dec  1 16:49 install.sh~ 
-rwxrwxrwx   1 ar       root         695 Dec  1 16:49 install_os161.sh 
-rwxrwxrwx   1 ar       root         668 Dec  1 16:49 install_os161.sh~ 
drwxrwxrwx  10 ar       root         512 Dec  1 16:49 os161-1.11 
drwxrwxrwx  16 ar       root        1024 Dec  1 16:49 sys161-1.12-2 
-rwxrwxrwx   1 ar       root          24 Dec  1 16:49 test2.txt~ 

grep
Very useful command. It is used to filter the outputs (pipes supported). If you don't understand the purpose in theory don't worry you will be able to understand from examples. If you give the command ls -a it will show like this,

$ ls -a 
total 44 
drwxrwxrwx   9 ar       root         512 Dec 26 20:03 . 
drwxrwxrwx   6 ar       root         512 Dec  1 16:43 .. 
drwxrwxrwx   4 ar       root         512 Dec  1 16:45 HarvardPackages 
-rwxrwxrwx   1 ar       root        1098 Dec  1 16:49 IS_OS161_v1.3.1.sh~ 
-rwxrwxrwx   1 ar       root        1140 Dec  1 16:49 IS_OS161_v1.3.2.sh 
drwxrwxrwx   2 ar       root         512 Dec  1 16:44 Package 1.4 
-rwxrwxrwx   1 ar       root         244 Dec  1 16:49 Scripting.txt 
...........................................................................
...........................................................................
drwxrwxrwx  10 ar       root         512 Dec  1 16:49 os161-1.11 
drwxrwxrwx  16 ar       root        1024 Dec  1 16:49 sys161-1.12-2 
-rwxrwxrwx   1 ar       root          24 Dec  1 16:49 test2.txt~

If the output was huge and wanted details about only the file whose name
contains Package. Then give the command like this:


$ ls -l | grep Package 
drwxrwxrwx   4 ar       root         512 Dec  1 16:45 HarvardPackages 
drwxrwxrwx   2 ar       root         512 Dec  1 16:44 Package 1.4

'|' This sign is called pipe which linked the output of first command to the second as input. You can search for files containing certain text string.

$ grep -r "textString" /path

Use -H switch to show line numbers. grep has many useful options learn them from manual ("$ man grep")

find

In case you know the name of a file but you don't know where it is then to find it's location use this command,

find /directoryname -name file.ext

which
If you apply a command but don't know from where it'll be executed or where the executable file resides? To view that location use this syntax,

which commandName
Example,

$ which gcc 
/usr/sfw/bin/gcc

It is the output for Solaris 10. Hence, your output can be different.

tar

An archive is a file containing compressed files and directories.
  • creating an archive using tar command

    $ tar -cvf test.tar local.login local.profile .bashrc 
    a local.login 1K 
    a local.profile 1K 
    a .bashrc 1K

    Why did we use v switch? Let's see the difference.

    $ tar -cf test.tar local.login local.profile .bashrc

    It means if v is included then tar displays information on every file it adds.

    To add a file in an existing archive use -r option(-f means next argument is
    the archive file name).

    $ tar -rf test.tar local.cshrc

  • Extracting archive,
    If file extension contains tar.gz then syntax is,
         tar xzvf fileName.tar.gz

    If extension is tar.bz2
         tar xjvf fileName.tar.bz2

    if extension is only .tar then
         tar xvf fileName.tar.bz2

echo
This is the easiest command. You know about this. An example is,

$ echo "This line will be printed." 
This line will be printed.

zip and unzip
You can create zip files using zip command. Use following syntax.

$ zip ArchiveFileName.zip /path/dir_OR_file

To unzip a zip file (extract an zip archive) use following syntax

$ unzip ArchiveFileName.zip

To work with rar archives follow this post.

No comments:

Post a Comment