Flipkart

Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Wednesday, 15 March 2017

TAR Command in LINUX/UNIX

In this Mid-Week article, I am going to share/remind few useful things that can be done using TAR command in Linux/UNIX.

What is TAR stands for?
Tape ARchive==> A Utility which helps to create single file called an ""archive" or a TAR file from number of individual files in Linux/UNIX.

->The contents of this archive can be only the names of the individual files used to create it but not the actual data those file have.

Creating an archive or a TAR file:

Example:

In the below example, I have created f1.out, f2.out and f3.out files with some text.

[root@server2]# ls -lrth
total 12K
-rw-r--r-- 1 root root 317 Mar 15 11:10 f1.out
-rw-r--r-- 1 root root 579 Mar 15 11:11 f2.out
-rw-r--r-- 1 root root 508 Mar 15 11:11 f3.out


Then I used below command to create a tar file which contains the above three files

[root@server2]# tar -cvzf File.tar.gz f1.out f2.out f3.out
f1.out
f2.out
f3.out

[root@server2]# ll
total 16
-rw-r--r-- 1 root root 317 Mar 15 11:10 f1.out
-rw-r--r-- 1 root root 579 Mar 15 11:11 f2.out
-rw-r--r-- 1 root root 508 Mar 15 11:11 f3.out
-rw-r--r-- 1 root root 857 Mar 15 11:17 File.tar.gz


File.tar.gz is the command file that was created using the above TAR command.

Why should we TAR a file?

In my experience I used TAR utility to move bunch of files in compressed mode up to the 1/3rd of the original size of the data with a single command.
==>So, suppose I have 10 files combined have a data of 30 GB, I can make a single TAR file that has a size of just 10 GB and move to the target

How to UnTAR an archive or a TAR file?

#tar -xvzf File.tar.gz
==>The above command gives you back the original files with their actual sizes in the target directory you wish either on local or on network.

Example:

[root@server2]# tar -xvzf File.tar.gz
f1.out
f2.out
f3.out

What other things TAR can do?



Scenario: Suppose you have received a TAR file from one of your superior or a colleague, but you want to know what that file actually has before opening that file. How?

#tar -tvf
==>The above command actually shows the names of the files that the above TAR file or archive has.

[root@server2]# tar -tvf File.tar.gz
-rw-r--r-- root/root       317 2017-03-15 11:10:27 f1.out
-rw-r--r-- root/root       579 2017-03-15 11:11:07 f2.out
-rw-r--r-- root/root       508 2017-03-15 11:11:29 f3.out


-So, if the files above seen are relevant to you, you can think of un-tarring the actual contents. If not, the file could be a malware or virus or anything that could be marked as danger.

Feel free to comment or add more details in the comment's section.

Little follow up work :
You can also break a big tar file into parts for easy uploads, just think of it or try to get the information :).

As always, refer man page of TAR to get required information.

#man tar

HAPPY LINUX LEARNING :)

Easy search from Google:  
Use the text in quotes shown below to get into my blog quickly:
"linuxunixdatabase blogger"

Tuesday, 21 February 2017

Learning AWK and SED Tools for LINUX/UNIX

GOAL:

Learning AWK and SED Tools for LINUX/UNIX:


I have posted so far L2 level of Linux issues and the processes to fix them.
  • In this post I would like to take something simple but still interesting.
  • It is sometimes may be important to have some kind of scripting knowledge in hand for a better Linux/Unix system administration.
  • Before I post something on scripting with examples, I would like to introduce two command line tools namely AWK and SED which are really powerful in LINUX/Unix world.
  • So, this post has the information about most frequently used data filtering commands in Linux/Unix world, none other than AWK and SED.
What is AWK and why is it used for?

Awk is both a programming language and text processor that can be used to manipulate text data in desirable manner.

Exercise using AWK:

To exercise a scenario with AWK, let's take a file called myfile.txt which has the below information.
=======
root x 0 0 root /root /bin/bash
bin x 1 1 bin /bin /sbin/nologin
Albert x 2 2 daemon /sbin /sbin/nologin
Chin x 3 4 adm /var/adm /sbin/nologin
Neon x 4 7 lp /var/spool/lpd /sbin/nologin
sync x 5 0 sync /sbin /bin/sync
shutdown x 6 0 shutdown /sbin /sbin/shutdown
halt x 7 0 halt /sbin /sbin/halt
mail x 8 12 mail /var/spool/mail /sbin/nologin
=======
  • In the above data, first column shows user name, and last column(7th column) shows login details like whether bash login is allowed or not.
  • From the above output I Just need to know the user names and corresponding login details.
Here is how AWK is used to get the required output.

#cat myfile.txt | awk '{print $1" "$7}'
root /bin/bash
bin /sbin/nologin
Albert /sbin/nologin
Chin /sbin/nologin
Neon /sbin/nologin
sync /bin/sync
shutdown /sbin/shutdown
halt /sbin/halt
mail /sbin/nologin
If I want to separate the output using a colon, I can use the below command:
# cat myfile.txt | awk '{print $1" : "$7}'
root : /bin/bash
bin : /sbin/nologin
Albert : /sbin/nologin
Chin : /sbin/nologin
Neon : /sbin/nologin
sync : /bin/sync
shutdown : /sbin/shutdown
halt : /sbin/halt
mail : /sbin/nologin
We can separate the columns as per our convenience like below:
 #cat myfile.txt | awk '{print $1" ==> "$7}'
root ==> /bin/bash
bin ==> /sbin/nologin
Albert ==> /sbin/nologin
Chin ==> /sbin/nologin
Neon ==> /sbin/nologin
sync ==> /bin/sync
shutdown ==> /sbin/shutdown
halt ==> /sbin/halt
mail ==> /sbin/nologin
--------------

In this same article, I would like to introduce SED as well.

What is SED?
  • SED is a stream editor. 
  • SED is used to perform basic text transformations on an input stream like  a file or input from a pipeline. SED works by making only one pass over the input(s), and is consequently more efficient.  But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Suppose say, you have the data in newfile.txt is something like below:

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
Albert:x:2:2:daemon:/sbin:/sbin/nologin
Chin:x:3:4:adm:/var/adm:/sbin/nologin
Neon:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  
The above data cannot be filtered by AWK as the columns are seperated by (:). Here we use SED to present the data to AWK in its readable limits.
Just to see try the above mentioned command on newfile.txt.

You have below output:

#cat myfile.txt | awk '{print $1" ==> "$7}'

root:x:0:0:root:/root:/bin/bash ==>
bin:x:1:1:bin:/bin:/sbin/nologin ==>
Albert:x:2:2:daemon:/sbin:/sbin/nologin ==>
Chin:x:3:4:adm:/var/adm:/sbin/nologin ==>
Neon:x:4:7:lp:/var/spool/lpd:/sbin/nologin ==>
sync:x:5:0:sync:/sbin:/bin/sync ==>
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ==>
halt:x:7:0:halt:/sbin:/sbin/halt ==>
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin ==>

But if you use SED in the middle, like shown below, we have a meaningful ouput like below:

# cat myfile.txt | sed 's/:/ /g' | awk '{print $1" ==> "$7}'

root ==> /bin/bash
bin ==> /sbin/nologin
Albert ==> /sbin/nologin
Chin ==> /sbin/nologin
Neon ==> /sbin/nologin
sync ==> /bin/sync
shutdown ==> /sbin/shutdown
halt ==> /sbin/halt
mail ==> /sbin/nologin

So SED allowed us to pass the output without delimiter to AWK which made AWK's life easy and thus ours :)

  • You can implement SED on all delimiters and modify the data to required output.
  • Using tail and head commands with AWK and SED makes your command or script more powerful.
  • I hope this article gave a boost that you are looking to start as a first step towards scripting.
  • Feel free to comment on my blog, ask questions and share to the people in need.

Last but not least, referring Man Pages for AWK and SED may help you with more options.

HAPPY LINUX LEARNING :)

You may be interested in my other posts below:
-----------------------------------------------------------
Please review or follow and share your comments if it helps.
File System State is clean with errors in Linux:
http://linuxunixdatabase.blogspot.com/2017/02/file-system-state-is-clean-with-errors.html

How to use IPERF to test interface/network throughput in Linux:
http://linuxunixdatabase.blogspot.com/2017/02/how-to-use-iperf-to-test.html

Linux/Unix Network Troubleshooting:
http://linuxunixdatabase.blogspot.com/2017/02/linuxunix-network-troubleshooting.html

Removing existing LVM from your Linux System
http://linuxunixdatabase.blogspot.com/2017/02/removing-existing-lvm-from-your-linux.html