directory

  • awk
  • cp
  • crontab
  • date
  • df
  • dig
  • du
  • env
  • find
  • fdisk
  • free
  • fuser
  • groups
  • grep
  • gzip
  • head
  • less
  • locate
  • ls
  • mount
  • mkdir
  • netstat
  • rm
  • rpm
  • rpm2cpio
  • sed
  • SELinux
  • sort
  • systemctl
  • sha256sum
  • tar
  • tee
  • tree
  • time
  • test
  • tcpdump
  • unzip
  • wc
  • who
  • wget
  • watch
  • Systemd service service
  • xargs

awk#

Example:

Env variable values are as follows, and we need to get the link value of pkg_URL:

Copy{"name": "michael", "sex": "male", "pkg_url": "www.github.com", "number": "888"}Copy the code
Copypkg_url=$(echo $env | awk -F "pkg_url\": \"" '{print $2}' | awk -F "\"," '{print $1}')
echo $pkg_url 
www.github.comCopy the code

-f Specifies the separator rule. Because the separator rule contains double quotation marks, you need to use the escape symbol.

Copy# # format $awk action file example $awk '{print $0}' demo. TXT echo 'hello: Michael: xiang' | awk -f ':' '{print $1}'Copy the code
  • Awk print function
  • Printf +awk is a perfect combination
  • AWK concise tutorial
  • Awk primer

## basename

The basename command is used to print the basic name of a directory or file

Copy[root@HGH1000059721 test]# basename a.tar.tar # Suffix: Optional string that specifies the file suffix to remove. A [root@HGH1000059721 test]# basename/TMP /test/a.tarCopy the code

Reference:

  • basename

cp#

Copy dest/ SRC to dest directory.

Copycp -r src destCopy the code

Copy the contents from SRC to dest:

Copycp -r src/* destCopy the code

Copy file, overwrite without asking:

Copycp -nrf a.txt b.txtCopy the code

The cp command is alias cp -i by default. Therefore, you need to confirm the conflict during replication. You can use the following method to not change the alias and achieve default overwriting:

Copy/bin/cp xx yyCopy the code

Reference:

  • Linux -cp command
  • Linux commands Command description -cp command
  • Linux uses the cp command to force the override function

crontab#

Copy# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executedCopy the code

You can also use the following special characters in each of the above fields:

  • Asterisk (*) : indicates all possible values. For example, the asterisk (*) in the month field indicates that the command will be executed every month after all restrictions on other fields are met.
  • Comma (,) : comma-separated values specify a list range, for example, “1,2,5,7,8,9”
  • Middle bar (-) : A range of integers can be represented by a middle bar between integers, for example “2-6” for “2,3,4,5,6”

Slash (/) : You can use a slash to specify the interval frequency. For example, 0-23/2 indicates that the interval is executed every two hours. A forward slash can be used with an asterisk, for example, */10. If it is used in the minute field, the command is executed every 10 minutes.

Location of task execution file:

Copy/var/spool/cron/crontabsCopy the code

Command parameters:

  • -uUser: used to set the crontab service for a user.

    File: File is the name of the command file, indicating that file is loaded as the crontab task list file
  • -e: Edits the crontab file of a user. If no user is specified, the crontab file of the current user is edited.
  • -l: Displays the crontab file of a user. If no user is specified, the crontab file of the current user is displayed.
  • -r: Deletes the crontab file of a user from the /var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
  • -i: Prompts users to confirm deletion of crontab files

Run the/TMP /test.sh script every 2 minutes:

CopyCrontab -e # Run the crontab -e command to edit the cron file */2 * * * */ TMP /test.sh of the corresponding user in /var/spool/cronCopy the code

Restart SMB at 10:00am on Saturday and Sunday – every Saturday and Sunday:

Copy10 1 * * 6 0 /etc/init.d/ SMB restart >> / TMP /run.log 2>&1Copy the code

Example 4: Execute at the 3rd and 15th minutes from 8am to 11am every other day:

Copy3,15 8-11 */2  *  * myCommandCopy the code

Example 5: Clearing HTTPD service logs that are more than 3 days old:

Copy0 5 * * * /usr/bin/find /var/log/httpd/ -type f -mtime +3 -exec rm -rf {} \;Copy the code

Example 6: Clean up the contents of a specified folder with a re

Copy#update-20181122: clean dir +120 days
0 6 * * * find /data/michael -maxdepth 7 -type d -mtime +120 -regextype posix-egrep -regex '.*/[0-9]{2}/[0-9]{6}$' -exec rm -rf {} \;Copy the code

Start/stop/restart crontab

Copy$ /etc/init.d/crond start
$ /etc/init.d/crond stop
$ /etc/init.d/crond restartCopy the code

See the log

Copy$ tail -f /var/log/cronCopy the code

Reference:

  • How do I view crontab logs
  • Runoob -Linux Scheduled Crontab task
  • LinuxTools- Crontab Scheduled task recommendation
  • One Linux command per day (50) : crontab command

date#

-d < string > : displays the date and time indicated in the string. The string must be surrounded by double quotation marks; -s < string > : Sets the date and time according to the string. The string must be surrounded by double quotation marks; -u: displays GMT. –help: online help; –version: displays the version information.

Parameter <+ time and date format > : Specifies the date and time format to use for display. It’s formatted string manipulation. Use double quotation marks when Spaces are required, for example, “+%Y-%m-%d %H:% m :%S”.

In general,%Y %m % D %H %m %S is the most basic. %a %b is also used when using month of the week

The sample

CopyVERSION = $(date + H % Y % m % d % % m % S) # 20180410192702 # date behind the space time = $(date "+ % Y - m - H: % d % % % m: % S") with Spaces in # time format, Need quotes » date "+%Y-%m-%d %T %a %a "2018-06-04 11:31:25 Mon MondayCopy the code

Reference:

  • The use of the date command in Shell

df#

Using the df command, you can easily see that the disk is running out of storage space. View the mount status and disk usage:

Copydf -hTCopy the code

dig#

View domain name information. Generally, a domain name is bound to multiple IP addresses. The ping command can view only one IP address at a time. This command displays domain name resolution information

Copydig baidu.comCopy the code

This command may not exist on the machine, you can install it as follows:

Copyyum install -y bind-utilsCopy the code

Reference:

  • Xindoo-csdn – Some Linux commands I use frequently

du#

The du command displays disk usage for a particular directory (by default, the current directory). This method can determine whether there are oversized files in a directory on the system.

View the file size of the current folder:

CopyDu - du - sh/usr/sh * * | sort - rn # M size orderCopy the code

env#

View environment variable values. For example, view environment variable values with SVN:

Copyenv|grep SVNCopy the code

find#

Example: Periodically clear HTTPD service logs for more than 3 days:

Copy0 5 * * * /usr/bin/find /var/log/httpd/ -type f -mtime +3 -exec rm -rf {} \;Copy the code

Example: Clean up the contents of a specified folder with a re

Copy#update-20181122: clean dir +120 days
0 6 * * * find /data/michael -maxdepth 7 -type d -mtime +120 -regextype posix-egrep -regex '.*/[0-9]{2}/[0-9]{6}$' -exec rm -rf {} \;Copy the code

Example: Search for conf configuration files at the bottom of the /etc directory

CopyFind /etc/-maxdepth 1-name "*.conf" # Quotes are bestCopy the code

Example: search only the current directory, but not the. Git directory, and count the number of directories

Copyfind . -maxdepth 1 -mindepth 1 -type d | grep -v .git |wc -lCopy the code

Example: List only directories

Copyfind . -type d -maxdepth 1Copy the code

Reference:

  • Examples of common use of find in Linux
  • Using regular expressions in FIND – How to use the FIND command in Linux

fdisk#

Fdisk -l see all partitions in the system of information at https://blog.csdn.net/cc_net/article/details/2894510

free#

The free command displays the free and used physical memory, swap memory, and buffer used by the kernel in Linux. The free command is one of the most frequently used Linux system monitoring tools.

Copyfree -h -s 2 -tCopy the code
  • -hUnits will be more human
  • -sThe memory usage is displayed every 2 seconds
  • -tDisplay memory Sum
CopyTotal used free shared buff/cache available Mem: 7.6g 935M 6.1g 9.7m 631M 6.4g Swap: 7.5g 0B 7.5g total: 15G 935M 13GCopy the code
  • Total: indicates the total physical memory size.
  • Used: Indicates the used size.
  • Free: How much is available.
  • Shared: Indicates the amount of memory Shared by multiple processes.
  • Buffers/cached: Specifies the size of the disk cache
  • SWAP, also known as virtual memory

From the application’s point of view, buffers/cached is equal to available for the application because buffer/cached is used to improve file reading performance and is quickly reclaimed when the application needs to use memory.

So from an application perspective, available memory = system free memory+buffers+cached

For Linux, as long as you don’t use swap space, you don’t have to worry about running out of memory. If you use a lot of swap, you may want to consider adding physical memory. This is also the standard for Linux to see if it has enough memory.

+ Buffers /cache: If free is too small for your application, it is time to optimize the application or add more memory

Reference:

  • One Linux command per day (45) : free command

fuser#

Fuser is commonly used to diagnose system “resource busy” problems, usually when you want to umount the specified mount point. If you want to kill all processes that are using a specified file, file system, or sockets, you can use -k option

CopyFuser -k -i /path/to/your/filename # If you add -i to the command, you need to confirm before killing the fileCopy the code
  • The fuser command
  • Summary of fuser commands

groups#

CopyWhoami # view user name groups # view user groupCopy the code

grep#

Grammar:

Copygrep [options] pattern [file]Copy the code
CopyGrep -rni 'github.com' -c 2Copy the code
CopyGrep 'shopbase' /home/admin -r-n --include *.{vm, Java} # exclude *.{vm, Java} # the matchCopy the code

Reference:

  • man-grep
  • Linux find file contents (grep)
  • One Linux command per day (39) : grep command
  • My List of Java troubleshooting tools

gzip#

Gzip is an outgrowth of the GNU project. This software contains the following tools:

  • Gzip: Used to compress files
  • Gzcat: Used to view the contents of compressed text files
  • Gunzip: Used to decompress files.
Copygzip xxx
gzip -l <filename> # list compressed file contentsCopy the code

head#

Display the first n lines:

Copyhead -nCopy the code

https://www.linuxdaxue.com/linux-command-intro-head.html

less#

In the case of more, we have no way to turn to the front, only to look at the back, but in the case of less, we can use [PageUp] [pageDown] and other key functions to go back and forth to look at the file.

locate#

CopyLocate gpg-key # find /etc/name '* gpg-key *' EquivalentCopy the code

If the system does not have the locate command, you can run the yum install mlocate -y command to install it. After the installation, run the updatedb command.

ls#

Display only directories:

Copyll -dCopy the code

The ls command displays the size of the file. It determines the size of the file in M, Kb, or G

Copyll -hCopy the code

mount#

Mount Displays all mounting information.

Mount a partition to a directory:

Copymount /dev/xvde /dataCopy the code
  • Linux the mount command

mkdir#

The mkdir sysadmin/admim_ {1, 2, 3, 4, 5}

Reference:

  • Manage file and directory properties with the chattr and lsattr commands in Linux

netstat#

Checking Port Usage

Copynetstat -anp|grep 80 Copy the code
  • Linux (redhat and centos) Release the occupied port

rm#

Delete only hidden files and hidden folders under the current folder:

Copyrm -rf .*Copy the code

https://blog.csdn.net/ficksong/article/details/52447729

rpm#

I have those RPM packages installed on my system

Copyrpm -qa Copy the code

If you want to find all installed packages that contain a certain string OF SQL

Copyrpm -qa | grep sqlCopy the code

Where is a file from an RPM package installed?

CopyThe RPM - ql package nameCopy the code

Uninstalling software packages

Copyrpm -e Copy the code

Reference: http://man.linuxde.net/rpm

rpm2cpio#

RPM package decompress:

Copy# note that with cpio - div. Otherwise, the terminal will print out the content of the excess rpm2cpio XXXX. RPM | cpio - divCopy the code

The rpm2CPIO command may not be available on your Linux operating system.

Copysudo apt-get install  rpm2cpio
sudo yum install rpm2cpioCopy the code

sed#

Eg1: Intercepts the content between two lines in the log, removing the matching header and tail lines:

Copycat mock.log |sed -n '/tee/,/find/p' mock.log|sed -n '1! p'|sed -n '$! p'|awk '{print $2,$3}'Copy the code

eg2:

Copynl passwd|sed "1d; 10d" # delete line 1, line 10Copy the code

eg3:

CopySed -i -e "1i%define upstream_version $UPSTREAMVERSION\\ *.spec # sed -i -e "s/ $UPSTREAMVERSION/g" *. The spec # replacedCopy the code

Reference:

  • SED Tutorial
  • Sed and AWK — two tools that o&M must master
  • See the example to learn sed
  • 30 minutes to learn SED
  • The sed command filters the lines between the specified strings

SELinux#

CopySestatus [-v] # check selinux status getenforce # Check selinux statusCopy the code

Selinux enablement often affects other services, such as HTTPD, so operations usually turn it off by default when they get the machine.

CopySetenforce 0 # set SELinux to permissive without restarting SELinuxCopy the code

After the restart, the values modified by using Setenforce will become invalid. Therefore, if the restart is valid, you need to modify the following files:

CopyThe soft link to the /etc/sysconfig/selinux # file is /etc/sysconfig/selinuxCopy the code
  • wiki-SELinux
  • Why turn SELinux off on Linux?
  • How do I turn SELinux on or off
  • SELinux Enables or disables the parameters

sort#

Sort the password file /etc/passwd by user ID. The -k and -t arguments are useful when sorting data that is split by an Ann field.

Copysort -t ":" -k 3 -n /etc/passwdCopy the code

systemctl#

task The old order New instructions
Enable a service to start automatically Chkconfig — level 3 HTTPD on systemctl enable httpd.service
Disable the automatic startup of a service Chkconfig — level 3 HTTPD off systemctl disable httpd.service
Checking service Status service httpd status systemctl status httpd.service
Displays all started services The chkconfig — the list Systemctl list – units – type = service
Starting a Service service httpd start systemctl start httpd.service
Stop a service service httpd stop systemctl stop httpd.service
Restarting a Service service httpd restart systemctl restart httpd.service
  • Linux setup program automatically starts upon startup (systemctl command and chkconfig command usage difference comparison)

sha256sum#

Sha256 value corresponding to the generated file:

CopySha256sum fusionSphere_upgrade_6.2.50.4001.tar. gz > a.sha256sum # Check sha256sum -c <(grep Fusionsphere_upgrade_6.2.50.4001.tar. gz a.sha256Sum) # CheckCopy the code

Reference:

  • In Linux, the SHA256 verification file is generated and a file is verified using sha256

tar#

The most widely used archiving tool on Unix and Linux is the tar command.

CopyTar function [options] object1 object2......Copy the code

First, create an archive:

Copytar -cvf test.tar test/ test2/Copy the code

Create an archive named test.tar containing the contents of the test and test2 directories.

Next, list the contents of the tar file test.tar (without extracting the file) :

Copytar -tf test.tarCopy the code

Finally extract the file with the command:

Copytar -xvzf test.tarCopy the code

The tar command is an easy way to create an archive for an entire directory

Tip: After downloading open source software, you’ll often see file names ending in. TGZ. These gzip compressed tar files can be decompressed using tar -zxvf filename. TGZ

tee#

The tee command is used to redirect data to a file, on the other hand, you can provide a copy of the redirect data as stdin for subsequent commands. Simply redirects data to a given file and screen.

Eg1 prints stdout on the terminal while redirecting to the file:

Copyls | tee out.txt | cat -nCopy the code

Eg2 creates daemon.json file with contents between EOF as stdin:

Copytee /etc/docker/daemon.json << EOF
{
    "insecure-registries" : [ "", ""]
}
EOFCopy the code

< < EOF… EOF is used to customize input during command execution. It is similar to a temporary file, but more convenient and flexible than using a file.

EOF use:

What it does is pass the Content between the two delimiters (the Here Document Content part) to CMD as an input parameter.

Copycmd << delimiter
  Here Document Content
delimiterCopy the code
Copy[root@ecs-6b86 tmp]# cat << EOF >tt.sh
123123123
345345
asdfasds
EOFCopy the code

User-defined EOF, for example, Michael [root@slave-server opt]# cat << Michael > haha. TXT

ggggggg

4444444

6666666

michael

<< becomes <<-. The only change with <<- is that tabs are removed from the content section of the Here Document before each line. This is done so that the content section can be indented to make the code easier to read when writing the Here Document.

Sometimes variables in script content do not want to be replaced by system environment variables by adding “before and after the initial delimiter

Reference:

  • Linux shell here document usage (cat << EOF)
  • What is EOF?
  • Good use of Linux shell script EOF

tree#

Copytree -FCL 2 FusionUpgradeCopy the code
  • Linux tree command – displays the tree structure of a directory

time#

Copytime nslookup michael.com
nslookup: can't resolve '(null)': Name does not resolve

Name:      micahel.com
Address 1: 10.248.250.158
real    0m 5.00s
user    0m 0.00s
sys     0m 0.00sCopy the code
  • time

test#

  • To check whether the string is empty, passhelp testTo view
Copy      -z STRING      True if string is empty.
    
      -n STRING
         STRING      True if string is not empty.Copy the code

Example:

Copy#! /bin/sh STRING="" # -z if [ -z "$STRING" ]; then echo "STRING is empty" fi if [ -n "$STRING" ]; then echo "STRING is not empty" fi # STRING is emptyCopy the code
  • The correct way to tell if a string is empty in a Linux shell: An interesting example that underscores the importance of needing quotes
  • Linux Shell programming string null conditional judgment? Bash’s built-in test command returns true if it has only one argument as long as the argument is not null

tcpdump#

The tcpdump -d command is used to list the network interfaces that can capture packets.

Copy$ tcpdump -D
1.virbr0
2.docker0
3.bluetooth0 (Bluetooth adapter number 0)
4.nflog (Linux netfilter log (NFLOG) interface)
5.nfqueue (Linux netfilter queue (NFQUEUE) interface)
6.usbmon1 (USB bus number 1)
7.usbmon2 (USB bus number 2)
8.wlp3s0
9.enp5s0
10.any (Pseudo-device that captures on all interfaces)
11.lo [Loopback]Copy the code

Lo is localhost. The special interface, any, can be used to capture packets of all active network interfaces.

Copy$ sudo tcpdump -i any -c5 -nn icmp port 80 -A -w webserver.pcap

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
22:10:51.809330 IP 192.168.3.43.22 > 192.168.3.66.50051: Flags [P.], seq 2040167007:2040167203, ack 59350146, win 1432, options [nop,nop,TS val 89942170 ecr 111122685], length 196
22:10:51.812472 IP 192.168.3.43.22 > 192.168.3.66.50051: Flags [P.], seq 196:416, ack 1, win 1432, options [nop,nop,TS val 89942173 ecr 111122685], length 220
22:10:51.833093 IP 192.168.3.66.50051 > 192.168.3.43.22: Flags [.], ack 0, win 23490, options [nop,nop,TS val 111123121 ecr 89942122], length 0
22:10:51.833193 IP 192.168.3.43.22 > 192.168.3.66.50051: Flags [P.], seq 416:612, ack 1, win 1432, options [nop,nop,TS val 89942194 ecr 111123121], length 196
22:10:51.835541 IP 192.168.3.66.50051 > 192.168.3.43.22: Flags [.], ack 196, win 23487, options [nop,nop,TS val 111123121 ecr 89942170], length 0
5 packets captured
7 packets received by filter
0 packets dropped by kernelCopy the code
  • -cThe tcpdump option can be used to limit the number of captured packets
  • with-nOption to display IP address,-nnOption displays the port number
  • icmpI’m going to use it as a filter condition, just grab itICMPmessage
  • portSpecifies the port number by which packets are filtered
  • Tcpdump provides two options to view the contents of packets,-XPrint out the contents of data packets in hexadecimal format.-AThe ASCII value of the printed data packet
  • use-wOption to save packets instead of showing captured packets on the screen

Tcpdump stores packets in binary files, so you can’t simply open it with a text editor. Use the -r option to read the contents of the message in the file:

Copytcpdump -nn -r webserver.pcapCopy the code

Use the host parameter to fetch only packets associated with a particular host:

CopySudo tcpdump -i any-c5-nn host 54.204.39.132Copy the code

You can use parentheses to create more complex filters, but enclose your filters in quotes inside the shell to prevent them from being identified as shell expressions:

Copy$sudo tcpdump -i any-c5-nn "port 80 and (SRC 192.168.122.98 or SRC 54.204.39.132)"Copy the code
  • srcCapture the source IP address
  • usedstFilter packets by destination IP/ host name
  • useandAs well asorLogical operator to create filter rules

Reference:

  • Bole online – Capture packets using tcpdump on the Linux command line
  • Operation and Maintenance -NAS storage packet capture analysis
  • Huang- Tcpdump common operations
  • Examples of Tcpdump introductory tutorial
  • Nine tcpdump examples

unzip#

CopyNot unzip - l - 0.1 - py2.7. An eggCopy the code
Copyunzip -o -d /home/sunny myfile.zipCopy the code

Unzip the myfile.zip file to /home/sunnyl/-o: overwrite the file without prompting; -d:-d /home/sunny Indicates to unzip the file to the /home/sunny directory.

Reference:

  • Zip decompression (unzip) command in Linux
  • Unzip the command

wc#

CopyWc [-clw][--help][--version][file...]Copy the code

Parameters:

Copy-c or --bytes or --chars displays only the number of bytes. -l or --lines displays only the number of lines. -wor --words displays only the word count. --help Online help. --version Displays version information.Copy the code

who#

Who // Display the current login user Displays the title bar

Copy# who -HCopy the code

Only the current user is displayed

Copy# who -m -HCopy the code

wget#

Download the entire contents of the remote directory to the save directory. -nd: If the remote directory also has subdirectories, the system downloads the files in the subdirectory without creating additional directories.

CopyWget - r - np - nd - r "index. The HTML *" -p test # http://xxx/FusionUpgrade/master/euler/20181101130551/ note that require a/at the end of the URL, or you will recursively downloadCopy the code
  • -r: downloads all files in a certain directory (including subdirectories) on a specified web page recursively
  • -nd: — no-directories do not create directories
  • -np: — no-parent Do not trace back to the parent directory
  • P: specify the directory to save the download. None will be created automatically
  • -nH: — no-host-directories do not create a host directory

Example 2:

Download the remote folder as it is, and the downloaded local path is also the remote directory, instead of creating multi-level directories. -nh indicates that the xxx.com directory will not be created. –cut-dirs will not download the other redundant directories. The implementation effect will only be downloaded to the local DLRN_RPMS directory.

Copywget -r -p -k -np -nH --cut-dirs=4 http://xxx.com/cps/FusionNetwork-for-fc/master/suse/DLRN_RPMS/Copy the code

Reference:

  • Wget file download
  • wget
  • Wget downloads files recursively

watch#

CopyWatch - d 'ls -l | grep SCF' # monitoring the current directory SCF 'file changes watch - n 10' cat/proc/loadavg '# 10 seconds an output system load average watch - n - 1 d netstat -ant # : highlights the number of network links every secondCopy the code

Reference:

  • Watch – command

Systemd service service#

  • Systemd tutorial: Commands

xargs#

To quickly change the suffix name

Under source folder:

CopyCentOS-base.repo.repo.bak
epel.repo.repo.bakCopy the code

Method one:

Copyls *.bak|awk -F. '{print $1}'|xargs -t -i mv {}.repo.repo.bak {}.repoCopy the code

** friends think useful words, move a hand to point out a “recommendation” bar 🙂 **

By Michael Xiang

Source: www.cnblogs.com/michael-xia…

This site uses “signature 4.0 international” creation and sharing agreement, please indicate the author and source in the obvious position of the article.