Difference between revisions of "Centos Bash Commands"

From rbachwiki
Jump to navigation Jump to search
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Redirection (send output to file)==
===Output descriptors===
#(0) Standard Input (stdin)
#(1) Standard Output (stdout)
#(2) Standard Error (stderr)
ls /etc/hosts 1>outputfilename
# the 1 is the standard output deliminator
ls /etc/hosts 2>badoutput
# will output only error because of the 2
===Redirecting Standard and Error Output to separate files===
ls /etc/hosts >goodoutput 2>badoutput
'''To redirect both Standard Output and Standard Error to the same file without any loss of data, you must use special notation. To specify that Standard Output be sent to the file goodoutput and Standard Error be sent to the same place as Standard Output, you can do the following'''
ls /etc/host > goodoutput 2>&1
'''You can also redirect a file to the Standard Input of a command using the < metacharacter.'''
tr r R </etc/filename
sort <newfile >finename
===Pipes ===
ls -h |grep 405
# will filter files with "405" in the name
[[File:Filtercommands.jpg|thumb|Filter Command]]
You can also combine redirection and piping, as long as input redirection occurs at the beginning of the pipe and output redirection occurs at the end of the pipe. An example of this is shown in the following output, which replaces all lowercase a characters with upper- case A characters in the prologue file used in the previous example, then sorts the file, num- bers each line, and saves the output to a file called newprologue instead of sending the output to the terminal screen.
tr a A <prologue |sort | nl > newprologue
===SED command===
'''syntax s/search/replace/. For example, the following output demonstrates how sed can be used to search for the string “the” and replace it with the string “THE” in the prologue file used earlier:'''
cat filename | sed s/the/THE/
====To have sed globally replace all occurrences of the string ====
cat filename | sed s/the/THE/g
'''You can also force sed to perform a search-and-replace on certain lines only. To replace the
string “the” with “THE” globally on lines 5 to 8 only, you can use the following command:'''
cat filename |sed 5,8s/the/THE/g
====You can also use sed to remove unwanted lines of text. To delete all the lines that contain the word “the,” you can use the following command:====
cat filename | sed /the /d
====Delete Lines that contain a pattern====
sed '/wordtofind/d' filename.txt
== Replace with special characters ==
Sample output
./dem/dem-116.jpg
find . -iname "*.jpg" | sed -e 's/\.\///g'
'' the . and the / are escaped \.\/''
===Using AWK===
'''Like sed, the awk command searches for patterns of text and performs some action on the text found. However, the awk command treats each line of text as a record in a database, and each word in a line as a database field. For example, the line “Hello, how are you?” has four fields: “Hello,” “how,” “are,” and “you?”. These fields can be referenced in the awk command using $1, $2, $3, and $4. For example, to display only the first and fourth words only on lines of the prologue file that contains the word “the,” you can use the following command:'''
cat prologue | awk ’/the/ {print $1, $4}’
'''By default, the awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems, however, are delimited using colon ( : ) char- acters. To change the delimiter that awk uses, you can specify the –F option to the com- mand. For example, the following example lists the last 10 lines of the colon-delimited file / etc/passwd and views only the 6th and 7th fields for lines that contain the word “bob” in the last 10 lines of the file:'''
tail /etc/passwd | awk -F : '/bob/{print $6, $7}'
===Environment Variables ===
'''List environment variables'''
set | less
'''Check Bash Shells installed'''
'''Check Bash Shells installed'''
  cat /etc/shells
  cat /etc/shells
Line 8: Line 63:
''' Check type of System'''
''' Check type of System'''
  uname -a
  uname -a
'''File Commands'''
=[[Centos OS]]=
locate filename
[[Category:Bash]]
The locate command looks in a premade database that contains a list of all the files on the system. This database is indexed much like a textbook for fast searching, yet can become outdated as files are added and removed from the system, which happens on a regular basis. As a result, the database used for the locate command (/var/lib/mlocate/mlocate.db) is updated each day automatically and can be updated manually by running the updatedb command at a command prompt. You can configure the directories that are searched by the updatedb command by editing the /etc/updatedb.conf file.

Latest revision as of 19:13, 29 May 2022

Redirection (send output to file)

Output descriptors

  1. (0) Standard Input (stdin)
  2. (1) Standard Output (stdout)
  3. (2) Standard Error (stderr)
ls /etc/hosts 1>outputfilename
# the 1 is the standard output deliminator
ls /etc/hosts 2>badoutput
# will output only error because of the 2

Redirecting Standard and Error Output to separate files

ls /etc/hosts >goodoutput 2>badoutput

To redirect both Standard Output and Standard Error to the same file without any loss of data, you must use special notation. To specify that Standard Output be sent to the file goodoutput and Standard Error be sent to the same place as Standard Output, you can do the following

ls /etc/host > goodoutput 2>&1

You can also redirect a file to the Standard Input of a command using the < metacharacter.

tr r R </etc/filename
sort <newfile >finename

Pipes

ls -h |grep 405
# will filter files with "405" in the name
Filter Command

You can also combine redirection and piping, as long as input redirection occurs at the beginning of the pipe and output redirection occurs at the end of the pipe. An example of this is shown in the following output, which replaces all lowercase a characters with upper- case A characters in the prologue file used in the previous example, then sorts the file, num- bers each line, and saves the output to a file called newprologue instead of sending the output to the terminal screen.

tr a A <prologue |sort | nl > newprologue

SED command

syntax s/search/replace/. For example, the following output demonstrates how sed can be used to search for the string “the” and replace it with the string “THE” in the prologue file used earlier:

cat filename | sed s/the/THE/

To have sed globally replace all occurrences of the string

cat filename | sed s/the/THE/g

You can also force sed to perform a search-and-replace on certain lines only. To replace the string “the” with “THE” globally on lines 5 to 8 only, you can use the following command: cat filename |sed 5,8s/the/THE/g

You can also use sed to remove unwanted lines of text. To delete all the lines that contain the word “the,” you can use the following command:

cat filename | sed /the /d

Delete Lines that contain a pattern

sed '/wordtofind/d' filename.txt

Replace with special characters

Sample output
./dem/dem-116.jpg
find . -iname "*.jpg" | sed -e 's/\.\///g'

the . and the / are escaped \.\/

Using AWK

Like sed, the awk command searches for patterns of text and performs some action on the text found. However, the awk command treats each line of text as a record in a database, and each word in a line as a database field. For example, the line “Hello, how are you?” has four fields: “Hello,” “how,” “are,” and “you?”. These fields can be referenced in the awk command using $1, $2, $3, and $4. For example, to display only the first and fourth words only on lines of the prologue file that contains the word “the,” you can use the following command:
cat prologue | awk ’/the/ {print $1, $4}’

By default, the awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems, however, are delimited using colon ( : ) char- acters. To change the delimiter that awk uses, you can specify the –F option to the com- mand. For example, the following example lists the last 10 lines of the colon-delimited file / etc/passwd and views only the 6th and 7th fields for lines that contain the word “bob” in the last 10 lines of the file:

tail /etc/passwd | awk -F : '/bob/{print $6, $7}'

Environment Variables

List environment variables

set | less


Check Bash Shells installed

cat /etc/shells

Check who is logged on

id
whoami
w
who

Check type of System

uname -a

Centos OS