Contents

command

Open Go 100 Days Code

  1. backslash 1 for shell command, (newline) represent a same line

  2. vairables with curly brackets variable expansion ${}

    1. distinguish it from other string: echo “${foo}name”
    2. excute some script: get array ${array[1]} replace variables: ${var/de/12};
  3. curly braces $(): command expansion

awk vs sed: text process

differ:

  1. sed:
  2. awk: more powerful
  1. print match
1
awk /rpc/ a.txt 
  1. print some colume
1
2
3
4
5
6
7
8

data.txt
a b
a1 b1

awk  '{print  $1}' data.text
a
a1
  1. find some columme is equal to a value
 1
 2
 3
 4
 5
 6
 7
 8
 9
10


cat a.txt
A    B    C
Tarun    A12    1
Man    B6    2

awk '{ if($2 == "B6") print $0;}'

Man B6 2 
  1. get total count: if … do
1
awak '/aaa/{count++} END {print count}' a.txt
  1. print the match
1
sed -n '/rpc/p' a.txt
1
2
3
sed 's/foo/boo/g' data.txt #  g: search in glbal model

sed '/^\s*$/d' log.txt  # d: delete line  

![](Paste Link)

word, line, character, and byte count

-l: line -w: worods -c: characters

read and excute commands from the file

define some global variable

1
foo='go' // a.xt

source a.txt

  1. du: display directory usage
    1. -s(summarize): total for the direcotry
    2. -a: all file size
1
2
du -h #current directory and subdirecotries size
du -s # the current directory size
  1. mkdir
    1. -p: create intermediate directories as required
  1. mv
  1. rm
    1. -r –recursive: remove the directories and their contents
    2. -f –force:
      1. ignore prompt(whether to remove the file)
        1. a file is unwritable;stdin is a terminal,
      2. not show any error message if the file not exis;

mv source target rename: if target isn’t exist;

move: target is a directory and exist;

  1. -f –force: force without prompt,prompt when

    1. a file is write protected
      1
      
      chmod 444 a.txt; rm a.txt
      
  2. -r –recursive: remove directory and their contents recursively

tree -l num: depth, current dirctory is 1; tree -d: print directories only;

create request

1
2
3
4
curl -X POST  \
-d  '{"hello":"world"}' \
-H 'Content-Type: application/json' \
http://baidu.com
1
2
3
curl -X POST \
-F 'image=@/home/user/Pictures/wallpaper.jpg' \
HTTP://baidu.com

change the standinput(keyboard) standout(screen) to another object

  1. three open file:

    1. 0:
    2. 1: standard output(default in terminal )
    3. 2: standard error(default in terminal )
  2. >: overwrite file

  3. >>: append file

  4. case:

    1
    2
    
    >/dev/null = 1>/dev/null
    2>&1: error 
    
  1. EOF: represent start[end] of file;

  2. single arror vs double array output: single: overwrite double: append input: single: for file double:

    1
    2
    3
    
    cat << EOF
    ab
    EOF
    
  3. change the output to a file echo 'a' > a.txt

  4. change the input to a file

1
cat < a.txt

: overwrite

: append

-maxdepth:

-mindepth:

-name: file name

-type: f: file

  • : last dirctory

  1. wget:download

    1. -c: resume getting a partially-downladed file
    2. -O: the name of the downloaded file
  2. tar: compress or exact file: tar xvfz

    1
    2
    
    tar -cvf a.tar file1 fil2
    tar -xvf a.tar
    
    1. -C: the target directory

    2. -f: the archive file

    3. -v: verbose

    4. -z: work only in c mode, compress the file with gzip

    5. mode:

      1. -c: compress
      2. -x: exact

what: change owner

director:777 files:6666

rsync source to dest

argumetns:

  1. -a: archive mode, which preserves file permissions, ownership, timestamps, etc.
    1. -t--times preserve modification times
    2. -g--group preserve group
    3. -o–owner` preserve owner (super-user only)
  2. -z: comprese during tranfering

what happen if uid not exist in des host: the file will owed by souce host owner’s id

1
2
3
4
5
6
a.txt  tang(501)

rsync -a  1.2.3.3:/

ls  1.2.3.3/
a owenr = 501 

example:

  1. rsync - avz /movies/* /api : sync all files to /api/, /api/move1 /api/movie2

  2. rsync -avz /movies /api: sync directory and file to /api, /api/movies/movie1….

  3. parameters

    1. –exclude,–include: from left to right
    2. -a: preserve meta data;
      1. -r, –recursive recurse into directories
      2. -l, –links Copy symlinks as symlinks
      3. preserve
        1. -p, –perms Preserve permissions
        2. -t, –times Preserve times
        3. -g,-o, –Preserve group,user(only take effect in super user mode)
        4. –devices, This option causes rsync to transfer character and block device files to the remote system to recreate these devices.
        5. –specials: This option causes rsync to transfer special files such as named sockets and fifos.
    3. -c: compress during sync
    4. –include
    5. –exclude
  4. case: sync a.txt to b a/{c.txt,d.pdf, a1/a.txt} b

    1
    
    sync a/ b --include *.txt --inclue='*.txt' --include='*/' --exclude='*' -avc 
    

rsync /mnt/tang01/qbitdownload/movie/* /mnt/tang01/movies/movie ' –exclude='*' -avc –chmod=a=rwx'

ps: report the running process pgrep: get all pid of match name(only need partly match )

ps:

  1. ps -u root: get the state of owner’s root
1
2
3
4
5

  UID   PID TTY           TIME CMD
  501   390 ??         6:33.55 /usr/libexec/UserEventAgent (Aqua)
  501   393 ??        12:12.36 /usr/sbin/universalaccessd launchd -s
  501   396 ??         4:32.29 /usr/libexec/lsd
  1. ps aux : detail information of some pid
  2. -o: specify the output fromat
    1. ps -o user,group,pid -p 1234

pkill

  1. pkil processname: only partly match
1
pkill ginx, nginx will be ill
  1. pkill -x: full match