-->

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
Reference: http://vimdoc.sourceforge.net/

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