Saturday 30 March 2013

The Ultimate Tar Command Tutorial with 10 Practical Examples


The Ultimate Tar Command Tutorial with 10 Practical Examples

by SATHIYAMOORTHY on APRIL 26, 2010



On Unix platform, tar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation.
In this article, let us review various tar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc.,

1. Creating an archive using tar command

Creating an uncompressed tar archive using option cvf

This is the basic command to create a tar archive.
$ tar cvf archive_name.tar dirname/
In the above command:
  • c – create a new archive
  • v – verbosely list files which are processed.
  • f – following is the archive file name

Creating a tar gzipped archive using option cvzf

The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.
$ tar cvzf archive_name.tar.gz dirname/
  • z – filter the archive through gzip
Note: .tgz is same as .tar.gz
Note: I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.

Creating a bzipped tar archive using option cvjf

Create a bzip2 tar archive as shown below:
$ tar cvfj archive_name.tar.bz2 dirname/
  • j – filter the archive through bzip2
gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
Note: .tbz and .tb2 is same as .tar.bz2

2. Extracting (untar) an archive using tar command

Extract a *.tar file using option xvf

Extract a tar file using option x as shown below:
$ tar xvf archive_name.tar
  • x – extract files from archive

Extract a gzipped tar archive ( *.tar.gz ) using option xvzf

Use the option z for uncompressing a gzip tar archive.
$ tar xvfz archive_name.tar.gz

Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf

Use the option j for uncompressing a bzip2 tar archive.
$ tar xvfj archive_name.tar.bz2
Note: In all the above commands v is optional, which lists the file being processed.

3. Listing an archive using tar command

View the tar archive file content without extracting using option tvf

You can view the *.tar file content before extracting as shown below.
$ tar tvf archive_name.tar

View the *.tar.gz file content without extracting using option tvzf

You can view the *.tar.gz file content before extracting as shown below.
$ tar tvfz archive_name.tar.gz

View the *.tar.bz2 file content without extracting using option tvjf

You can view the *.tar.bz2 file content before extracting as shown below.
$ tar tvfj archive_name.tar.bz2

4. Listing out the tar file content with less command

When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article Open & View 10 Different File Types with Linux Less Command — The Ultimate Power of Less.

5. Extract a single file from tar, tar.gz, tar.bz2 file

To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.
$ tar xvf archive_file.tar /path/to/file
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/file

$ tar xvfj archive_file.tar.bz2 /path/to/file

6. Extract a single directory from tar, tar.gz, tar.bz2 file

To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.
$ tar xvf archive_file.tar /path/to/dir/
To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.
$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/dir/

$ tar xvfj archive_file.tar.bz2 /path/to/dir/

7. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression

You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.
$ tar xvf archive_file.tar --wildcards '*.pl'
Options explanation:
  • –wildcards *.pl – files with pl extension

8. Adding a file or directory to an existing archive using option -r

You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:
$ tar rvf archive_name.tar newfile
This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,
$ tar rvf archive_name.tar newdir/
Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.
$ tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

9. Verify files available in tar using option -W

As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.
$ tar cvfW file_name.tar dir/
If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.
$ tar tvfW file_name.tar
Verify 1/file1
1/file1: Mod time differs
1/file1: Size differs
Verify 1/file2
Verify 1/file3
If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.
Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.
Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.
Finding the difference between gzip archive file and file system
$ tar dfz file_name.tgz
Finding the difference between bzip2 archive file and file system
$ tar dfj file_name.tar.bz2

10. Estimate the tar archive size

The following command, estimates the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c
20480
The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.
$ tar -czf - /directory/to/archive/ | wc -c
508

$ tar -cjf - /directory/to/archive/ | wc -c
428

Friday 29 March 2013

XAMPP: Another web server daemon with SSL is already running

This is a peculiar error you will get if you run XAMPP in LINUX.



Assumption: XAMPP is unzipped @ /opt/ folder.

If you get the error "XAMPP: Another web server daemon with SSL is already running" when you run "./lampp start". Simply follow the steps below to get rid of this error:

1. Open the file /opt/lampp/etc/httpd.conf
2. Search the "Listen 80" and change it to some other port (e.g. Listen 2145)  (Line No. 40)
3. Open the file /opt/lampp/etc/extra/httpd-ssl.conf
4. Search the "Listen 443" and change it to some other port (e.g. Listen 16443) (Line No. 39)
5. Open the file "/opt/lampp/lampp"
6. Search for the port "testport 80" and replace it to "testport 2145". Also change the "testport 443" to "testport 16443". (Happens to be the Line No. 197, 214)
7. Now go and run "/opt/lampp/lampp start". (It should work now).




Hope this Helps :-)

Saturday 23 March 2013

How to Setup Chroot Directory Structure ...



DebootstrapChroot



This article demonstrates a quick and easy way to create a chroot environment on an Ubuntu computer, which is like having a virtual system without the overhead of actual virtualization.
A chroot can be used for things like:
  • Running a 32-bit Firefox browser or a 32-bit Wine bottle on a 64-bit system.
  • Trying an older or newer Ubuntu release without reinstalling the operating system.
  • Trying a Debian release or other distribution derived from Debian.
  • Cross compiling and building packages for a different platform like Launchpad or Soyuz does it.

Example Configuration


In this example, we use a current Ubuntu 9.04 Jaunty system (the "host") to create a chroot for the older Ubuntu 8.04 Hardy release (the "target"). We are arbitrarily naming the new chroot environment hardy_i386 and putting it in the /srv/chroot directory on the host system.

Step 1: Install packages on the host computer.


First, install debootstrap, which is a utility that downloads and unpacks a basic Ubuntu system:

 $ sudo apt-get install debootstrap


Second, install schroot, which is a utility that wraps the regular chroot program and automatically manages chroot environments:

 $ sudo apt-get install schroot


Note: The debootstrap utility is usually backwards compatible with older releases, but it may be incompatible with newer releases. For example, the debootstrap that is bundled with Jaunty can prepare a Hardy chroot like we are doing here, but the debootstrap that is bundled with Hardy cannot prepare a Jaunty chroot.

If you have any difficultly with a debootstrap version mismatch, then visit http://packages.ubuntu.com/ to manually download and install the debootstrap package on the host system from the repository for the target release.

Step 2: Create a configuration file for schroot.


Choose a short name for the chroot, we use hardy_i386 in this example, and create a configuration file for it like this:

  sudo editor /etc/schroot/chroot.d/hardy_i386.conf


Note: In lucid the filename must not contain '.' , it should be lucid_i386_conf.
Put this in the new file:

  [hardy_i386]
  description=Ubuntu 8.04 Hardy for i386
  location=/srv/chroot/hardy_i386
  #personality=linux32
  root-users=bob
  run-setup-scripts=true
  run-exec-scripts=true
  type=directory
  users=alice,bob,charlie


Note: if you copy this example to your clipboard, be careful to start each line in column 1 before you save the new file! If you forget, the command schroot -l will fail with an error, e.g. 

E:/etc/schroot/chroot.d/hardy_i386.conf: line 0: Invalid line: “  [hardy_i386]”.

Note: for lucid use directory instead of location, e.g. directory=/srv/chroot/hardy_i386 .
Change these things in the example configuration file to fit your system:
  • location: This should be a directory that is outside of the /home tree. The latest schroot documentation recommends /srv/chroot.
  • personality: Enable this line if the host system is 64-bit running on an amd64/x64 computer and the chroot is 32-bit for i386. Otherwise, leave it disabled.
  • users: These are users on the host system that can invoke the schroot program and get access to the chroot system. Your username on the host system should be here.
  • root-users: These are users on the host system that can invoke the schroot program and get direct access to the chroot system as the root user.
Note: Do not put whitespace around the '=' character, and do not quote strings after the '=' character.

Step 3: Run debootstrap.


This will download and unpack a basic Ubuntu system to the chroot directory, similar to what the host system already has at the real root directory ("/").

 $ sudo mkdir -p /srv/chroot/hardy_i386
 $ sudo debootstrap --variant=buildd --arch=i386 hardy /srv/chroot/hardy_i386 http://archive.ubuntu.com/ubuntu/


This command should work for any distribution that is derived from Debian. Substitute the architecture "i386", the release name "hardy", and the repository address "http://archive.ubuntu.com/ubuntu/" appropriately. For example, do this to get the 64-bit build of Hardy instead of the 32-bit build:

  $ sudo debootstrap --arch=amd64 hardy /srv/chroot/hardy_amd64/ http://archive.ubuntu.com/ubuntu/


Note: Remember to change all instances of hardy_i386 to hardy_amd64 in the configuration file and on the command line if you actually do this.
Do something like this to get an upstream Debian release:

  $ sudo debootstrap --arch=amd64 sid /srv/chroot/sid_amd64/ http://ftp.debian.org/debian/


If trouble arises, debootsrap accepts a --verbose flag that may provide further insight.

Step 4: Check the chroot


This command lists configured chroots:tro
  $ schroot -l


If hardy_i386 appears in the list, then run:
  $ schroot -c hardy_i386 -u root


Note: This should work without using sudo to invoke the schroot program, and it should result in a root prompt in the chroot environment. Check that the root prompt is in a different system:

  # lsb_release -a


For the Hardy system that we just built, the lsb_release command should print:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 8.04
Release:        8.04
Codename:       hardy

We're done!

WARNING


For convenience, the default schroot configuration rebinds the /home directory on the host system so that it appears in the chroot system. This could be unexpected if you are familiar with the older dchroot program or the regular chroot program because it means that you can accidentally delete or otherwise damage things in /home on the host system.

To change this behavior run:
  $ sudo editor /etc/schroot/mount-defaults


And disable the /home line so that the file reads:

  # mount.defaults: static file system information for chroots.
  # Note that the mount point will be prefixed by the chroot path
  # (CHROOT_PATH)
  #
  # <file system> <mount point>   <type>  <options>       <dump>  <pass>
  proc            /proc           proc    defaults        0       0
  /dev/pts        /dev/pts        none    rw,bind         0       0
  tmpfs           /dev/shm        tmpfs   defaults        0       0
  #/home           /home           none    rw,bind         0       0
  /tmp            /tmp            none    rw,bind         0       0


The mount.defaults file is the /etc/fstab for chroot environments.

Hints


Install the ubuntu-minimal package in a new chroot after you create it:

  $ schroot -c hardy_i386 -u root
  # apt-get install ubuntu-minimal

If you get locale warnings in the chroot like "Locale not supported by C library." or "perl: warning: Setting locale failed." , then try one or more of these commands:

  $ sudo dpkg-reconfigure locales

  $ sudo apt-get install language-pack-en

  $ locale-gen en_US.UTF-8


If your preferred language is not English, then change "-en" and "en_US" appropriately.
As of Lucid, schroot has changed in these ways:
  • The file should be named: /etc/schroot/chroot.d/hardy-i386
  • The keywords in the file have changed and some have been deprecated. Additionally, keywords have to start at the beginning of the line. The file should read:
  [hardy-i386]
  description=Ubuntu 8.04 Hardy for i386
  directory=/srv/chroot/hardy-i386
  #personality=linux32
  root-users=bob
  type=directory
  users=alice,bob,charlie


As of Maverick schroot has further changed in these ways:
  • The configuration file should be stored in /etc/schroot/

TLDR


There's a much simplier way to get a basic chroot environment from an ISO image; if the text above seems TLDR, try this.

First of all, install Ubuntu Customization Kit:

  $ sudo apt-get install uck


Then set the directory in which you want to create the chroot environment:
  $ export BASEDIR=/path/to/chroot/directory/


Unpack the ISO image (this may take quite some time):
  $ sudo uck-remaster-unpack-iso /path/to/your/image.iso "$BASEDIR" && sudo uck-remaster-unpack-rootfs "$BASEDIR" && sudo uck-remaster-unpack-initrd "$BASEDIR"


You're done! Now, to enter the chroot environment, just execute
  $ sudo uck-remaster-chroot-rootfs /path/to/chroot/directory/


every time you wish to enter the chroot console. To leave it, type "exit".
To be able to run X applications, e.g. gedit, run

  # HOME=/root


in the chroot environment. 

Thursday 21 March 2013

How to Disable Guest Account Login on Ubuntu



By default ubuntu 12.04 comes with guest account.You can disable this account using the following procedure.Guest account is a paswordless account which allow users to get access to Ubuntu machine


Open /etc/lightdm/lightdm.conf file from your terminal using the following command
gksudo gedit /etc/lightdm/lightdm.conf
Add the following line
allow-guest=false
Save and exit the file
After adding the above line you should see similar to the following in lightdm.conf file
[SeatDefaults]
user-session=ubuntu
greeter-session=unity-greeter
allow-guest=false
Finally you have to restart lightdm using the following command from your terminal
sudo restart lightdm
Note:- After executing above command all graphical programs running will be close

Mastering Docker Minified Systems: A Step-by-Step Guide with Real Use Cases

Introduction Docker is a powerful platform for developing, shipping, and running applications. Minified Docker systems are optimized for siz...