Linux Command Tips

Source: Internet
Author: User
Tags array definition

printf "%-5s%-10s%-4s\n" No Name Mark

printf "%-5s%-10s%-4.2f\n" 1 Sarath 80.3456

%-5s well-known a string substitution with a format of left-aligned and a width of 5 (-left justified)

Pgrep-f WeChat to find the PID with WeChat

var= "Hello"

echo $var and Echo ${var}

Get the length of the variable value:

var= ' [Email protected]#$ '

echo ${#var} #19

echo $ show what shell is used

-bash

echo $SHELL show what SHELL is used

/bin/bash

echo $UID display the current user ID

0

Adding environment variables using functions

Prepend () {[-D $] && eval $1=\ ' $2\$\{$1:+ ': ' \$$1\}\ ' && export $;}

How to use:

Prepend Path/opt/myapp/bin

Using the shell for mathematical operations

In the bash shell, both the expr and BC tools are used frequently when performing basic arithmetic operations using let (()) and [].

    1. #!/bin/bash
    2. No1=4;
    3. No2=5;
    4. Let Result=no1+no2
    5. Echo $result

    1. #!/bin/bash
    2. No1=10;
    3. Let no1++
    4. echo $no1
    5. Let no
    6. echo $no1

Shorthand:

    1. #!/bin/bash
    2. No1=10
    3. Let no1+=5
    4. echo $no1
    5. Let no1-=6
    6. echo $no1

The use of the operator [] is similar to the Let command:

#!/bin/bash
no1=2
No2=3
result=$[No1 + NO2]
Echo $result
#也可以使用 $ prefix
result=$[$no 1 + 5]
Echo $result

Expr

#!/bin/bash
No1=3
No2=4
result= ' expr $no 1 + $no 2 '
Echo $result

result= ' expr 5 + 10 '
Echo $result

result=$ (Expr $no 1 + 5)
Echo $result

Standard output and standard error

CMD 2>stderr.txt 1>stdout.txt

CMD 2>&1 output.txt and cmd &> output.txt effect

Tee command, get standard output and write to next file

Normal arrays and associative arrays (hash arrays)

Associative arrays are not supported after bash4.0 version

Array definition:

Array_var= (1 2 3 4 5 6)

array_var[0]= "Test1"

To print an array:
Echo ${array_var[0]}

echo ${array_var[*]} #打印所有数组成员

echo ${array_var[@]} #打印所有数组成员

echo ${#array_var [*]} #打印数组长度

Associative arrays: slightly

Find

Find. -iname "example*" #-iname Ignore letter case

Find. \ (-name "*.txt"-o name "*.pdf" \)-print #-o or operation

Find/home/users-path "*/slynux/*"-print #-path match file path with wildcard characters

Find. -regex ". *\ (\.py\|\.sh\) $" #用正则找

Find. -iregex ". *\ (\.py\|\.sh\) $" #让正则忽略大小写

Find. ! -name "*.txt"-print #找出所有不以txt结尾的文件名

Find. -maxdepth 1-name "f*"-print #最大深度为1, which is the current directory

Find. -mindepth 2-name "f*"-print #最小深度为2, find it from the next level directory

Find. -type d-print #找出所有目录, excluding files

Find. -type F-print #找出所有文件, excluding directories

Find. -type f-atime-7-print #7天内被访问过的恩所有文件

Find. -type f-atime 7-print #打印出恰好在7天前被访问过的所有文件

Find. -type f-atime +7-print #打印出访问时间超过7天的所有文件.

-atime-mtime-ctime #都是基于天

-amin-mmin-cmin #都是基于分钟

Find. -type f-amin +7-print #打印出访问时间超过7分钟的所有文件

Find. -type f-newer file.txt-print #找出比file. txt All files with more recent modification time

Find. -type f-size +2k #大于2kb的文件, B (512 bytes, block), C Byte, W Word (2 bytes), K (1024 bytes), M (1024k), G (1024M)

Find. -type f-size-2k #小于2kb的文件

Find. -type f-size 2k #大小等于2kb的文件

Find. -type f-name "*.SWP"-delete #删除swp文件

Find. -type f-perm 644-print #查找权限为644的文件

Find. -type f-name "*.php"! -perm 644-print #找出服务器上所有的php文件并且权限不是644的.

Find. -type F-user Alex-print #找出所有alex拥有的文件

Split file

Split-b 10k data.file #把文件切割成10k大小 data.file xaa xab xac xad

Split-b 10k Data.file-d-a 4 #切割成4位数字补全的大小data. File x0009 x0019 x0029 x0039

Split-b 10k Data.file-d-a 4 split_file #data. File split_file0000 split_file0001 split_file0002

Split-l Data.file #10行一割

Using loopback files

DD If=/dev/zero of=big.img bs=1g count=1

MKFS.EXT4 big.img

Mkdir/mnt/loopback

Mount-o Loop Big.img/mnt/loopback

Sed

Sed ' s/text/replace/' file >newfile

MV NewFile File

Equivalent to Sed-i ' s/text/replace/g ' file

Sed-i ' s/text/replace/3g ' file #从第三次找到的地方修改, change to the last

Echo This was an example | Sed ' s/\w\+/[&]/g ' #已匹配字符串标记使用 & matches the found content and adds [], [This] [is] [an] [example]

echo this was digit 7 in a number | Sed ' s/digit \ ([0-9]\)/\1/' #this is 7 in a number command digit 7, was replaced by 7. The substring to which the style matches is 7,\ (.. \) is used to match substrings, the first substring that matches to is marked as \1, and so on the second result that matches to \2, for example:
echo AAA BBB | Sed ' s/\ ([a-z]\+\) \ ([a-z]\+\)/\2 \1/' #BBB AAA

Awk

awk ' BEGIN {pring ' start '} pattern {commands} end {print ' End '} ' file

Work Flow:

1. Execute Begin{commands} statement fast

2. Read a line from the file or stdin and execute pattern{commands}. Repeat this process to know that the files are all read.

3. Last execute End{commands} statement block

Stitching strings

echo |  awk ' {var1= ' v1 ', var2= ' v2 '; var3= ' v3 '; print var1 "-" var2 "-" VAR3;} ' #v1-v2-v3

Special variables:

NR: Indicates the number of records, corresponding to the line number during execution
NF: Indicates the number of fields, the number of fields corresponding to the current row during execution
$: This variable contains the text content of the current line during execution
$: The text content of the first field
$: Text content for the second field

Echo-e "line1 F2 f3\nline2 f4 f5\nline3 f6 F7" | awk ' { print ' Line no: "NR", No. of Fields: "NF," $0= "$," $1= "$," $2= "," $3= " $ "

Results:

Line No:1,no of Fields:3 $0=line1 F2 f3 $1=line1 $2=f2 $3=f3

Line No:2,no of Fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5

Line No:3,no of Fields:3 $0=line3 f6 F7 $1=line3 $2=f6 $3=f7

awk operations:

Seq 5 | awk ' begin{sum=0;print ' summation: '}{print $ ' + ' sum+=¥1}end{print ' = ';p rint sum} '

Results:

Summation:

1 +

2 +

3 +

4 +

5+

==

15

Example:

awk ' end{print filename} ' awk.txt print file name

Example:

Cat Section.txt
Line with PATTERN1
Line with PATTERN2
Line with PATTERN3
Line End With PATTERN4
Line with PATTERN5


awk */pa.*3/,/end/* section.txt
The output is:
Line with PATERN3
Line End With PATTERN4

Linux Command Tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.