Sunday, September 6, 2015

zip , gzip, unzip, gunzip, tar and family Linux

Archiving files is a very important and useful task in real-world day to day use, tools like Win-zip or Win-Rar are probably the most known and well known archive tools for Windows system, Linux on uses a separate set of tools for this purpose, lets look at some of those.

Lets us consider we want to archive a set of files ( in *nix almost everything is a file ) as below.
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
we can use tar to create an archive of it using below command.
bash-$
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$ tar -cf archive.tar *
bash-$
bash-$
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$ 
Where -c indicates creation of archive identified by option -f with name archive.tar .
To see contents of archive file archive.tar

bash-$
bash-$ tar -tf archive.tar
directory1/
directory2/
directory3/
directory4/
file1/
file2/
file3/
bash-$ 


you can get more information on this using file command.
bash-$ file archive.tar
archive.tar: POSIX tar archive (GNU)
bash-$ 

To extract archive file and retrieve contents -x option can be issues along with -f and archive file.
bash-$
bash-$ ls
archive.tar
bash-$ tar -xf archive.tar
bash-$ ls
archive.tar  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
Existing tar files can be compressed further using gzip command.
bash-$ gzip archive.tar
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
gzip produces a more compressed archive file in terms of size.

extracting gzipped file to its original tar can be done by using gunzip, followed by tar with -x to get original contents.

bash-$
bash-$ ls
archive.tar.gz
bash-$
bash-$ gunzip archive.tar.gz
bash-$
bash-$ ls
archive.tar
bash-$
bash-$ tar -xf archive.tar
bash-$
bash-$ ls
archive.tar  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
bash-$ 

in fact,   tar can be used to generate/extract zipped archive directly using -z option
bash-$ tar -czf archive.tar.gz *
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ rm directory* file* -rf
bash-$
bash-$ ls
archive.tar.gz
bash-$
bash-$ tar -xzf archive.tar.gz
bash-$ ls
archive.tar.gz directory1  directory2 directory3  directory4 file1  file2  file3
bash-$ 
similar operation can be performed using zip and unzip commands

Zip files to create an archive ( note -r option for recursive zipping )
bash-$
bash-$ ls
directory1  directory2 directory3  directory4 file1  file2  file3
bash-$
bash-$
bash-$ zip -r archive.zip *
  adding: directory1/ (stored 0%)
  adding: directory2/ (stored 0%)
  adding: directory3/ (stored 0%)
  adding: directory4/ (stored 0%)
  adding: file1/ (stored 0%)
  adding: file2/ (stored 0%)
  adding: file3/ (stored 0%)
bash-$
bash-$ ls
archive.zip  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$ 

unzip to extract zipped archives
bash-$
bash-$ rm directory* file* -rf
bash-$
bash-$
bash-$ ls
archive.zip
bash-$
bash-$
bash-$ unzip archive.zip
Archive:  archive.zip
   creating: directory1/
   creating: directory2/
   creating: directory3/
   creating: directory4/
   creating: file1/
   creating: file2/
   creating: file3/
bash-$
bash-$ ls
archive.zip  directory1  directory2  directory3  directory4  file1  file2  file3
bash-$
bash-$ 



aliases in Linux

Aliasing in Linux simplifies most tasks by providing simple short-cuts to commands and paths. Lets take a look at how aliasing works and how aliasing can be done.

Step-1:
           Check available aliases in system. below command should provide list of existing aliases.
No Aliases defined 
bash-$
bash-$ alias
bash-$
bash-$ 


List of Aliases
bash-$
bash-$ alias
alias ls='ls --color=auto'
alias rm='rm -i'
alias temp='cd /tmp'
bash-$
 
Step-2: 
           Defining alias is all about simplifying tasks ( well not necessarily, aliases can be defined for fun as well ). Let us consider a user named break-it is working under a directory name, using files frequently under directories manuals, sources and run as below
/home/break-it/Documents/space/work-space/
/home/break-it/Documents/space/work-space/references/samples/manuals
/home/break-it/Documents/space/work-space/sources/programming-sources
/home/break-it/Documents/space/work-space/binaries/work-binary/ru
n
Navigation is probably done with cd either with a relative path of absolute path, typing the entire path every time there is a change in directory is unavoidable. aliases will resolve this pain with ease. adding below lines to .bashrc with a unique name to each individual command will create aliases for each of the path.
alias ws='cd /home/break-it/Documents/space/work-space/'
alias manuals='cd /home/break-it/Documents/space/work-space/references/samples/manuals'
alias srcs='cd /home/break-it/Documents/space/work-space/sources/programming-sources'
alias run='cd /home/break-it/Documents/space/work-space/binaries/work-binary/run'
reload bashrc using below command
bash-$ source ~/.bashrc
bash-$
bash-$
bash-$ alias
alias manuals='cd /home/break-it/Documents/space/work-space/references/samples/manuals'
alias run='cd /home/break-it/Documents/space/work-space/binaries/work-binary/run'
alias srcs='cd /home/break-it/Documents/space/work-space/sources/programming-sources'
alias ws='cd /home/break-it/Documents/space/work-space/'
bash-$ 
as you can see aliases are now created for navigating to reach directory simply type aliases on terminal followed enter to navigate to specified directory.
bash-$
bash-$ manuals
bash-$ pwd
/home/break-it/Documents/space/work-space/references/samples/manuals
bash-$
bash-$ run
bash-$ pwd
/home/break-it/Documents/space/work-space/binaries/work-binary/run
bash-$
bash-$ srcs
bash-$ pwd
/home/break-it/Documents/space/work-space/sources/programming-sources
bash-$ 
Step-3:
Here is a list of sample aliases which are quite useful. Below alias will help in searching for a given file 
alias mfind='find ./ -name '
             Example for searching for a file that end with config.
bash-$ mfind *config
./usr/lib/klibc/bin/ipconfig
./usr/lib/X11/rstart/config
./usr/lib/python2.7/dist-packages/numpy/core/lib/npy-pkg-config
./usr/lib/x86_64-linux-gnu/ImageMagick-6.7.7/config
./usr/lib/x86_64-linux-gnu/pkgconfig
./usr/lib/compizconfig
./usr/lib/smlnj/lib/pathconfig

or the below alias here will show disk-usage under a given directory
alias disk_usage='sudo du -skh *'
Example output as follows when issues under /var directory
bash-$ cd /var/
bash-$ disk_usage
5.6M backups
774M cache
28K crash
26G lib
4.0K local
0 lock
8.8M log
4.0K mail
4.0K metrics
4.0K opt
0 run
60K spool
4.0K tmp
bash-$ 





Saturday, September 5, 2015

Customize bash prompt

Navigating in a Linux using shell can be a bit tedious, especially considering user sometimes will not be aware of his/her current working directory.

some of the examples of bash prompts looks like below.
$
#
bash::$
sh:$
sh:#
etc etc etc.

each of these prompts can be customized to provide a more meaningful prompt, an example shown below
[break-it@foo-bar-host ~]$ 
  break-it      - Current User Name
 foo-bat-host  - host name
  ~            - current working directory
on a whole translates to break-it is now logged on to system foo-bar-host working under home directory ( ~ is nothing but user`'s HOME )
Below are a few steps needed to customize bash prompt

Step-1: Login to terminal which probably looks like this.
$
$
Step-2:  lets start modifying type below lines on bash prompt and press enter
$
$ PS1="my-bash-$ "
my-bash-$
my-bash-$ 
There you go the new bash prompt. The trick here is to provide what ever you want as prompt to PS1 variable. for instance lets modify the prompt a bit more.
my-bash-$
my-bash-$ PS1='Hey, `whoami` Type-in your command Here # '
Hey, break-it Type-in your command Here #
Hey, break-it Type-in your command Here #
or change to this
$
$
$ PS1='[`whoami` `date +%F`] #'
[break-it 2015-09-06] #
[break-it 2015-09-06] #
or this

$$ PS1='[`whoami` `date +%F`] #'[break-it@foo-bar-host ~] #

Step-3:   Finally once done on selecting bash prompt copy paste the lines in to ~/.bashrc file.
            Here is a example of bashrc file contents.
myname=`whoami`
myhost=`hostname`

export PS1='[$myname@$myhost `pwd`]$ '

That's all you now have a customized bash prompt ready.


Add users to sudoers list

In a *nix environment it is prohibited to execute few commands unless given user belongs to a specific group or himself a super-user or otherwise. to overcome this *nix provides sudo.

as a basic definition ( from manual pages )
"sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy."

Here are a few steps to make a user part of this sudo set-up which will enable a user to execute almost all commands that a super user can using sudo prefix.

Step-1 : Make sure given user is not in sudoers list. though there are a number of ways to check if one user is allowed to issue a sudo command perhaps the most easiest way is to switch to super-user.
$ sudo su -
[sudo] password for break-it:
break-it is not in the sudoers file.  This incident will be reported.
Other options include commands such as
 $ sudo -l
[sudo] password for break-it:
Sorry, user break-it may not run sudo on foo-bar-host-name.

Step-2: Ensure you have root privileges or part of a group which has sudo privileges. issue one of the below commands depending on which user you are logged in as.
if root :
$ vi /etc/sudoers
else in case of special user with sudo permissions
$ sudo vi /etc/sudoers
Step-3:  Find out a section in sudoers file with line as
# User privilege specification 
Probably followed by few lines like these
# User privilege specification
root    ALL=(ALL:ALL) ALL
foo     ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
Now all you need to do is to add the username to given list as below ( lets say the user in our case is break-it ), final file after edit will look like this.
# User privilege specification
root    ALL=(ALL:ALL) ALL
kk      ALL=(ALL:ALL) ALL
break-it ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
Save file ( by typing Esc, :wq ) and that's it, go to step-1 and type the same command, user will be able to switch to super-user mode without any problem.

$sudo su -
[sudo] password for break-it:
root@foo-bar-hostname ~ #


for more information on adding groups to sudoers list check manual pages.
$man -a sudo 







Install Wireshark on Ubuntu 14.04

Navigating in a Linux using shell can be a bit tedious, especially considering user sometimes will not be aware of his/her current working directory.

some of the examples of bash prompts looks like below.
$
#
bash::$
sh:$
sh:#
etc etc etc.

each of these prompts can be customized to provide a more meaningful prompt, an example shown below
[break-it@foo-bar-host ~]$ 
  break-it      - Current User Name
 foo-bat-host  - host name
  ~            - current working directory
on a whole translates to break-it is now logged on to system foo-bar-host working under home directory ( ~ is nothing but user`'s HOME )
Below are a few steps needed to customize bash prompt

Step-1: Login to terminal which probably looks like this.
$
$
Step-2:  lets start modifying type below lines on bash prompt and press enter
$
$ PS1="my-bash-$ "
my-bash-$
my-bash-$ 
There you go the new bash prompt. The trick here is to provide what ever you want as prompt to PS1 variable. for instance lets modify the prompt a bit more.
my-bash-$
my-bash-$ PS1='Hey, `whoami` Type-in your command Here # '
Hey, break-it Type-in your command Here #
Hey, break-it Type-in your command Here #
or change to this 
$
$
$ PS1='[`whoami` `date +%F`] #'
[break-it 2015-09-06] #
[break-it 2015-09-06] #
or this

$$ PS1='[`whoami` `date +%F`] #'[break-it@foo-bar-host ~] #

Step-3:   Finally once done on selecting bash prompt copy paste the lines in to ~/.bashrc file.
            Here is a example of bashrc file contents.
myname=`whoami`
myhost=`hostname`

export PS1='[$myname@$myhost `pwd`]$ '

That's all you now have a customized bash prompt ready.