How to find a String in all files in Linux

Author Piyush Gupta


How to Find all Files Containing a String in Linux. This tutorial will help you to search all files containing specific text string recursively. This tutorial uses “find” command to search string in files. Alternatively, You can also use grep command to search text.

Command:

# find / -exec grep -l “string-to-search” {} ;

Uses:

Below is an example command which searches text ‘redhat’ in / filesystem. This command will search for all files containing string ‘redhat’ and list file names only like below.
# find / -exec grep -l "redhat" {} ; 

/lib64/security/pam_oddjob_mkhomedir.so
/lib64/libdevmapper.a.1.02
/lib64/libdevmapper-event.a.1.02
/lib64/libdevmapper.a
/lib64/libdevmapper-event.a
...
To Search in specific file extension files. Like search ‘redhat’ in all php files only, use following command.
# find / -name '*.php' -exec grep -l "redhat" {} ; 

/var/www/projects/index.php
/var/www/projects/new.php
...

How to use DPKG commands in Linux

Author Piyush Gupta

DPKG is the main package management program in Debian and Debian based System. It is used to install, build, remove, and manage packages. Aptitude is the primary front-end to dpkg.


Some the most commonly used dpkg commands along with their usages are listed in below table:

COMMAND DETAILS
DPKG COMMAND
Install a package
dpkg -i {file.deb}
Update package
dpkg -i {file.deb}
Remove an installed package
dpkg -r {package}
List all installed packages
dpkg -l
List files in an installed package
dpkg -L {package}
Show information about installed package
dpkg -p {package}
Show information about package file
dpkg -I {file.deb}
List files in a package file
dpkg -c {file.deb}

Example:

For installing packages on debian based system we use dpkg (Debian Package Management System) command.

sudo dpkg -i package.deb

Installing Package:

To install a Google chrome from a deabian package file (google-chrome-stable_current_i386.deb) use -i command line switch.
$ sudo dpkg -i google-chrome-stable_current_i386.deb

Removing Package:

In case if we need to remove any package use -r command line switch with package name.
$ sudo dpkg -r google-chrome-stable_current_i386.deb

How to use RPM Commands in Linux

Author Piyush Gupta

RPM command is used for installing, uninstalling, upgrading, querying, listing, and checking RPM packages on your Linux system. RPM stands for Red Hat Package Manager.
With root privilege, you can use the rpm command with appropriate options to manage the RPM software packages.

In this article, let us review command details of rpm command.

COMMAND DETAILS
RPM COMMAND
Install a package
rpm -i {package.rpm}
Update package
rpm -U {file.rpm}
Remove an installed package
rpm -e {package}
List all installed packages
rpm -qa
List files in an installed package
rpm -ql {package}
Show information about installed package
rpm -qi
Show information about package file
rpm -qpi {file.rpm}
List files in a package file
rpm -qpl {file.rpm}
Verify all installed packages
rpm -Va
Verify installed package
rpm -V {package}

Example:

For installing packages on RedHat based systems, we use rpm (Red Hat Package Manager) command.

rpm -i package-1.2.3.rpm

Installing Package:

To install a rpm package using command line on redhat based system use -i command line switch with rpm command.
rpm -i package-1.2.3.rpm
You can also use YUM or DNF package manager to install downloaded rpm file. Its benefit to resolve dependencies required for the package.
yum localinstall package-1.2.3.rpm     ## CentOS, RHEL systems 
dnf localinstall package-1.2.3.rpm ## Fedora systems

Removing Package:

In case if we need to remove any package use -e command line switch with the package name.
rpm -e package-1.2.3.rpm

How to Exit Vim Text Editor

Author Piyush Gupta


VIM is an enhanced version of VI text editor. It is highly configurable text editor to edit files very efficiently. Many of Linux beginners faces issue with exit form vi/vim editor. This tutorial will help you to exit Vim text editor in Linux command line.

How to Exit Vim?

As we have already seen in advanced Vim tips, there are multiple ways to quit Vim. Let me list them one by one along with the procedure.
You have several ways to exit from Vim text editor. But you need to choose the right command to exit from Vim editor based on your decision. Remember that exit command always run from Vim command mode. So you need to press ESC to go to command mode. Now type a colon :to before writing exit command.
  • ESC + :q – Simple exit from a file, only if no changes made to file.
  • ESC + :wq – Write changes and quite from Vim editor.
  • ESC + :q! – Quit Vim editor without saving any changes (discard all changes made after edit file or last saved).

Using Keyboard Shortcuts

You can also use keyboard shortcuts to exit from vim editor. No need to type any thing. Press ESC to make sure you are in command mode.
  • Shift + z + z – save changes and exit
  • Shift + z + q – exit without saving changes

How to use cut Command in Linux

Author Piyush Gupta

Linux cut command is used for extracting file content on fields basis. text files do not have row/column like databases and some times we need the data of single column only. Cut identified columns on basis of separator (eg: colon ‘:’, semicolon ‘;’, comma ‘,’ etc).
For this example we are taking /etc/passwd file. All the rows are stored as below format with colon (:) separated like below. We use -f to specify field number and -d for delimiter (separator).

As per above screen-cast this file has 7 fields. Cut also support to fetch values on character basis suing -c command line switch. Lets take below examples, for which I am using /etc/passwd file.

1. Select Single Field from File –

For example we need the list of usernames from our /etc/passwd file. We know that first column stored username, Entire file is separated by colon (:).
# cut -d":" -f1  < /etc/passwd

root
bin
daemon
adm
lp
sync
shutdown
halt
We can also use pipeline ” | ” for passing the file content as input to cut command, like below –
# cat  /etc/passwd | cut -d":" -f1

2. Select Multiple Columns from File –

We can specify multiple field names with command separated, like below example will show the 1’st, 2’nd and 7’th fields only.
# cut -d":" -f1,2,7 < /etc/passwd

root:x:/bin/bash
bin:x:/sbin/nologin
daemon:x:/sbin/nologin
adm:x:/sbin/nologin
lp:x:/sbin/nologin
sync:x:/bin/sync
shutdown:x:/sbin/shutdown
halt:x:/sbin/halt
mail:x:/sbin/nologin
uucp:x:/sbin/nologin
We can also specify the range of columns with hyphen (-) on fields as well as both option’s together like below example commands.
    • Here first command will select 1’st, 2’nd,3’rd and 4’th fields.
    • Second command will select 3’rd, 4’th and 5’th fields.
    • Last command will show 2’nd, 3’rd, 4’th, 6’th and 10’th fields.
# cut -d":" -f1-4 < /etc/passwd
# cut -d":" -f3-5 < /etc/passwd
# cut -d":" -f2-4,6,10 < /etc/passwd
To get values of all columns except one use following command. For example if we need to select all columns but not 6.
# cut -d":" --complement -s -f6 < /etc/passwd

3. Selecting Single Character’s from File –

Except fields we can also select values from file on basis of single characters, while using characters we don’t need to specify separator.
# cut -c1 < /etc/passwd

r
b
d
a
l
s
s
Similarly fields we can also specify multiple comma separated characters or range of characters.
# cut -c1,2,3,6,7 < /etc/passwd
# cut -c1-3,6,7 < /etc/passwd

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 ' /dev/sda1
Implementation:
$ debugfs -R 'stat ' /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
Design a site like this with WordPress.com
Get started