-->

How to Use chattr Command in Linux

Author : Piyush Gupta

Chattr – command is useful to change file attributes on Linux second extended file system. It provides more security on your files from unwanted changes and deletes. There are many attributes available to do it.
To Add attributes on a file we use plus (+) and to remove attributes use minus(-) sign . Some impotent attributes are [ASacDdIijsTtu].

How to Use Chattr Command

Add Attribute on File

Create a new file to test this. As example I have crate a new file piyush.txt using touch command and assigned 777 permission on file.
# touch piyush.txt
# chmod 777 piyush.txt
# ls -l piyush.txt
-rwxrwxrwx 1 root root 0 Apr 19 15:10 piyush.txt
Now enable i attribute on file
# chattr +i piyush.txt
As you have enabled i attribute on file, let’s try to remove this file, you we get following error, even file has 777 permissions.
# rm -f piyush.txt
rm: cannot remove `piyush.txt': Operation not permitted

List Attributes of File

To List attributes on a file use lsattr command specified with file name.
# lsattr piyush.txt
----i-------- piyush.txt

Remove Attributes of File

To clear attribute on a file just use (-) sign with attached attributes. for example in piyush.txt.
# chattr -i piyush.txt

# lsattr piyush.txt
------------- piyush.txt

Attributes List in Chattr Command

Attribute [ i ]: A file with i attribute cannot be modified, it cannot be deleted or renamed by any user included root. Only root can clear this attribute. By this attribute you can keep safe your impotent files from other users or accidental delete.

How to read file Line by Line using Linux Shell Script

Author : Piyush Gupta

You can use while..do to read file line by line on a Linux or Unix-like system.

Syntax: Read file line by line on a Bash Unix & Linux shell:

  1. The syntax is as follows for bash, ksh, zsh, and all other shells to read a file line by line
  2. while read -r line; do COMMAND; done < input.file
  3. The -r option passed to read command prevents backslash escapes from being interpreted.
  4. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed -
  5. while IFS= read -r line; do COMMAND_on $line; done < input.file

Example:-

How to Read a File Line By Line in Bash

Here is more human readable syntax for you:
#!/bin/bash
input="/path/to/txt/file"
while IFS= read -r line
do
  echo "$line"
done < "$input"
The input file ($input) is the name of the file you need use by the read command. The read command reads the file line by line, assigning each line to the $line bash shell variable. Once all lines are read from the file the bash while loop will stop. The internal field separator (IFS) is set to the empty string to preserve white-space issues. This is a fail-safe feature.

How to read a file Line By Line using Shell Script

Some times we required to read file content line by line inside shell a script. For this example this script will read /etc/passwd file line by line and print usernames with there corresponding home directory.
#!/bin/bash

while read line
do
    USERNAME=`echo $line | cut -d":" -f1`
    HOMEDIR=`echo $line | cut -d":" -f6`
    echo "$USERNAME =>  $HOMEDIR"
done < /etc/passwd

How to Find File Creation Time in Linux

Author : Piyush Gupta

File creation time is stored in inode in EXT4 file system. An earlier version of EXT files systems doesn’t support file creation time.

There is a crtime (create time) timestamp in the debugfs stat output. finally EXT4 supports create time just like btime in NTFS windows.

Follow below instructions to how to find file creation time. Select an existing file or create a new file for testing. For this example, I am using an existing file.

Step 1 – Find Inode Number of File

First of all, find the inode number of any file using the following command on terminal.
$ ls -i /var/log/secure

13377 /var/log/syslog

Step 2 – Find File Creation Time (crtime)

After getting the inode number of file, Use debugfs command with inode number stats following by disk path.
$ debugfs -R 'stat <inode_number>' /dev/sda1
Implementation:
$ debugfs -R 'stat <13377>' /dev/sda1

debugfs 1.41.12 (17-May-2010)
Inode: 13377   Type: regular    Mode:  0600   Flags: 0x80000
Generation: 2326794244    Version: 0x00000000:00000001
User:     0   Group:     0   Size: 223317
File ACL: 0    Directory ACL: 0
Links: 1   Blockcount: 440
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x5230b7ae:55efa068 -- Thu Sep 12 00:04:22 2013
 atime: 0x5230b7ae:55efa068 -- Thu Sep 12 00:04:22 2013
 mtime: 0x5230b7ae:55efa068 -- Thu Sep 12 00:04:22 2013
crtime: 0x4eeacc8a:0948eb58 -- Fri Dec 16 10:13:54 2011
Size of extra inode fields: 28
Extended attributes stored in inode body:
  selinux = "system_u:object_r:var_log_t:s000" (31)
EXTENTS:
(0-24): 35008-35032, (25-54): 164224-164253
Find the entry of crtime in above output. This is the actual file creation time.
References: Read more about ext4 file system

Find Public IP using Linux Command

Author : Piyush Gupta

Public IP is used for communication between computers over the Internet. A computer running with public IP is accessible all over the world using the Internet. So we can say that it is the identity of the computer on the internet. Now the question is how do we know our public IP?. For computers having GUI can easily get there IP using web tools but how to get public IP of the computers having terminal access only. The solution is here – use one of the following commands to find public IP of your system using Linux terminal. These are also useful to use in a shell script.

Find Public IP using Linux Command

Command 1 –

Use dig command to find your public IP address. The dig command is a DNS lookup utility for Linux systems to look up your public IP address by connecting to the OpenDNS servers.
dig +short myip.opendns.com @resolver1.opendns.com

Command 2 –

Use wget command to get your Public IP address as below example.
wget http://ipecho.net/plain -O - -q ; echo

Command 3,4,5 –

Use curl command to get your Public address.
curl ipecho.net/plain; echo
curl icanhazip.com
curl ifconfig.me

Get Public IP in Shell Script

We can simply use following commands in our shell script get our computers public IP and store them in a variable to use anywhere in a shell script.
#!/bin/bash

PUBLIC_IP=`wget http://ipecho.net/plain -O - -q ; echo`
echo $PUBLIC_IP

How to Remove Empty Lines from File in Linux

Author : Piyush Gupta

Some time we need to remove empty lines from a file. Its can be done manually if file have few lines but if file have thousands of line this is hard to be done manually. Use one of following method to remove empty lines from a file.

Method 1 – Using sed

Sed is an stream editor. We can easily remove all blank lines using sed command. Use one of following sed command to remove blank lines from file. For example main.txt is your original file from which you need to remove blank lines.
Below command will remove all blank line and save content in seconf file out.txt. It will not affect the original file.
# sed '/^$/d' main.txt > out.txt
Now if you want to make changes in original file using -i switch sed command.
# sed -i '/^$/d' main.txt
    -i ( edit files in place ) Used for make changes in same file.

Method 2 – Using perl

Instead of sed, you can also use perl (a programming languege) to remove blank lines. Use the below example command to remove blank lines from main.txt file.
# perl -i -n -e "print if /S/" main.txt

Method 3 – Using awk

Also you can use AWK command line tool to remove blank lines from a file. For example use below command.
# awk 'NF > 0' main.txt > out.txt