Notes: Unix Lab 07

  1. The file command
  2. The which command
  3. The cat command
  4. The head command
  5. The tail command
  6. The more and less commands
  7. Compressing Files
  8. Homework

  1. The file command
    file tests each element passed as an argument to the command and prints the file type back to the screen (standard output). Examples:
     
      
    >>
    >> file /mnt/homes/tuckerm/photos/dsc00375.jpg
    dsc00375.jpg: JPEG image data, EXIF standard 0.73, 10752 x 2048
    >>
    

     
      
    >>
    >> file test.sxw
    test.sxw: Zip archive data, at least v2.0 to extract
    >>
    
    Like many Unix commands, file may use more than one argument:
     
    >>
    >> file money.pdf rails.mpeg
    money.pdf:  PDF document, version 1.4
    rails.mpeg: RIFF (little-endian) data, AVI
    >>
    
    Or wildcards may be used:
     
    >>
    >> file *.mpg
    manualdrop1.mpg: RIFF (little-endian) data, AVI
    trials1.mpg:     RIFF (little-endian) data, AVI
    >>
    
  2. The which command
    which returns the full (absolute) path of a shell command. The which command will search each directory included in your environment variable $PATH. Which only returns the names of executatble files found in the user's path. Files without the executable bit will not be found. Examples:
     
    >>
    >> which ssh
    /usr/bin/ssh
    >>
    >> which ldd ls
    /usr/bin/ldd
    /usr/bin/ls
    >>
    >>  which moo
    >>
    
  3. The head command
    By default, head returns the first ten lines of the file(s). Different numbers of lines can be specified with the the "-n" where "n" is substituted with a number: head -99 somefile.txt head -4 somefile.txt head can also take multiple arguments:
     
    >>
    >>  head -3 junk.pl localback.sh
    ==> junk.pl <==
    #!/usr/bin/perl
    #
    print "testing if conditionals\n";
    
    ==> localback.sh <==
    #!/bin/bash
    #
    # Creates a daily backup to the local disk for general use.
    
    head can also use the wildcard:
     
    >>  head -1 t*.pl
    ==> test.pl <==
    #!/usr/bin/perl
    
    ==> test3.pl <==
    #!/usr/bin/perl
    
    ==> test4.pl <==
    #!/usr/bin/perl
    
    >>
    >>  head -2 test?.pl
    ==> test3.pl <==
    #!/usr/bin/perl
    #
    
    ==> test4.pl <==
    #!/usr/bin/perl
    #
    >>
    

  4. The tail command
    By default, tail returns the last ten lines of the file(s). Like head different numbers of lines can be specified with the the "-n" where "n" is substituted with a number:

    tail -50 somefile.txt

    tail -8 somefile.txt

    tail can also take multiple arguments:

    tail can also use the wildcard but you must use the "--lines -n" option:
     
    >>
    >>  tail --lines -1 t*.pl
    >>
    >>
    >>  tail --lines -2 test?.pl
    >>
    

  5. The cat command
    cat displays the contents of a file to the terminal (standard output). If multiple files or wildcards are given as arguments then the outputs are streamed together to the terminal.

    cat somefile.txt

    cat *.txt

    cat one.txt two.txt

  6. The more and less commands These commands allow the contents of a file or stream to be viewed on the terminal so that one page at a time may be viewed. Both allow basic text searching by using the "/" key and entering the text string to search for.

    less includes additional functionality such as the ability to scroll backwards as well as forewards. less also allows right to left scrolling and advancing one line at a time using the cursor keys (arrow keys).

    For either program, the space-bar or page-down will scroll down and the "q" key quits out of the program.

    more somefile.txt

    less somefile.txt

  7. Compressing files with tar, gzip and compress
    compression:

    Compression is used to reduce the size of a file for storeage or transmission on a network.

    • compress is the older of the compression utilities. It typically will append a ".Z" to the file name of a compressed file although this is not required. uncompress is used to decompress a compressed file. It is not installed on the ATM system, but is included here as an example.
       
      >> 
      >> ls -l zipback.sh
      -rwxr--r--    1 mark     users        1120 Feb 10 07:51 zipback.sh
      >> compress zipback.sh
      >> ls -l zipback.sh.Z
      -rwxr--r--    1 mark     users         675 Feb 10 07:51 zipback.sh.Z
      >> uncompress zipback.sh.Z
      >> ls -l zipback.sh
      -rwxr--r--    1 mark     users        1120 Feb 10 07:51 zipback.sh
      >>
      
    • gzip is somewhat newer than compress and provides significantly better compression. gzip typically appends the ".gz" extension to the file name during compression. gunzip or gzip -d are used to decompress a gzipped file and will remove the ".gz" filename extension.
       
        
      >>
      >> ls -l zipback.sh
      -rwxr--r--    1 mark     users        1120 Feb 10 07:51 zipback.sh
      >> gzip zipback.sh
      >> ls -l zipback.sh.gz
      -rwxr--r--    1 mark     users         479 Feb 10 07:51 zipback.sh.gz
      >> gunzip zipback.sh.gz
      >> ls -l zipback.sh
      -rwxr--r--    1 mark     users        1120 Feb 10 07:51 zipback.sh
      >>
      
    • bzip2 is a less commonly used compression utility but is still seen occasionally. It provides much higher levels of compress on larger data files but is slower in accomplishing its compression. bzip2 will append the filename extension ".bz2" to the filename during compression. bunzip2 or bzip2 -d are used to decompress a bzip2 compressed file. The ".bz2" filename extension is removed during decompression.
    • archive with tar:
      tar (tape archive) is historically used to archive files and directories to a tape device. Currently, it is more commonly used to archive files and directories to a file file on the file system. This archive, referred to as a "tarfile" can then be expanded at a different location on the file system or on a different host using the tar command with a different set of options.

      options:
      -c create an archive
      -v verbose output
      -f file to create or exectract
      -x extract contents of an archive
      -z gzip archive during creation or decompress during exctraction

      To create a tar archive:
      tar -cvf tarfile.tar /paths/to/archive /files/to/store.txt

      Using wildcards:
      tar -cvf tarfile.tar *.dat

      Extracting an archive:
      tar -xvf tarfile.tar

      Creating a tar archive with compression:
      To create a compressed tar archive of all *.txt files:
      tar -czvf tarfile.tar.gz *.txt

      Extracting a gzip compressed archive:
      tar -xzvf tarfile.tar.gz

      Listing the contents of an archive:
      tar -tvf tarfile.tar