Linux: How to Install and Enable Bash Completion

This post will guide you how to install and enable bash auto completion on CentOS/RHEL Linux. How do I add auto completion in Ubuntu/Debian Linux.

Install and Enable Bash Completion


If you want to use bash auto completion feature in Linux, you need to install bash-completion package in your Linux system.

For CentOS/RHEL Linux, you need to use yum command to install it, type:

# yum install bash-completion

Outputs:

[root@osetc_x8664 ~]# yum install bash-completion
Loaded plugins: rhnplugin, ulninfo
This system is receiving updates from RHN.
Resolving Dependencies
--> Running transaction check
---> Package bash-completion.noarch 1:2.1-6.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================================
Installing:
bash-completion noarch 1:2.1-6.el7 RH7_x86_64_latest 85 k

Transaction Summary
==========================================================================================================================================================
Install 1 Package

Total download size: 85 k
Installed size: 259 k
Is this ok [y/d/N]: y
Downloading packages:
bash-completion-2.1-6.el7.noarch.rpm | 85 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:bash-completion-2.1-6.el7.noarch 1/1
Verifying : 1:bash-completion-2.1-6.el7.noarch 1/1

Installed:
bash-completion.noarch 1:2.1-6.el7

Complete!

For Ubuntu/Debian Linux, you need to use apt command to install bash-completion package, type:

$ sudo apt-get install bash-completion

Then you need to logout your current bash shell and login again. the bash auto completion feature should be enabled. you can try one command, such as: press “yum i” then press TAB key. you should see that the command will be completed automatically.

# yum i[TAB]

Outputs:

[root@osetc_x8664 ~]# yum in
info install
[root@osetc_x8664 ~]# yum install

 

Sed: Replace String in a File

This post will guide you how to search and replace a string with a new text string in Linux. How do I use sed tool to find and replace string with another string.

Replace String using Sed Tool


If you want to find the text string called “osetc” in fio.txt and replace to “osetcen”, how do I use sed tool to achieve the reuslt. You can use the following sed command with -i option to replace old_test with new_text:

# sed -i 's/osetc/osetcen/g' fio.txt

Outputs:

root@devops-osetc:/home/devops/working# cat fio.txt
osetc.com
this is osetc

root@devops-osetc:/home/devops/working# sed -i 's/osetc/osetcen/g' fio.txt
root@devops-osetc:/home/devops/working# cat fio.txt
osetcen.com
this is osetcen

If you do not update the original file using sed command, you should remove -i option, like below:

# sed  's/osetc/osetcen/g' fio.txt

Outputs:

root@devops-osetc:/home/devops/working# sed 's/osetc/osetcen/g' fio.txt
osetcen.com
this is osetcen

root@devops-osetc:/home/devops/working# cat fio.txt
osetc.com
this is osetc

 

Linux: List Contents Of A Package

This post will guide you how to list the files of a given package in Linux. How do I list the contents of a package using yum command in Linux.

List Contents of A package


You can use rpm -qi command to list the contents of a installed packaged in your linux system. and you can also use the repoquery command to get the contents of a package. but you can not use yum command to achieve the reuslt directly. so using one of the following command:

# rpm -qli packageName

Or

# repoquery --list packageName

For example, you need to get the information and contents of a given package called wget, you can type the following command:

# rpm -qli wget

Outputs:

[root@osetc~]# rpm -qli wget
Name : wget
Version : 1.19.5
Release : 6.el8
Architecture: x86_64
Install Date: Fri 28 Dec 2018 10:10:37 PM EST
Group : Applications/Internet
Size : 2917226
License : GPLv3+
Signature : RSA/SHA256, Tue 16 Oct 2018 10:17:04 AM EDT, Key ID 199e2f91fd431d51
Source RPM : wget-1.19.5-6.el8.src.rpm
Build Date : Fri 12 Oct 2018 05:14:29 AM EDT
Build Host : x86-vm-05.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Vendor : Red Hat, Inc.
URL : http://www.gnu.org/software/wget/
Summary : A utility for retrieving files using the HTTP or FTP protocols
Description :
GNU Wget is a file retrieval utility which can use either the HTTP or
FTP protocols. Wget features include the ability to work in the
background while you are logged out, recursive retrieval of
directories, file name wildcard matching, remote file timestamp
storage and comparison, use of Rest with FTP servers and Range with
HTTP servers to retrieve files over slow or unstable connections,
support for Proxy servers, and configurability.
/etc/wgetrc
/usr/bin/wget
/usr/lib/.build-id
/usr/lib/.build-id/ae
/usr/lib/.build-id/ae/af87a0902650c25ed2555704ba3cd2ccb42b99
/usr/share/doc/wget
/usr/share/doc/wget/AUTHORS
/usr/share/doc/wget/COPYING
......

If you want to use repoquery command, you need to install yum-utils package firstly, type:

# yum install yum-utils

Outputs:

[root@osetc ~]# yum install yum-utils
Last metadata expiration check: 0:30:46 ago on Fri 28 Dec 2018 09:43:04 PM EST.
Dependencies resolved.
========================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================
Installing:
dnf-utils noarch 4.0.0-1.el8 baseiso 59 k

Transaction Summary
========================================================================================================================================
Install 1 Package

Total download size: 59 k
Installed size: 17 k
Is this ok [y/N]: y
Downloading Packages:
dnf-utils-4.0.0-1.el8.noarch.rpm 54 kB/s | 59 kB 00:01
----------------------------------------------------------------------------------------------------------------------------------------
Total 54 kB/s | 59 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installed: dnf-utils-4.0.0-1.el8.noarch
Installing : dnf-utils-4.0.0-1.el8.noarch 1/1
Installed: dnf-utils-4.0.0-1.el8.noarch
Running scriptlet: dnf-utils-4.0.0-1.el8.noarch 1/1
Verifying : dnf-utils-4.0.0-1.el8.noarch 1/1

Installed:
dnf-utils-4.0.0-1.el8.noarch

Complete!

Then you can type the following repoquery command to list the contents of wget package:

# repoquery --list wget

outputs:

[root@osetc ~]# repoquery --list wget
Last metadata expiration check: 0:31:32 ago on Fri 28 Dec 2018 09:43:04 PM EST.
/etc/wgetrc
/usr/bin/wget
/usr/lib/.build-id
/usr/lib/.build-id/ae
/usr/lib/.build-id/ae/af87a0902650c25ed2555704ba3cd2ccb42b99
/usr/share/doc/wget
/usr/share/doc/wget/AUTHORS
/usr/share/doc/wget/COPYING
/usr/share/doc/wget/MAILING-LIST
/usr/share/doc/wget/NEWS
/usr/share/doc/wget/README
/usr/share/doc/wget/sample.wgetrc
/usr/share/info/wget.info.gz
/usr/share/locale/be/LC_MESSAGES/wget.mo
/usr/share/locale/bg/LC_MESSAGES/wget.mo
/usr/share/locale/ca/LC_MESSAGES/wget.mo
/usr/share/locale/cs/LC_MESSAGES/wget.mo
/usr/share/locale/da/LC_MESSAGES/wget.mo

 

Linux Cat Command Usage And Examples

This post will guide you how to use Linux cat command to concatenate multiple files and print files on the standard output.

NAME


cat – concatenate files and print on the standard output

SYNOPSIS


cat [OPTION]… [FILE]…

DESCRIPTION


Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

-A, –show-all
equivalent to -vET

-b, –number-nonblank
number nonempty output lines, overrides -n

-e equivalent to -vE

-E, –show-ends
display $ at end of each line

-n, –number
number all output lines

-s, –squeeze-blank
suppress repeated empty output lines

-t equivalent to -vT

-T, –show-tabs
display TAB characters as ^I

-u (ignored)

-v, –show-nonprinting
use ^ and M- notation, except for LFD and TAB

–help display this help and exit

–version
output version information and exit

EXAMPLES


Displaying Contents of Files

You can use cat command to display the contents of a given file in Linux, just use the following command:

# cat /etc/passwd

Outputs:

root@devops-osetc:/home/devops/working# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
.......

If you want to put the contents of one file into another file or new file, you can use cat command with redirection operator to achieve it. Just type the following command:

# cat /etc/passwd > /tmp/fio.txt

This command will redirect all outputs from cat command to file fio.txt. then you can use cat command again to display the contents of fio.txt file.

Concatenate files

You can specify tow ore more file names in cat command, and it will display the contents of those files one after the other. so you can use the cat command to concatenate their contents to standard output. or you can also use redirection operator to redirect their contents to a another file. type:

# cat fio1.txt fio2.txt

or

# cat fio1.txt fio2.txt > fio.txt

Appending File

If you only want to append a source text file to another file, you can use cat command with the redirectio operator “>>”, type:

# cat fio1.txt >> fio.txt

this command will read the contents of fio1.txt, and then append them at the end of fio.txt file. Note: If fio.txt file does not exist, it will create new one.

 

Linux: How to Rename Directory

This post will guide you how to change or rename a file or directory under Linux operating system. How do I rename a directory via command line in Linux.

Rename Directory in Linux


You can use mv (move) command to remove for either a directory or a file. This command also can be used to move file to other directory in Linux. To rename a file or directory, you can refer the following syntax:

mv /path/oldDir /path/newDir

You need to add one or more space between old and new directory names.

For example, renaming a directory called fio to fio.new in your current directory, type the following command:

# mv ./fio ./fio.new

Outputs:

root@devops-osetc:/home/devops/working# ls -d fio
fio
root@devops-osetc:/home/devops/working# mv ./fio ./fio.new

root@devops-osetc:/home/devops/working# ls -d fio.new
fio.new

If you want to move one directory call fio.new to another directory /tmp, you can use the following command:

# mv ./fio.new /tmp

Outputs:

root@devops-osetc:/home/devops/working# mv ./fio.new/ /tmp
root@devops-osetc:/home/devops/working#
root@devops-osetc:/home/devops/working# ls /tmp
fio.new

If you want to show what is being done for mv command, you can use mv command with -v option, type:

root@devops-osetc:/home/devops/working# mv -v fio /tmp
'fio' -> '/tmp/fio'
root@devops-osetc:/home/devops/working# ls /tmp
fio

More Help for Mv Command


If you want to get more information or usage about mv command, you can use the following command:

# mv --help

or

# man mv

Outputs:

root@devops-osetc:/home/devops/working# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force do not prompt before overwriting
-i, --interactive prompt before overwrite
-n, --no-clobber do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
--strip-trailing-slashes remove any trailing slashes from each SOURCE
argument
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY
-T, --no-target-directory treat DEST as a normal file
-u, --update move only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
-Z, --context set SELinux security context of destination
file to default type
--help display this help and exit
--version output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:

none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mv>
or available locally via: info '(coreutils) mv invocation'

 

Ubuntu: Changing Root Password

This post will guide you how to change root password on Ubuntu Linux. How do I reset Root password on Ubuntu Linux 16.04/18.04.

Changing Root Password


The root user account is locked in Ubuntu Linux by default. so you do not use root user to login the Ubuntu Linux by default. and if you want to run program or command as a superUser, and you need to use su command. If you want to change root password, you need to use sudo command with passwd command to achieve it. Just use the following command:

$ sudo passwd root

Outputs:

devops@devops-osetc:~$ sudo passwd root
sudo: unable to resolve host devops-osetc
[sudo] password for devops:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

then you can use su command to change the current logging user as root. Type:

# su root

Outputs:

devops@devops-osetc:~$ su root
Password:
root@devops-osetc:/home/devops# whoami
root

If you want to disable your root account for security on Ubunt Linux, you can use the following command:

$ sudo passwd -dl root

This command will quickly disable a password for root account.

Linux: How to Find Process by Name

This post will guide you how to find a process by name on Linux. How do I find a process by name under Linux operating system.

Find Process by Name


You can use PS command to find running process in your Linux. and if you want to use ps command to find process by its name, you still need to combine with grep command. Like as follows:

# ps aux | grep -i sshd

Outputs:

devops@devops-osetc:~$ ps aux | grep -i sshd
root 1092 0.0 0.2 65512 2256 ? Ss Dec18 0:00 /usr/sbin/sshd -D
root 18170 0.0 0.6 94904 6796 ? Ss 10:50 0:00 sshd: devops [priv]
devops 18218 0.0 0.4 94904 4320 ? S 10:50 0:00 sshd: devops@pts/1
devops 18252 0.0 0.0 14224 936 pts/1 S+ 10:53 0:00 grep --color=auto -i sshd

Find Process using pidof Command


You can also use pidof command to achieve the same result of finding process by its name in Linux, just use the following command:

# pidof sshd

Outputs:

devops@devops-osetc:~$ pidof sshd
18218 18170 1092

From the above outputs, you should see that this command will find the process ID of a running program.

Find Process Using pgrep Command


You can also use pgrep command to look up or signal processes based on name and other attributes. just use the following command:

# pgrep sshd

Outputs:

devops@devops-osetc:~$ pgrep sshd
1092
18170
18218

Show all Running Process


If you want to show all running process in your Linux system, you need to use ps command with aux options. type:

# ps axu | less

Or

# ps aux | more

Outputs:

devops@devops-osetc:~$ ps aux | less
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 185248 4444 ? Ss Dec18 0:05 /sbin/init splash
root 2 0.0 0.0 0 0 ? S Dec18 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Dec18 0:06 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Dec18 0:00 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Dec18 1:31 [rcu_sched]
root 8 0.0 0.0 0 0 ? S Dec18 0:00 [rcu_bh]
root 9 0.0 0.0 0 0 ? S Dec18 0:00 [migration/0]
root 10 0.0 0.0 0 0 ? S< Dec18 0:00 [lru-add-drain]
root 11 0.0 0.0 0 0 ? S Dec18 0:00 [watchdog/0]
root 12 0.0 0.0 0 0 ? S Dec18 0:00 [cpuhp/0]
root 13 0.0 0.0 0 0 ? S Dec18 0:00 [kdevtmpfs]
root 14 0.0 0.0 0 0 ? S< Dec18 0:00 [netns]
root 15 0.0 0.0 0 0 ? S Dec18 0:00 [khungtaskd]
root 16 0.0 0.0 0 0 ? S Dec18 0:00 [oom_reaper]
root 17 0.0 0.0 0 0 ? S< Dec18 0:00 [writeback]
root 18 0.0 0.0 0 0 ? S Dec18 0:00 [kcompactd0]
root 19 0.0 0.0 0 0 ? SN Dec18 0:00 [ksmd]
root 20 0.0 0.0 0 0 ? SN Dec18 0:01 [khugepaged]
root 21 0.0 0.0 0 0 ? S< Dec18 0:00 [crypto]
.......

See Also:

Linux grep Command Usage and Examples

This post will guide you how to use grep command on Linux system. How do I serach the given file for lines containg a match to the given pattern.

NAME


grep, egrep, fgrep, rgrep – print lines matching a pattern

SYNOPSIS


grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN]... [-f FILE]... [FILE...]

DESCRIPTION


grep searches the named input FILEs for lines containing a match to the given PATTERN. If no files are specified, or if the file “-” is given, grep searches standard input. By default, grep prints the matching lines.

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

OPTIONS


Generic Program Information

–help Output a usage message and exit.

-V, –version
Output the version number of grep and exit.

Matcher Selection
-E, –extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below).

-F, –fixed-strings
Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

-G, –basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

-P, –perl-regexp
Interpret the pattern as a Perl-compatible regular expression (PCRE). This is highly experimental and grep -P may warn of unimplemented features.
Matching Control
-e PATTERN, –regexp=PATTERN
Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (–file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”.

-f FILE, –file=FILE
Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (–regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.

-i, –ignore-case
Ignore case distinctions in both the PATTERN and the input files.

-v, –invert-match
Invert the sense of matching, to select non-matching lines.

-w, –word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

-x, –line-regexp
Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $.

-y Obsolete synonym for -i.

General Output Control

-c, –count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option (see below), count non-matching lines.

–color[=WHEN], –colour[=WHEN]
Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for ields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority. WHEN is never, always, or auto.

-L, –files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.

-l, –files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

-m NUM, –max-count=NUM
Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or –count option is also used, grep does not output a count greater than NUM. When the -v or –invert-match option is also used, grep stops after outputting NUM non-matching lines.

-o, –only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-q, –quiet, –silent
Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or –no-messages option.

-s, –no-messages
Suppress error messages about nonexistent or unreadable files.

Output Line Prefix Control

-b, –byte-offset
Print the 0-based byte offset within the input file before each line of output. If -o (–only-matching) is specified, print the offset of the matching part itself.

-H, –with-filename
Print the file name for each match. This is the default when there is more than one file to search.

-h, –no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.

–label=LABEL
Display input actually coming from standard input as input coming from file LABEL. This is especially useful when implementing tools like zgrep, e.g., gzip -cd foo.gz | grep –label=foo -H something. See also the -H option.

-n, –line-number
Prefix each line of output with the 1-based line number within its input file.

-T, –initial-tab
Make sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal. This is useful with options that prefix their output to the actual content: -H,-n, and -b. In order to improve the probability that lines from a single file will all start at the same column, this also causes the line number and byte offset (if present) to be printed in a
minimum size field width.

-u, –unix-byte-offsets
Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a Unix-style text file, i.e., with CR characters stripped off. This will produce results identical to running grep on a Unix machine. This option has no effect unless -b option is also used; it has no effect on platforms other than MS-DOS and MS-Windows.

-Z, –null
Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be used with commands like find -print0, perl -0, sort -z, and xargs
-0 to process arbitrary file names, even those that contain newline characters.

Context Line Control

-A NUM, –after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.

-B NUM, –before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.

-C NUM, -NUM, –context=NUM
Print NUM lines of output context. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or –only-matching option, this has no effect and a warning is given.

File and Directory Selection

-a, –text
Process a binary file as if it were text; this is equivalent to the –binary-files=text option.

–binary-files=TYPE
If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is binary, and grep normally outputs either a one-line message saying that a binary file matches, or no message if there is no match. If TYPE is without-match, grep assumes that a binary file does not match; this is equivalent to the -I option. If TYPE is text, grep
processes a binary file as if it were text; this is equivalent to the -a option. When processing binary data, grep may treat non-text bytes as line terminators; for example, the pattern ‘.’ (period) might not match a null byte, as the null byte might be treated as a line terminator. Warning: grep –binary-files=text might output binary garbage, which can have nasty side effects if the output is a
terminal and if the terminal driver interprets some of it as commands.

-D ACTION, –devices=ACTION
If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is read, which means that devices are read just as if they were ordinary files. If ACTION is skip, devices are silently skipped.

-d ACTION, –directories=ACTION
If an input file is a directory, use ACTION to process it. By default, ACTION is read, i.e., read directories just as if they were ordinary files. If ACTION is skip, silently skip directories. If ACTION is recurse, read all files under each directory, recursively,following symbolic links only if they are on the command line. This is equivalent to the -r option.

–exclude=GLOB
Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and […] as wildcards, and \ to quote a wildcard or backslash character literally.

–exclude-from=FILE
Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under –exclude).

–exclude-dir=DIR
Exclude directories matching the pattern DIR from recursive searches.

-I Process a binary file as if it did not contain matching data; this is equivalent to the –binary-files=without-match option.

–include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under –exclude).

-r, –recursive
Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

-R, –dereference-recursive
Read all files under each directory, recursively. Follow all symbolic links, unlike -r.

SEE ALSO


Regular Manual Pages

awk(1), cmp(1), diff(1), find(1), gzip(1), perl(1), sed(1), sort(1), xargs(1), zgrep(1), read(2), pcre(3), pcresyntax(3), pcrepattern(3),
terminfo(5), glob(7), regex(7).

POSIX Programmer’s Manual Page
grep(1p).

Full Documentation

A complete manual ⟨http://www.gnu.org/software/grep/manual/⟩ is available. If the info and grep programs are properly installed at your site, the command

info grep

should give you access to the complete manual.

Linux Grep Command Examples


Search /etc/passwd file for root user

If you want to serach root user information in /etc/passwd file, you can use the grep command to achieve it. Type the following command:

# grep root /etc/passwd

Outputs:

devops@devops-osetc:~$ grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash

Recursively Serach in a given Directory

If you want to recursively search a string “fio” in a given directory /home/devops, you can use the following grep command:

# grep -r "fio" /home/devops

Outputs:

devops@devops-osetc:~$ grep -r "fio" /home/devops/
Binary file /home/devops/.bash_history matches
Binary file /home/devops/working/test.rar matches
/home/devops/fio.txt:fio test
/home/devops/fio.txt:fio test 2
/home/devops/.viminfo:'0 3 9 ~/fio.txt

 

Linux: How to Change Password Over SSH

This post will guide you how to change the root password on Linux server over ssh. How to use a bash shell script to change user password on remote Linux server.

Change Password on Linux Server


If you have logged on Linux server, and you want to change password for your current logging user, you can use passwd command to achieve the result. Type the following command:

# passwd

Outputs:

devops@devops-osetc:~$ passwd
Changing password for devops.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:

Change Password Over SSh on Remote Linux server


You can also change the root password over ssh on a remote linux server, you can use the ssh command to connect your linux server, then pass passwd command to it. type the following command to change password for root user using ssh:

# ssh root@192.168.1.21 passwd

You can also write an expect script to achieve the same result of changing password for a specific user using ssh:

#!/usr/bin/expect -f
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd

set username [lindex $argv 0]
set password [lindex $argv 1]
set serverid [lindex $argv 2]
set newpassword [lindex $argv 3]

spawn ssh $serverid passwd
expect "assword:"
send "$password\r"
expect "UNIX password:"
send "$password\r"
expect "password:"
send "$newpassword\r"
expect "password:"
send "$newpassword\r"
expect eof

You need to install expect package in your linux system before running this script.

Linux: How to List Only Hidden Files And Directories

This post will guide you how to recursively list all hidden files and directories on Linux operating system. How do I show only hidden files in Linux. How do I only list hidden directories in a given directory on Linux.

List All Hidden Files with ls command


You can use ls command to list all files in a given directory in Linux, and the hidden files are not listed by default using ls command. If you want to list all hidden files in a directory using ls command, you need to pass -a or -al option to ls command. type the following command:

# ls -a

or

# ls -la

Outputs:

devops@devops-osetc:~$ ls
Desktop Documents Downloads examples.desktop fio.cc fio.txt Music Pict
devops@devops-osetc:~$ ls -a
. .bashrc .dmrc fio.cc .ICEauthority .mysql_hist
.. .cache Documents fio.txt .lesshst Pictures
.bash_history .config Downloads .gconf .local .profile
.bash_logout Desktop examples.desktop .gnupg Music Public

devops@devops-osetc:~$ ls -la
total 144
drwxr-xr-x 16 devops devops 4096 Dec 19 21:23 .
drwxr-xr-x 3 root root 4096 Jul 24 2017 ..
-rw------- 1 devops devops 4884 Dec 20 06:18 .bash_history
-rw-r--r-- 1 devops devops 220 Jul 24 2017 .bash_logout
-rw-r--r-- 1 devops devops 3791 Aug 1 2017 .bashrc
drwx------ 12 devops devops 4096 Aug 1 2017 .cache
drwx------ 14 devops devops 4096 Jul 24 2017 .config
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Desktop
-rw-r--r-- 1 devops devops 25 Jul 24 2017 .dmrc
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Documents
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Downloads
-rw-r--r-- 1 devops devops 8980 Jul 24 2017 examples.desktop
-rw-rw-r-- 1 devops devops 99 Jul 31 2017 fio.cc
-rw-rw-r-- 1 devops devops 20 Aug 1 2017 fio.txt
drwx------ 2 devops devops 4096 Jul 24 2017 .gconf
drwx------ 3 devops devops 4096 Dec 18 21:27 .gnupg
-rw------- 1 devops devops 2112 Dec 18 21:27 .ICEauthority
-rw------- 1 devops devops 36 Aug 1 2017 .lesshst
drwx------ 3 devops devops 4096 Jul 24 2017 .local
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Music
-rw------- 1 devops devops 614 Aug 1 2017 .mysql_history
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Pictures
-rw-r--r-- 1 devops devops 655 Jul 24 2017 .profile
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Public
-rw------- 1 devops devops 43 Aug 3 2017 .rediscli_history
-rw-r--r-- 1 devops devops 0 Jul 24 2017 .sudo_as_admin_successful
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Templates
drwxr-xr-x 2 devops devops 4096 Jul 24 2017 Videos
-rw------- 1 devops devops 3956 Dec 19 21:23 .viminfo
-rw-rw-r-- 1 devops devops 11 Dec 19 01:37 .vimrc
drwxrwxr-x 3 devops devops 4096 Dec 20 02:46 working
-rw------- 1 devops devops 119 Dec 18 21:27 .Xauthority
-rw------- 1 devops devops 82 Dec 18 21:27 .xsession-errors
-rw------- 1 devops devops 82 Jul 31 2017 .xsession-errors.old

If you want to only list all hidden files in a directory using ls command, you can use one of the following commands:

# ls -d .[!.]?*

or

# ls -l -d .[!.]?*

Outputs:

devops@devops-osetc:~$ ls -d .[!.]?*
.bash_history .dmrc .local .viminfo
.bash_logout .gconf .mysql_history .vimrc
.bashrc .gnupg .profile .Xauthority
.cache .ICEauthority .rediscli_history .xsession-errors
.config .lesshst .sudo_as_admin_successful .xsession-errors.old

devops@devops-osetc:~$ ls -l -d .[!.]?*
-rw------- 1 devops devops 4884 Dec 20 06:18 .bash_history
-rw-r--r-- 1 devops devops 220 Jul 24 2017 .bash_logout
-rw-r--r-- 1 devops devops 3791 Aug 1 2017 .bashrc
drwx------ 12 devops devops 4096 Aug 1 2017 .cache
drwx------ 14 devops devops 4096 Jul 24 2017 .config
-rw-r--r-- 1 devops devops 25 Jul 24 2017 .dmrc
drwx------ 2 devops devops 4096 Jul 24 2017 .gconf
drwx------ 3 devops devops 4096 Dec 18 21:27 .gnupg
-rw------- 1 devops devops 2112 Dec 18 21:27 .ICEauthority
-rw------- 1 devops devops 36 Aug 1 2017 .lesshst
drwx------ 3 devops devops 4096 Jul 24 2017 .local
-rw------- 1 devops devops 614 Aug 1 2017 .mysql_history
-rw-r--r-- 1 devops devops 655 Jul 24 2017 .profile
-rw------- 1 devops devops 43 Aug 3 2017 .rediscli_history
-rw-r--r-- 1 devops devops 0 Jul 24 2017 .sudo_as_admin_successful
-rw------- 1 devops devops 3956 Dec 19 21:23 .viminfo
-rw-rw-r-- 1 devops devops 11 Dec 19 01:37 .vimrc
-rw------- 1 devops devops 119 Dec 18 21:27 .Xauthority
-rw------- 1 devops devops 82 Dec 18 21:27 .xsession-errors
-rw------- 1 devops devops 82 Jul 31 2017 .xsession-errors.old

Recursively List All Hidden Files and Directories


If you want to list all hidden files recursively in a given directory, you need to use find command to achieve the result.

For example, you need to show all hidden files in /home/devops directory and its sub-directories, you can use the following command:

# find /home/devops -name ".*" -print

Outputs:

devops@devops-osetc:~$ find /home/devops -name ".*" -print
/home/devops/.sudo_as_admin_successful
/home/devops/.gnupg
/home/devops/.profile
/home/devops/.bash_history
/home/devops/.bash_logout
/home/devops/.lesshst
/home/devops/.mysql_history
/home/devops/.cache
/home/devops/.gconf
/home/devops/.dmrc
/home/devops/.xsession-errors.old
/home/devops/.local
/home/devops/.local/share/.converted-launchers
/home/devops/.vimrc
/home/devops/.xsession-errors
/home/devops/.rediscli_history
/home/devops/.config
/home/devops/.viminfo
/home/devops/.bashrc
/home/devops/.ICEauthority
/home/devops/.Xauthority

If you only want to list all hidden normal files, type the following command:

# find /home/devops -type f -name ".*" -print

Outputs:

devops@devops-osetc:~$ find /home/devops -type f -name ".*" -print
/home/devops/.sudo_as_admin_successful
/home/devops/.profile
/home/devops/.bash_history
/home/devops/.bash_logout
/home/devops/.lesshst
/home/devops/.mysql_history
/home/devops/.dmrc
/home/devops/.xsession-errors.old
/home/devops/.local/share/.converted-launchers
/home/devops/.vimrc
/home/devops/.xsession-errors
/home/devops/.rediscli_history
/home/devops/.viminfo
/home/devops/.bashrc
/home/devops/.ICEauthority
/home/devops/.Xauthority

If you want to only show all hidden directories, type the following command:

# find /home/devops -type d -name ".*" -print

Outputs:

devops@devops-osetc:~$ find /home/devops -type d -name ".*" -print
/home/devops/.gnupg
/home/devops/.cache
/home/devops/.gconf
/home/devops/.local
/home/devops/.config