Thursday, 13 September 2018

Linux Commands to manage Local Accounts – useradd, usermod, chage & passwd

User administration is one of the important task of Linux system administrator. Local accounts or users in Linux like operating system is managed by useradd, usermod, userdel, chage and passwd commands.

◈ useradd command is used to create new accounts in Linux
◈ usermod command used to modify the existing accounts in linux
◈ userdel command is used to delete local account in linux
◈ passwd command used assign password to local accounts or users.
◈ chage comamnd is used to view & modify users password expiry information

Syntax of ‘useradd’ command


# useradd <options> <username_or_login>

Options used in useradd command :

Linux Command, LPI Guides, LPI Certification, LPI Learning, LPI Tutorial and Materials

Syntax of usermod command :


# usermod <options> <username_or_login>

Options used in usermod command.

Linux Command, LPI Guides, LPI Certification, LPI Learning, LPI Tutorial and Materials

Syntax of userdel command:


# userdel <options> <username_or_login>

Options used in userdel command :

Linux Command, LPI Guides, LPI Certification, LPI Learning, LPI Tutorial and Materials

Syntax of chage:


# chage <options> <username_or_login>

Options used in chage command :

Linux Command, LPI Guides, LPI Certification, LPI Learning, LPI Tutorial and Materials

Syntax of passwd Command:


# passwd <username_or_login>

In this article we will discuss different examples of user administration on CentOS 7 & RHEL 7.

Example:1 Create a local account & assign password.


User the below syntax to create and assign to the username.

# useradd <username> ; echo -e "<newpassword>\n<newpassword>" | passwd username

Let’s create a username ‘harry’ and assign password.

[root@lpicentral ~]# useradd harry ; echo -e "Roxicant@123#\nRoxicant@123#" | passwd harry
Changing password for user harry.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[root@lpicentral ~]#

Note : When a user is created in Linux followings are updated:

◈ A home directory is created under ‘/home/<username>’
◈ User info is updated in ‘/etc/passwd’ file
◈ Group Information is stored in ‘/etc/group’
◈ password info is updated in ‘/etc/shadow’ file.
◈ File for user’s email is created under ‘/var/spool/mail/<username>’

Example:2 Create a user with customize settings


Let’s create a user with following options :

UID = 2000
GID = 5000
Comments = ‘Admin Account of SAP’
Home Directory = /opt/sap
Shell = /bin/ksh
Username = john
password = xxxxxx

[root@lpicentral ~]# useradd -u 2000 -g 5000 -c "Admin Account of SAP" -d /opt/sap -s /bin/ksh john
[root@lpicentral ~]#
[root@lpicentral ~]# echo -e "Sapcant@123#\nSapcant@123#" | passwd john
Changing password for user john.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[root@lpicentral ~]#

Verify the above settings from /etc/passwd file.

[root@lpicentral ~]# grep john /etc/passwd
john:x:2000:5000:Admin Account of SAP:/opt/sap:/bin/ksh
[root@lpicentral ~]#

Example:3 Modify the Existing User


usermod command is used to modify the existing local accounts in Linux.

Let’s make the existing user “harry” part of Secondary group “sap” and change its home directory from ‘/home/harry’ to ‘/opt/sap’ and login shell from ‘/bin/bash’ to ‘/bin/sh’

[root@lpicentral ~]# usermod -G sap -d /opt/sap -s /bin/sh harry
[root@lpicentral ~]#
[root@lpicentral ~]# grep harry /etc/passwd
harry:x:1000:1000::/opt/sap:/bin/sh
[root@lpicentral ~]#

Example:4 Create a user and force to change the password at first login.


Let’s create a user ‘mark’ with secondary group ‘sap’, home directory as ‘/opt/sap’ and force him to change his password at the first login.

We can force users to change its password at first login by using command ‘chage -d 0 <username>‘.

[root@lpicentral ~]# useradd -c "sap user" -G sap -d /opt/data mark
[root@lpicentral ~]# echo -e "Sapdata@123#\nSapdata@123#" | passwd mark ; chage -d 0 mark
Changing password for user mark.
New password: Retype new password: passwd: all authentication tokens updated successfully.
[root@lpicentral ~]#

Now try to login as mark and see whether user is getting a prompt to change password or not.

Linux Command, LPI Guides, LPI Certification, LPI Learning, LPI Tutorial and Materials

Note: Use ‘chage -l <username>‘ command to view the user’s password expiry info.

Example:5 Delete a User along with its home directory


userdel command is used to delete local accounts or users in Linux. Let’s delete a user lpicentral along with its related its files (home directory).

[root@lpicentral ~]# userdel -r lpicentral
[root@lpicentral ~]# grep lpicentral/etc/passwd
[root@lpicentral ~]#

Tuesday, 11 September 2018

Linux/UNIX xargs command examples

xargs is a command in UNIX like System that reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.

xargs Command, Linux Command, Unix Command, LPI Study Materials

xargs command is very handy when combined with other commands. By default it expects input from STDIN.xargs is basically used to enhance the output of the initial command and utilize the output for performing numerous operations.

In this post we will discuss 10 practical examples of xargs command :

Example:1 Basic Usage of xargs


Type xargs , it will expect an input from us , start typing with enter for next line and then do a ctrl+d to see the output as below.

lpicentral@mail:~$ xargs
hello
john
this is me ( ctrl+d)
hello john this is me
lpicentral@mail:~$home/Downloads#

Example:2 Use of Delimiters


Here we specify a delimiter using the option -d , with \n as the delimiter. It echoes the string back to the screen when we press the ctrl+d

lpicentral@mail:~$ xargs -d\n
Hi
Welcome here
Now press Ctrl+D

lpicentral@mail:~$ xargs -d\n
Hi
Welcome hereHi
Welcome here
Now press Ctrl+D
lpicentral@mail:~$

Example:3 Limiting output per line


We can limit the output as per requirement using -n option in xargs command, for example to display only 2 items per line ,

lpicentral@mail:~$ echo a1 b2 c3 d4 e45
a1 b2 c3 d4 e5

lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -n 2
a1 b2
c3 d4
e5
lpicentral@mail:~$

Example:4 Enable User Prompt before execution


Using the option -p in xargs command , user will be prompted before the execution with y (means yes) and n (means no) options.

lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
n

lpicentral@mail:~$ echo a1 b2 c3 d4 e5 | xargs -p -n 2
/bin/echo a1 b2 ?…y
/bin/echo c3 d4 ?…a1 b2
y
/bin/echo e5 ?…c3 d4
y
e5
lpicentral@mail:~$

Example:5 Deleting files using find and xargs


lpicentral@mail:~$ find /tmp -name ‘abc.txt’ | xargs rm

Example:6 Using grep to query files


we can use the grep command with xargs to filter the particular search from the result of find command. An example is shown below :

lpicentral@mail:~$ find . -name “stamp” | xargs grep “country”
country_name
lpicentral@mail:~$

Example:7 Handle space in file names


xargs can also handle spaces in files by using print0 and xargs -0 arguments to find command.

lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 ls
/tmp/abcd asd.txt /tmp/asdasd asdasd.txt /tmp/cdef.txt

lpicentral@mail:~$ find /tmp -name “*.txt” -print0 | xargs -0 rm
lpicentral@mail:~$

Example:8 Use xargs with cut command


First create a cars.txt with below conetnts :

lpicentral@mail:~$ cat cars.txt
Hundai,Santro
Honda,Mobilio
Maruti,Ertiga
Skoda,Fabia

To display data in first column as shown below.

lpicentral@mail:~$ cut -d, -f1 cars.txt | sort | xargs
Honda Hundai Maruti Skoda
lpicentral@mail:~$

Example:9 Count the number of lines in each file.


lpicentral@mail:~$ ls -1 *.txt | xargs wc -l
4 cars.txt
13 trucks.txt
17 total
lpicentral@mail:~$

Example:10 Move files to a different location


lpicentral@mail:~$ pwd
/home/lpicentral
lpicentral@mail:~$ ls -l *.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh

lpicentral@mail:~$ sudo find . -name “*.sh” -print0 | xargs -0 -I {} mv {} backup/
lpicentral@mail:~$ ls -ltr backup/
total 0
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcd.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 abcde.sh
-rw-rw-r– 1 lpicentral lpicentral 0 Sep 15 22:53 fg.sh
lpicentral@mail:~$

Sunday, 9 September 2018

Linux/UNIX wget command with practical examples

wget command, Linux/UNIX, Linux Tutorial and Material, Linux Study Materials

wget is a Linux/UNIX command line file downloader.Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.Wget is non-interactive, meaning that it can work in the background, while the user is not logged on.

In this post we will discuss different examples of wget command.

Example:1 Download Single File


# wget http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso

This command will download the CentOS 7 ISO file in the user’s current working directtory.

Example:2 Resume Partial Downloaded File


There are some scenarios where we start downloading a large file but in the middle Internet got disconnected , so using the option ‘-c’ in wget command we can resume our download from where it got disconnected.

# wget -c http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso

wget command, Linux/UNIX, Linux Tutorial and Material, Linux Study Materials

Example:3 Download Files in the background


We can download the file in the background using the option ‘-b’ in wget command.

lpicentral@localhost:~$ wget -b http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/
CentOS-7.0-1406-x86_64-DVD.iso
Continuing in background, pid 4505.
Output will be written to ‘wget-log’.

As we can see above that downloading progress is capture in ‘wget-log’ file in user’s current directory.

lpicentral@localhost:~$ tail -f wget-log
2300K ………. ………. ………. ………. ………. 0% 48.1K 18h5m
2350K ………. ………. ………. ………. ………. 0% 53.7K 18h9m
2400K ………. ………. ………. ………. ………. 0% 52.1K 18h13m
2450K ………. ………. ………. ………. ………. 0% 58.3K 18h14m
2500K ………. ………. ………. ………. ………. 0% 63.6K 18h14m
2550K ………. ………. ………. ………. ………. 0% 63.4K 18h13m
2600K ………. ………. ………. ………. ………. 0% 72.8K 18h10m
2650K ………. ………. ………. ………. ………. 0% 59.8K 18h11m
2700K ………. ………. ………. ………. ………. 0% 52.8K 18h14m
2750K ………. ………. ………. ………. ………. 0% 58.4K 18h15m
2800K ………. ………. ………. ………. ………. 0% 58.2K 18h16m
2850K ………. ………. ………. ………. ………. 0% 52.2K 18h20m

Example:4 Limiting Download Speed .


By default wget command try to use full bandwidth , but there may be case that you are using shared internet , so if you try to download huge file using wget , this may slow down Internet of other users. This situation can be avoided if you limit the download speed using ‘–limit-rate‘ option.

#wget --limit-rate=100k http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso

In the above example,the download speed is limited to 100k.

Example:5 Download Multiple Files using ‘-i’ option


If you want to download multiple files using wget command , then first create a text file and add all URLs in the text file.

# cat download-list.txt
url1
url2
url3
url4

Now issue issue below Command :

# wget -i download-list.txt

Example:6 Increase Retry Attempts.


We can increase the retry attempts using ‘–tries‘ option in wget. By default wget command retries 20 times to make the download successful.

This option becomes very useful when you have internet connection problem and you are downloading a large file , then there is a chance of failures in the download.

# wget --tries=75 http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso

Example:7 Redirect wget Logs to a log File using -o


We can redirect the wget command logs to a log file using ‘-o‘ option.

#wget -o download.log http://mirror.nbrc.ac.in/centos/7.0.1406/isos/x86_64/CentOS-7.0-1406-x86_64-DVD.iso

Download.log file will be created in the user’s current directory.

Example:8 Download Full website for local viewing.


# wget --mirror -p --convert-links -P ./<Local-Folder> website-url

Whereas

◈ –mirror : turn on options suitable for mirroring.
◈ -p : download all files that are necessary to properly display a given HTML page.
◈ –convert-links : after the download, convert the links in document for local viewing.
◈ -P ./Local-Folder : save all the files and directories to the specified directory.


Example:9 Reject file types while downloading.


When you are planning to download full website , then we can force wget command not to download images using ‘–reject’ option .

# wget --reject=png Website-To-Be-Downloaded

Example:10 Setting Download Quota using wget -Q


We can force wget command to quit downloading when download size exceeds certain size using ‘-Q’ option

# wget -Q10m -i download-list.txt

Note that quota will never affect downloading a single file. So if you specify wget -Q10m ftp://wuarchive.wustl.edu/ls-lR.gz, all of the ls-lR.gz will be downloaded. The same goes even when several URLs are specified on the command-line. However, quota is respected when retrieving either recursively, or from an input file. Thus you may safely type ‘wget -Q10m -i download-list.txt’ download will be aborted when the quota is exceeded.

Example:11 Downloading file from password protected site.


# wget --ftp-user=<user-name> --ftp-password=<password> Download-URL

Another way to specify username and password is in the URL itself.

Either method reveals your password to anyone who bothers to run “ps”. To prevent the passwords from being seen, store them in .wgetrc or .netrc, and make sure to protect those files from other users with “chmod”. If the passwords are really important, do not leave them lying in those files either edit the files and delete them after Wget has started the download.

Thursday, 6 September 2018

The Best Linux Certifications for 2018

In the IT world, Linux certifications are the determining factor on how far your career goes seeing that more than half of the world’s servers run on Linux Operating Systems and virtually every IT expert is expected to have certain certificates to bank a job placement.

Linux Certifications, Linux Command, LPIC-1 Certifications, LPIC-2 Certifications

Today, we bring you the list of the most important Linux certifications arranged into 2 groups, Administration and Engineering.

Linux Administration Certifications


1. RHCSA (Red Hat Certified System Administrator)


The Red Hat Certified System Administrator Certificate is the basis for all the other Red Hat certificates because it handles the basics such the Linux command line, simple configuration, system administration skills and a lot more.

2. LFCS (Linux Foundation Certified System Administrator)


The Linux Foundation Certified System Administrator is a certification earned by taking a test that is based on administrative performance.

The Linux Foundation is responsible for the performance-based tests and is the only one offering such exams. At the time of writing, the Operating Systems used for this test are Ubuntu 16 or CentOS 7.

3. SUSE Certified Administrator


SUSE Certified Administrator (SCA) in Enterprise Linux trains its holders on advanced-level administration tasks in the open-source environment, specifically OpenSUSE.

Here, coursework isn’t required as all you will need to do is pass the organized exams. Nevertheless, it is important that you go through all the course objectives and put their task solutions to long-term memory.

4. CompTIA Linux+


CompTIA Linux+ gives you the know-how of administrative tasks which exponentially increases your chances of getting a job.

It is also a one-time test and doesn’t have to be renewed – allowing you to work with virtually any distro out there like a pro. If you’re pursuing a career in Linux (or server administration don’t skip it.

5. GIAC Certified UNIX System Security Administrator


The number one reason you should obtain a GIAC Certified UNIX System Security Administrator Certificate is to educate yourself on security and inspection of Linux OS and UNIX systems. With it, you would have learned how to install, configure and monitor Linux and UNIX systems.

6. LPIC-1: Linux Administrator


LPIC-1 for Linux Administrators vaildates holders’ ability to carry out maintenance tasks via the command line as well as configure basic networking achitectures on Linux distros.

You must pass 2 exams before becoming LPIC-1 certified after covering topics ranging from GNU and Unix Commands, System Architecture, Linux filesystems, to Networking Fundamentals and Essential System Services.

It has no prerequisites and is valid for a period of 5 years.

Linux Engineer Certifications


7. RHCE (Red Hat Certified Engineer)


RHCE (Red Hat Certified Engineer) is also a necessary certificate to have under your belt because its senior-level great for grabbing the attention of potential employees.

In order to obtain the RHCE (Red Hat Certified Engineer), you must first obtain the Red Hat Certified System Administrator (RHCSA) credential. After which follows an approximately 4-hour test.

With it, you can apply to work as a Linux administrator, IT analyst, Senior Systems Engineer, and Senior UNIX Administrator, among other job titles. It isn’t easy but certainly worth it.

8. LFCE (Linux Foundation Certified Engineer)


A Linux Foundation Certified Engineer (LFCE) is the Engineering certificate equivalent of LFCS. Engineers with this certificate are tasked with the duties of designing and implementing various system architectures.

Users with this certificate are a step closer to their educational goals and can serve as Subject Matter Experts (SMEs) for the next generation of system administration professionals.

9. SUSE Certified Engineer (SCE)


SUSE Certified Engineer certification covers higher-level day-to-day engineering level tasks particular to the SUSE Linux Enterprise Server Environment.

Students are to perform real-time exams on at least 2 live systems via a remote online examination environment which is hosted by ExamsLocal.

10. Oracle Linux OCA & OCP

The Oracle Linux OCA & OCP is a certification that is built on the self-named Operating System. It also is about gaining all the necessary skills and knowledge in order to operate Oracle technology and products.

For you to earn this particular certification, you must have to pass two examinations which will test you on your knowledge of GNU/Linux OSes and of Oracle.

Bear in mind that there are 6 levels, namely:

◈ Oracle Certified Junior Associate (OJA)
◈ Oracle Certified Associate (OCA)
◈ Oracle Certified Professional (OCP)
◈ Oracle Certified Master (OCM)
◈ Oracle Certified Expert (OCE) and
◈ Oracle Certified Specialist (OCS)

So make sure to take enough time to study and gain enough knowledge to past test after test.

11. LPIC-2: Linux Engineer


The LPIC-2 is an engineer certification meant specifically or Linux Engineers. In order to obtain this certificate, you must first and foremost pass the LPIC-2 201 and the LPIC-2 202. Similar to the LPIC-1, this certificate is distro independent.

There are other certificates worth mentioning such as the Certified Novell Linux Engineer, the Certified Novell Identity Manager Administrator, and the IBM Admin Certifications.

Tuesday, 4 September 2018

14 useful ‘ls’ command examples for linux beginners

ls Command, LPI Study Materials, LPI Guides, LPI Tutorial and Materials

The ls (list) command displays a list of files and directories. It supports several options some of them are listed in the below table along with a short description of each. In this post we will discuss 14 different examples of ls command which can be useful for beginners.

Syntax of ls command :

# ls [options] [file | directory]

Option Description 
-a Lists hidden files also. If a file or directory name starts with a dot, it is referred to as hidden. 
-F  Displays file types. Shows / for directories, * for executable files, @ for symbolic links and nothing for text files. 
-lh   Displays long listing with file sizes in human readable format. 
-l  Displays long listing with detailed file information including file type, permissions, link count, owner, group, size, date and time 
of  last modification, and name of the file.-ld Displays long listing of the specified directory, but hides its contents. 
-R  Lists contents of the specified directory and all its sub-directories (recursive listing). 
-lt  Lists all files sorted by date and time with the newest file first. 
-ltr  Lists all files sorted by date and time with the oldest file first. 

Note : For more detailed options of ls command please refer ls man page.

Example:1 list the files & directory of your current working directory


[root@localhost /]# ls
bin boot cgroup dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@localhost /]#

Example:2 To list files in the current directory with detailed information


[root@localhost /]# ls -l
total 94
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
dr-xr-xr-x.  8 root root  4096 May  3  2014 lib
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
drwx------.  2 root root 16384 May  3  2014 lost+found
drwxr-xr-x.  2 root root  4096 Sep 23  2011 media
drwxr-xr-x.  3 root root  4096 Jun 22 01:14 mnt
..................................................

Example:3 List the contents of a specific directory


[root@localhost /]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 0 Aug 3 14:31 bad-blocks.txt
drwx------. 2 root root 4096 May 3 2014 gpg-auCCFn
-rw-------. 1 root root 0 May 3 2014 yum.log
-rw------- 1 root root 1428 Nov 23 03:58 yum_save_tx-2014-11-23-03-58BHO8Jg.yumtx

To list only directory entries instead of contents, use ‘-ld‘ option. Example is shown below

[root@localhost /]# ls -ld /tmp/
drwxrwxrwt. 4 root root 4096 Dec 12 21:34 /tmp/

Example:4 To display all files in the current directory with their file types


[root@localhost /]# ls -F
bin/ cgroup/ etc/ lib/ linux-stuff media/ opt/ root/ selinux/ sys/ usr/
boot/ dev/ home/ lib64/ lost+found/ mnt/ proc/ sbin/ srv/ tmp/ var/
[root@localhost /]#

When we use ‘-F‘ option with ls command , then in the output ‘/’ will appended at every directory and for normal files nothing is appended.

Example:5 To list all files in the current directory with detailed information and sorted by date & time with the newest file first.


[root@localhost /]# ls -lt
total 94
-rw-r--r--   1 root root    72 Dec 12 21:40 linux-stuff
dr-xr-xr-x  13 root root     0 Dec 12 21:36 sys
drwxrwxrwt.  4 root root  4096 Dec 12 21:34 tmp
drwxr-xr-x  18 root root  3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root  4096 Dec 12 21:32 etc
dr-xr-xr-x  95 root root     0 Dec 12 21:31 proc
dr-xr-xr-x.  5 root root  1024 Nov 23 05:29 boot
drwxr-xr-x  10 root root  4096 Nov 23 03:42 cgroup
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 sbin
dr-xr-xr-x.  8 root root 12288 Nov 23 03:13 lib64
dr-xr-xr-x.  2 root root  4096 Nov 23 03:13 bin
dr-xr-x---.  5 root root  4096 Nov 23 02:53 root
drwxr-xr-x. 18 root root  4096 Aug 24 05:14 var
drwxr-xr-x   8 root root  4096 Jul 19 02:43 home
............................

Example:6 To display all files in the current directory with their sizes in human readable format( e.g 1K, 234M, 2G )


[root@localhost /]# ls -lh
total 94K
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1.0K Nov 23 05:29 boot
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3.6K Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
drwxr-xr-x. 2 root root 4.0K Jun 22 01:25 opt
......................................

Example:7 To list all files, including the hidden files, in the current directory with detailed information


[root@localhost /]# ls -la
total 102
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 .
dr-xr-xr-x. 23 root root 4096 Dec 12 21:49 ..
-rw-r--r-- 1 root root 0 Dec 12 21:32 .autofsck
-rw-r--r-- 1 root root 0 Jul 19 02:17 .autorelabel
dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
-rw-r--r-- 1 root root 0 Dec 12 21:48 .linux-rocks
-rw-r--r-- 1 root root 72 Dec 12 21:40 linux-stuff
-rw-r--r-- 1 root root 0 Dec 12 21:49 .linux-tips
drwx------. 2 root root 16384 May 3 2014 lost+found
drwxr-xr-x. 2 root root 4096 Sep 23 2011 media
..................................................................

Example:8 To list contents of the /etc directory recursively


[root@localhost /]# ls -R /etc/
/etc/:
adjtime
aliases
aliases.db
alternatives
anacrontab
audisp
audit
bash_completion.d
bashrc
blkid
centos-release
..........................

Example:9 List the files sorted by their size


[root@localhost /]# ls -lhS
total 13M
-rw-r--r-- 1 root root 12M Dec 12 22:05 linux-stuff
drwx------. 2 root root 16K May 3 2014 lost+found
dr-xr-xr-x. 8 root root 12K Nov 23 03:13 lib64
dr-xr-xr-x. 2 root root 4.0K Nov 23 03:13 bin
drwxr-xr-x 10 root root 4.0K Nov 23 03:42 cgroup
drwxr-xr-x. 64 root root 4.0K Dec 12 21:32 etc
drwxr-xr-x 8 root root 4.0K Jul 19 02:43 home
dr-xr-xr-x. 8 root root 4.0K May 3 2014 lib
drwxr-xr-x. 2 root root 4.0K Sep 23 2011 media
drwxr-xr-x. 3 root root 4.0K Jun 22 01:14 mnt
..............................................
Above command will display those files and directory first which are bigger in size.

Example:10 List the inode numbers of files & directories using ‘-i’ option


[root@localhost /]# ls -li 
total 12378
 786436 dr-xr-xr-x. 2 root root 4096 Nov 23 03:13 bin
 2 dr-xr-xr-x. 5 root root 1024 Nov 23 05:29 boot
 655361 drwxr-xr-x 10 root root 4096 Nov 23 03:42 cgroup
 3 drwxr-xr-x 18 root root 3680 Dec 12 21:32 dev
 393217 drwxr-xr-x. 64 root root 4096 Dec 12 21:32 etc
 2 drwxr-xr-x 8 root root 4096 Jul 19 02:43 home
 917506 dr-xr-xr-x. 8 root root 4096 May 3 2014 lib
 262146 dr-xr-xr-x. 8 root root 12288 Nov 23 03:13 lib64
 2668 -rw-r--r-- 1 root root 12582912 Dec 12 22:05 linux-stuff
...................................................................

Example:11 Display the current version of ls command using ‘–version’


[root@localhost /]# ls --version 
ls (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Example:12 List uid & gid of files or directories using ‘-n’ option


[root@localhost /]# ls -n 
total 14476
-rw-rw-r-- 1 1000 1000 193 Aug 16 16:10 1
-rw-rw-r-- 1 1000 1000 262 Nov 28 12:35 Anjali and Aakash help Yohan-B3MzR5cTpd2mLBSZfkPKppip8vCTKUdd.mp4.part
-rw-rw-r-- 1 1000 1000 1849048 Jun 7 2014 BINGO from Super Simple Songs-9mmF8zOlh_g.mp4.part
drwxr-xr-x 18 1000 1000 4096 Dec 13 08:59 Desktop
drwxr-xr-x 2 1000 1000 4096 Sep 27 13:09 Documents
drwxr-xr-x 2 1000 1000 4096 Dec 2 10:10 Downloads
drwx------ 3 1000 1000 4096 Nov 30 15:36 Dropbox
-rw-rw-r-- 1 1000 1000 94296 Nov 20 02:00 dropbox_2.10.0_amd64.deb
-rw-r--r-- 1 1000 1000 8980 Apr 20 2014 examples.desktop
-rw-rw-r-- 1 1000 1000 203 Oct 26 16:02 fs.sh
drwxrwxr-x 2 1000 1000 4096 Sep 1 14:31 Google Drive
................................................................

Example:13 Default Aliases of ls command


Type the alias command on the terminal to display the default aliases set for ls command.

[root@localhost /]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Example:14 Enable timestamps in the output of ls command using “–time-style” option


[root@localhost /]# ls -l --time-style="+%Y-%m-%d $newline%m-%d %H:%M"
total 12378
dr-xr-xr-x. 2 root root 4096 2014-11-23 11-23 03:13 bin
dr-xr-xr-x. 5 root root 1024 2014-11-23 11-23 05:29 boot
drwxr-xr-x 10 root root 4096 2014-11-23 11-23 03:42 cgroup
drwxr-xr-x 18 root root 3680 2014-12-12 12-12 21:32 dev
drwxr-xr-x. 64 root root 4096 2014-12-12 12-12 21:32 etc
drwxr-xr-x 8 root root 4096 2014-07-19 07-19 02:43 home
dr-xr-xr-x. 8 root root 4096 2014-05-03 05-03 05:24 lib
dr-xr-xr-x. 8 root root 12288 2014-11-23 11-23 03:13 lib64
-rw-r--r-- 1 root root 12582912 2014-12-12 12-12 22:05 linux-stuff
drwx------. 2 root root 16384 2014-05-03 05-03 05:19 lost+found
drwxr-xr-x. 2 root root 4096 2011-09-23 09-23 07:50 media
......................................................................

Saturday, 1 September 2018

17 useful tar command with practical examples in Linux

Tar (tape archive ) is the most widely used command in Unix like operating system for creating archive of multiple files and folders into a single archive file and that archive file can be further compressed using  gzip and bzip2 techniques. In other words we can say that tar command is used to take backup by archiving multiple files and directory into a single tar or archive file and later on files & directories can be extracted from the tar compressed file.

Tar Command, Linux, Linux Command, LPI Study Materials

In this article we will discuss 17 practical examples of tar command in Linux.

Syntax of tar command:

# tar <options> <files>

Some of the commonly used options in tar command are listed below :

Tar Command, Linux, Linux Command, LPI Study Materials

Note : hyphen ( – ) in the tar command options are optional.

Example: 1 Create a tar archive file


Let’s create a tar file of /etc directory and ‘/root/ anaconda-ks.cfg’ file.

[root@lpicentral ~]# tar -cvf myarchive.tar /etc /root/anaconda-ks.cfg

Above command will create a tar file with the name “myarchive” in the current folder. Tar file contains all the files and directories of /etc folder and anaconda-ks.cfg file.

In the tar command ‘-c‘ option specify to create a tar file, ‘-v’ is used for verbose output and ‘-f’ option is used to specify the archive file name.

[root@lpicentral ~]# ls -l myarchive.tar
-rw-r--r--. 1 root root 22947840 Sep 7 00:24 myarchive.tar
[root@lpicentral ~]#

Example:2 List the contents of tar archive file.


Using ‘–t‘ option in tar command we can view the contents of tar files without extracting it.

[root@lpicentral ~]# tar -tvf myarchive.tar

Listing a specific file or directory from tar file. In the below example i am trying to view whether ‘anaconda-ks.cfg’ file is there in the tar file or not.

[root@lpicentral ~]# tar -tvf myarchive.tar root/anaconda-ks.cfg
-rw------- root/root 953 2016-08-24 01:33 root/anaconda-ks.cfg
[root@lpicentral ~]#

Example:3 Append or add files to end of archive or tar file.


‘-r‘ option in the tar command is used to append or add file to existing tar file. Let’s add /etc/fstab file in ‘data.tar‘

[root@lpicentral ~]# tar -rvf data.tar /etc/fstab

Note: In the Compressed tar file we can’t append file or directory.

Example:4 Extracting files and directories from tar file.


‘-x‘ option is used to extract the files and directories from the tar file. Let’s extract the content of above created tar file.

[root@lpicentral ~]# tar -xvf myarchive.tar

This command will extract all the files and directories of myarchive tar file in the current working directory.

Example:5 Extracting tar file to a particular folder.


In case you want to extract tar file to a particular folder or directory then use ‘-C‘ option and after that specify the path of a folder.

[root@lpicentral ~]# tar -xvf myarchive.tar -C /tmp/

Example:6 Extracting particular file or directory from tar file.


Let’s assume you want to extract only anaconda-ks.cfg file from the tar file under /tmp folder.

Syntax :

# tar –xvf {tar-file } {file-to-be-extracted } -C {path-where-to-extract}

[root@lpicentral tmp]# tar -xvf /root/myarchive.tar root/anaconda-ks.cfg -C /tmp/
root/anaconda-ks.cfg
[root@lpicentral tmp]# ls -l /tmp/root/anaconda-ks.cfg
-rw-------. 1 root root 953 Aug 24 01:33 /tmp/root/anaconda-ks.cfg
[root@lpicentral tmp]#

Example:7 Creating and compressing tar file (tar.gz or .tgz )


Let’s assume that we want to create a tar file of /etc and /opt folder and also want to compress it using gzip tool. This can be achieved using ‘-z‘ option in tar command. Extensions of such tar files will be either tar.gz or .tgz

[root@lpicentral ~]# tar -zcpvf myarchive.tar.gz /etc/ /opt/

Or

[root@lpicentral ~]# tar -zcpvf myarchive.tgz /etc/ /opt/

Example:8 Creating and compressing tar file ( tar.bz2 or .tbz2 )


Let’s assume that we want to create compressed (bzip2) tar file of /etc and /opt folder. This can be achieved by using the option ( -j) in the tar command.Extensions of such tar files will be either tar.bz2 or .tbz

[root@lpicentral ~]# tar -jcpvf myarchive.tar.bz2 /etc/ /opt/

Or

[root@lpicentral ~]# tar -jcpvf myarchive.tbz2 /etc/ /opt/

Example:9 Excluding particular files or type while creating tar file.


Using “–exclude” option in tar command we can exclude the particular file or file type while creating tar file. Let’s assume we want to exclude the file type of html while creating the compressed tar file.

[root@lpicentral ~]# tar -zcpvf myarchive.tgz /etc/ /opt/ --exclude=*.html

Example:10 Listing the contents of tar.gz or .tgz file


Contents of tar file with the extensions tar.gz or .tgz is viewed by using the option ‘-t’. Example is shown below :

[root@lpicentral ~]# tar -tvf myarchive.tgz  | more 
............................................. 
drwxr-xr-x root/root         0 2016-09-07 08:41 etc/ 
-rw-r--r-- root/root       541 2016-08-24 01:23 etc/fstab 
-rw------- root/root         0 2016-08-24 01:23 etc/crypttab 
lrwxrwxrwx root/root         0 2016-08-24 01:23 etc/mtab -> /proc/self/mounts 
-rw-r--r-- root/root       149 2016-09-07 08:41 etc/resolv.conf 
drwxr-xr-x root/root         0 2016-09-06 03:55 etc/pki/ 
drwxr-xr-x root/root         0 2016-09-06 03:15 etc/pki/rpm-gpg/ 
-rw-r--r-- root/root      1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 
-rw-r--r-- root/root      1004 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7 
-rw-r--r-- root/root      1690 2015-12-09 04:59 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7 
-rw-r--r-- root/root      3140 2015-09-15 06:53 etc/pki/rpm-gpg/RPM-GPG-KEY-foreman 
..........................................................

Example:11 Listing the contents of tar.bz2 or .tbz2 file.


Contents of tar file with the extensions tar.bz2 or .tbz2 is viewed by using the option ‘-t’. Example is shown below :

[root@lpicentral ~]# tar -tvf myarchive.tbz2  | more 
........................................................ 
rwxr-xr-x root/root         0 2016-08-24 01:25 etc/pki/java/ 
lrwxrwxrwx root/root         0 2016-08-24 01:25 etc/pki/java/cacerts -> /etc/pki/ca-trust/extracted/java/cacerts 
drwxr-xr-x root/root         0 2016-09-06 02:54 etc/pki/nssdb/ 
-rw-r--r-- root/root     65536 2010-01-12 15:09 etc/pki/nssdb/cert8.db 
-rw-r--r-- root/root      9216 2016-09-06 02:54 etc/pki/nssdb/cert9.db 
-rw-r--r-- root/root     16384 2010-01-12 16:21 etc/pki/nssdb/key3.db 
-rw-r--r-- root/root     11264 2016-09-06 02:54 etc/pki/nssdb/key4.db 
-rw-r--r-- root/root       451 2015-10-21 09:42 etc/pki/nssdb/pkcs11.txt 
-rw-r--r-- root/root     16384 2010-01-12 15:45 etc/pki/nssdb/secmod.db 
drwxr-xr-x root/root         0 2016-08-24 01:26 etc/pki/CA/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/certs/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/crl/ 
drwxr-xr-x root/root         0 2015-06-29 08:48 etc/pki/CA/newcerts/ 
drwx------ root/root         0 2015-06-29 08:48 etc/pki/CA/private/ 
drwx------ root/root         0 2015-11-20 06:34 etc/pki/rsyslog/ 
drwxr-xr-x root/root         0 2016-09-06 03:44 etc/pki/pulp/ 
..............................................................

Example:12 Extracting or unzip tar.gz or .tgz files.


tar files with extension tar.gz or .tgz is extracted or unzipped with option ‘-x’ and ‘-z’. Example is shown below :

[root@lpicentral ~]# tar -zxpvf myarchive.tgz -C /tmp/

Above command will extract tar file under /tmp folder.

Note : Now a days tar command will take care compression file types automatically while extracting, it means it is optional for us to specify compression type in tar command. Example is shown below :

[root@lpicentral ~]# tar -xpvf myarchive.tgz -C /tmp/

Example:13 Extracting or unzip tar.bz2 or .tbz2 files


tar files with the extension tar.bz2 or .tbz2 is extract with option ‘-j’ and ‘-x’. Example is shown below:

[root@lpicentral ~]# tar -jxpvf myarchive.tbz2 -C /tmp/

Or

[root@lpicentral ~]# tar xpvf myarchive.tbz2 -C /tmp/

Example:14 Scheduling backup with tar command


There are some real time scenarios where we have to create the tar files of particular files and directories for backup purpose on daily basis. Let’s suppose we have to take backup of whole /opt folder on daily basis, this can be achieved by creating a cron job of tar command. Example is shown below :

[root@lpicentral ~]# tar -zcvf optbackup-$(date +%Y-%m-%d).tgz /opt/

Create a cron job for above command.

Example:15 Creating compressed archive or tar file with -T and -X option.


There are some real time scenarios where we want tar command to take input from a file and that file will consists of path of files & directory that are to be archived and compressed, there might some files that we would like to exclude in the archive which are mentioned in input file.

In the tar command input file is specified after ‘-T’ option and file which consists of exclude list is specified after ‘-X’ option.

Let’s suppose we want to archive and compress the directories like /etc , /opt and /home and want to exclude the file ‘/etc/sysconfig/kdump’ and ‘/etc/sysconfig/foreman‘, Create a text file ‘/root/tar-include’ and ‘/root/tar-exclude’ and put the following contents in respective file.

[root@lpicentral ~]# cat /root/tar-include
/etc
/opt
/home
[root@lpicentral ~]#
[root@lpicentral ~]# cat /root/tar-exclude
/etc/sysconfig/kdump
/etc/sysconfig/foreman
[root@lpicentral ~]#

Now run the below command to create and compress archive file.

[root@lpicentral ~]# tar zcpvf mybackup-$(date +%Y-%m-%d).tgz -T /root/tar-include -X /root/tar-exclude

Example:16 View the size of .tar , .tgz and .tbz2 file.


Use the below commands to view the size of tar and compressed tar files.

[root@lpicentral ~]# tar -czf - data.tar | wc -c
427
[root@lpicentral ~]# tar -czf - mybackup-2016-09-09.tgz | wc -c
37956009
[root@lpicentral ~]# tar -czf - myarchive.tbz2 | wc -c
30835317
[root@lpicentral ~]#

Example:17 Splitting big tar file into smaller files.


In Unix like operating big file is divided or split into smaller files using split command. Big tar file can also be divided into the smaller parts using split command.

Let’s assume we want to split ‘mybackup-2016-09-09.tgz‘ file into smaller parts of each 6 MB.

Syntax :  split -b <Size-in-MB> <tar-file-name>.<extension> “prefix-name”

[root@lpicentral ~]# split -b 6M mybackup-2016-09-09.tgz mybackup-parts

Above command will split the mybackup compressed tar file into the smaller files each of size 6 MB in current working directory and split file names will starts from mybackup-partsaa … mybackup-partsag. In case if you want to append numbers in place of alphabets then use ‘-d’ option in above split command.

[root@lpicentral ~]# ls -l mybackup-parts*
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaa
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsab
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsac
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsad
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsae
-rw-r--r--. 1 root root 6291456 Sep 10 03:05 mybackup-partsaf
-rw-r--r--. 1 root root 637219  Sep 10 03:05 mybackup-partsag
[root@lpicentral ~]#

Now we can move these files into another server over the network and then we can merge all the files into a single tar compressed file using below mentioned command.

[root@lpicentral ~]# cat mybackup-partsa* > mybackup-2016-09-09.tgz
[root@lpicentral ~]#