Wednesday, September 28, 2011

Linux commands for Compressing and Archiving File

1. gzip and gunzip
gzip command is used to compress the file, and gunzip is used to de-compress it.
Usage:
gzip <file name>
It provides the extension .gz and removes the original file
Example:
$ wc sample_copy.txt
      65 2776 17333 sample_copy.txt
$ gzip sample_copy.txt
$ wc sample_copy.txt.gz
      26 155 7095 sample_copy.txt.gz

The compression ratio depends on the type, size and nature of the file

Usage:
gunzip <file name with.gz>
Example:
$ gunzip sample_copy.txt.gz
$ /*do ls and you can see the original file*/

If you want to compress the directory contents recursively, use -r option with gzip command and unzip it use the same option with gunzip command.

2. tar : The archival program
tar command is used to create archive that contains a group or file or entire directory structure.
It is generally used for back ups.
Usage:
tar [options] <file1 or dir> . . .

Options:
  • -c Create an archive
  • -x Extract files from archive
  • -t Display files in archive
  • -f arch Name the archive arch
Examples:
$ tar -cvf compression.tar compression
compression/ //v for verbose
compression/temp/
compression/temp/sample2.txt
compression/sample1.txt
  • We can use tar and gzip command in succession to compress the tar file.
$ tar -cvf compression.tar compression
$ gzip compression.tar
$ //will create compression.tar.gz file
  • For un-compression the file first use gunzip command, which will create a tar file and then use tar command to untar the contents
$ gunzip compression.tar.gz
$ tar -xvf compression.tar
  • To just view the contents of the tar file use -t option 
$ tar -tvf compression.tar
  • Instead of doing tar first and then gzip next, we can combine both of them using the option -z
$ tar -cvzf compression.tar.gz compression
compression/
compression/temp/
compression/temp/sample2.txt
compression/sample1.txt
  •  We can de-compress .tar.gz agin in a single command using the option -z with -x
$ tar -xvzf compression.tar.gz
 3. zip and unzip: compressing and archiving
zip command can be used for archiving as well as compressing the contents of the directory or the file.
Usage:
zip [options] output.zip <files to be zipped or directory>
Example:
$ zip sample1.zip sample1.txt
//will create sample1.zip file
  • Use -r option to recursively zip the contents of the directory
$ zip -r compression.zip compression
// will create compression.zip file
  • To un-compress the file use unzip command 
$ unzip compression.zip
// will uncompress the compression.zip file

Linux File Handling Commands

1. cat : Concatenate Files

cat command is used to display the contents of a small file on terminal
Usage:
cat <file name>
Example:

$ cat sample3.txt
Unix (officially trademarked as UNIX, sometimes...


cat when supplied with more than one file will concatenate the files without any header information
Usage: 
$ cat sample3.txt sample4.txt


2. tac : concatenate files in reverse

tac command is used to display the contents of a small file in reverse order on terminal
Usage:
tac <file name>
Example:

$ tac sample3.txt
/*displays sample3.txt in reverse order*/


tac when supplied with more than one file will concatenate the reverse contents of files without any header information
Usage:

$ tac sample3.txt sample4.txt

3. more, less : paging output

more and less commands are used to view large files one page at a time
Usage:
more <file name>
less <file name>
Example:

$ more sample1.txt
/*sample1.txt will be displayed one page at a time */
$ less sample1.txt
/*sample1.txt will be displayed one page at a time */








less is the standard pager for linux and in general less is more powerful than more


4. wc : statistic of file

wc command is used to count lines, words and characters, depending on the option used.
Usage:
wc [options] [file name]
Example:

$ wc sample1.txt
65 2776 17333 sample1.txt
  • Which means sample1.txt file has 65 lines, 2776 words, and 17333 characters
  • you can just print number of lines, number of words or number of charcters by using following options:
    • -l : Number of lines
    • -w : Number of words
    • -c : Number of characters
5. cmp: comparing two files
cmp command is used to compare two files whether they are identical or not
Usage:
cmp <file1> <file2>
Example:
$ cmp sample1.txt sample2.txt
sample1.txt sample2.txt differ: byte 1, line 1
$ cmp sample1.txt sample1_copy.txt
$ /*No output prompt returns back*/
  • The two files are compared byte by byte and the location of the first mismatch is printed on the screen.
  • If two files are identical, then it doesnot print anything on the screen.
6. comm : what is common?
comm command displays what is common between both the files
Usage:
comm <file1> <file2>
Example:
$ comm sample5.txt sample6.txt
  • The input files to comm command should be sorted alphabetically.







Linux file system

1. Standard directory structure
  • / - the topmost
  • /dev - all the devices are accessible as files
  • /var - "variable" data such as mails, log files, databases
  • /usr - almost all the packages installed
  • /etc - con?guration files
  • /home - home directories for all the users
  • /root - home directory of the privileged user root
  • /mnt - used to mount other directories/partitions
2. File Attributes
  • To see the file attributes type ls -l on your terminal
ls -l
total 156-rw-r--r--  1 root root   1053 Sep 22  2010 anaconda-ks.cfg-rw-r--r--  1 root root 127004 Sep 22  2010 CHECKSUMSdrwxr-xr-x  2 root root   4096 Aug  3 00:02 Desktop-rw-r--r--  1 root root    100 Jun  6 22:14 svn-checkout-command
    •  It has 10 characters, first character is d if its directory and - if its file.
    • Next 9 characters are divided into three groups with a set of 3 characters each
    • First 3 characters - Owner of the file or directory
    • Next 3 characters - Group
    • Last 3 characters - Others
    • r - Read i.e. File or directory is readable
    • w - Write i.e. File or directory is writable
    • x - Execute i.e. File or directory is executable
    • -rw-r–r– means it has read, write but not execute permissions for the owner of the file, only read permissions for the group and only read permissions for others.
    • The third column of the command ls -l tells about the owner of the file, next column tells to which group it belongs
3. Changing the File attributes
chmod Changing the permissions of the file
Usage: 
$ chmod o+x Testing.java
$ chmod 655 Testing.java

4. Changing ownership
chown command is used for changing the ownership and also group of the file
Usage:
$ chown guest Testing.java
$ chown guest:guest Testing.java

5. File system commands
  • Deleting Files - rm
  • Copying and moving files - cp, mv
  • Creating directories - mkdir
  • Deleting Empty Directory - rmdir
$ rm Testing.java
//deletes the file Testing.java
$ cp Testing.java Copy.java
//creates the copy of Testing.java
$ mv Testing.java Test.java
//renames the file Testing.java to Test.java
$ mkdir newDir
//Creates directory newDir
$ rmdir newDir
//deletes directory newDir newDir should be empty.