a shell command (basic)

shell Command Summary (basic)

basis

  • Special symbols
    • Prompt: $ normal user, # root user
    • Double quote string will do the interpretation, not a single quote
    • $0: shell name
    • The introduction of a state order on $ ?:
    • $ @: All parameters
    • $ #: The number of all parameters
    • $ *: All parameters in a row to break out
  • grammar
    • "A = B" Assignment; "A = B" and the like sentence
    • length_of_var = $ {# was}
    • $ {Parameter: + expression} If the parameter has the value is not null, then the value of expression using
    • Arithmetic:
      • let result=a+b
        let a++
      • result=$[ a + b ]
      • result=expr a+b
        result=$(expr a+b)
      • Above only supports integer
    • Array:
      • Ordinary array, a numeric index
        • array=(test1 test2 test3)
          array[0]=test1 ...
        • echo $(array[*])
          echo $(#array[*])
      • Associative array, we need to declare declare -A array
    • [ "$ A" == "ON"] && cmd1: cmd2
      cmd1 cmd2 && represents cmd2 performed only when the return value. 1 cmd1
      cmd1 || cmd2 represents cmd1 returns only when 0 is executed cmd2
    • Generally used when condition judgment [] but if the time is preferably used in the comparison string [[]]
    • cd -
  • Basic Commands
    • export PATH=/usr/lib:$PATH
      • The environment variable PATH = / usr / lib: / lib when looking for sequential
    • Redirection:
      • top | grep xfr > xfr.log 2>&1
        top | grep xfr > xfr.log 2>>&1
      • cat file | tee out.txt | cat -n #tee can be read from the standard input, and outputs the specified area and the standard output
      • cat < test.log # After this command can wait for user input to know EOF
      • exec 3 <file # fd == 3 to open the file file. Mode is writable
      • cat - file # - is stdin file name
    • (Sub-shell)
      `` cmd dereference
    • cat
      • cat -s file # remove blank lines
      • cat -T file.py # to print in the form of tabs ^ I for troubleshooting the format of python
      • cat -n file
    • find
      • find. # list all files and subdirectories
      • find. -print0 # with '\ 0' to separate
      • find . -type [f,d,l] -[iname or name] "*.text"
      • find . -type f (-path '*.txt' [-and,-a,-or,-o] -path 'a.*')
      • find -regex. '*.. (py | sh) $' # regular expression
      • Support the operation of the document find -type f -user root -exec chown wu {};. # Must ends with;
    • xargs
      • xargs echo command called by default
      • cat arms.txt | xargs -d X -n 3 # # with three parameters with X as a delimiter dismantling every time you invoke the command
      • cat arms.txt |. xargs -I {} find -f {} .txt # -I disposed placeholder
    • tr
      • echo "HELLO WORLD" | tr 'A-Z' 'a-z'
      • echo "hello 123 world" | tr -d '0-9'
      • cat mult_blanks.txt | tr -s '\n'
    • base64
      • base64 a.jpg > out.file
      • base64 -d a.jpg > out.file
    • sort
      • sort -nr file.txt | uniq # -n in accordance with the numerical order
      • sort -k 2 file.txt | uniq -c # number of sort output in the second column
    • split
      • split -b 10k data.file -d -a 4 splited_filename_prefix
      • split -l 100 data.file
      • split server.log /SERVER/ -n 2 -f server -b "%02d.log"
    • Canonical correlation
      • name = $ {file_jpg%. *} # non-greedy
      • extension = $ {file_jpg # *.} # greedy
  • Document Management
    • comm
      • sort a.txt -o a; sort b.txt -o b
        comm a b -1 -2 Take the intersection of ab #
      • sort a.txt -o a; sort b.txt -o b
        comm a b | tr -d '\t' # Take a different line in ab
      • sort a.txt -o a; sort b.txt -o b
        comm a b -2 -3 #a-b
    • File permissions problem
      • drwxr-xr-x file: the first one is the file type, -: normal file, D: directory and the like; other parts are user, group, orher read and write, and execute permissions
      • chmod u=rwx, g=rw, o=r filename
      • chmod o+x; chmod a+x #all
      • chown slynux: users test.sh # changes to the ownership test slynux: users
      • chmod 777 ./dir -R
    • ln -s model_tensorrt models
    • find . -type l -print
    • file a.jpg
    • Mount loopback file
      • dd if = / dev / zero of = loopbackfile.img bs = 1G count = 1 # 1g application space, is initialized to zero
      • mkfs.ext4 loopbackfile.img # format
      • mount -o loop loopbackfile.img /mnt/loopback #挂载
      • or
      • losetup/dev/loop1 loopbackfile.img
      • mount /dev/loop1 /mnt/loopback
      • fdisk / dev / loop1 # file partition
      • losetup -o 32256 / dev / loop2 loopback.img # mount the first partition
    • diff patch
      • diff -u test_version1.txt test_version2.txt > test.patch
      • patch -p1 test_version1.txt <test.patch # first execution of the patch hit the version1, the second execution revoked
    • head -n 4 a.txt
    • tail -n 4 a.txt
  • Text Processing
    • wc
      • wc -l # rows
    • print directory tree tree -h #
    • grep
      • grep -E "[az] +" file # Regular Expressions
      • grep "/xfr/api/detect" file | grep "2019-01-01 13:00:00" -c #QPS,and
      • grep "fail to init xtrace" -n file # determine the search to a few lines in
      • grep 'pattern1|pattern2' xfr.log
        grep -e pattern1 -e pattern2
        grep -E 'pattern1|pattern2'
      • grep "test" filelist -lZ | xargs -0 rm # -Z with '\ 0' to separate search results
      • grep "test" xfr.log -C 3 # context
    • cut
      • cut -f 2,3 file
      • cut -f 2 -d ";" file
    • and
      • sed 's/a/b/' file
      • sed -i 's/a/b' file
      • sed 's/a/b/g' file
      • But '/ ^ $ / d' file
      • sed 's / [0-9] {3} / 0 / g'
      • echo this is an example | sed 's/\w+/[&]/g' #[this] [is] [an] [example]
      • sed 's: /*.**/ :: g' # delete comment
    • awk
      • awk 'BEGIN{} pattern {cmd} END{}' file
      • awk '{print $ NR, $ NF, $ 0, $ 1, $ 2}' file # outputs row number, number of fields, text, text in the first field, second field
      • awk '/ pattern /'
        awk 'NR <5'
      • awk -F ; 'print $1'
      • awk '{for(i=0;i<10;i++){print $0}}'
  • Network operations
    • wget
      • wget -t 5 url
      • wget -c url
    • curl
      • curl url --cookie-jar cookie_file --user-agant "Mozilla/5.0"
      • curl http://127.0.0.1:8080/check_your_status?user=Summer&passwd=12345678 #GET
      • curl -H "Content-Type:application/json" -X POST --data '{"message": "sunshine"}' http://localhost:8000/ #POST
      • Detailed
  • Detailed GIT version management
    • status and diff
      • git clone [email protected]: nanan1993 / learn.git; git status # clean working directory
        • On branch master
          nothing to commit, working directory clean
      • touch add_file; git status # new file, but no trace
        • Untracked files:
          (use "git add ..." to include in what will be committed)
          add_file
      • git add add_file; git status # establish tracking, you can see this already staged a
        • Changes to be committed:
          (use "git reset HEAD ..." to unstage)
          new file: add_file
      • echo "add a word" >> add_file; git diff # modify the file directly diff
      • git status
      • git add add_file; after git diff --cached #stage, diff not see the need diff --cached, diff is not scratch up changes, diff --cached display is already stored temporarily change
      • git commit -m "add_file" ; git status
    • git log, recommended that instead of using graphical tools
      • git log -p # commit code churn situation shown in detail
      • show only commit git log #
    • git fallback
      • commit rollback
      • git commit -m 'initial commit' # commit bad
      • git add forgotten_file # file a supplement
      • git commit --amend #amend, the commit will bring new forgetten_file, a commit upper cover
      • add fallback
        • git add -A # a.txt at this time should not submit files found
        • git reset HEAD a.txt # git will prompt you to revoke the stage for a
      • File modification rollback
        • git checkout - a.txt # did not modify the stage will be revoked, can not be retrieved
      • commit version rollback
        • RESET --soft the HEAD ^ Git
          Git RESET ~ 1 # on the HEAD --soft withdrawn once the commit, Reserved Code, Reserved staged
        • git reset --soft HEAD ^ # revoked reservation code staged withdrawal
        • git reset --hard HEAD ^ # revoked, the code is not retained
        • git reset --soft e7d5fa76488 # fall back to a particular commit
      • Undo push
        • Local commit adjusted to the desired version
        • git push or igin branch --force
      • Back operation fallback =. =
        • git reflog can see the point to change the label of the previous head, although head pointing change, but the corresponding snapshot inside should still be in this database, you can also get it back, this is key!
        • reset a label to be able to fall back to a previous version
    • Daily operations
      • git init
      • git clone
      • git add file
      • git commit -m "comment"
      • git push or igin master
      • git tag -a v1.4 -m 'my version 1.4'
      • git push origin v1.4
      • git remote -v
      • git remote add [shortname] [url]
      • git remote rename name1 name2
      • git remote rm remote_name
      • git checkout -b dev
      • git checkout -b dev origin / dev # pull from a remote branch above
      • git fetch upstream
      • git checkout master; git merge dev # merge a remote branch
        • This time may be required to resolve merge conflicts, this is actually not too much trouble for beginners think, is that there will be

          <<<<<<HEAD

          first version

          =========

          second version

          >>>>>>>>>dev

          like this, the above is the current branch, here is another branch, under treatment, there are many visual tools that can do this matter is not recommended to force the command line, do not you happier?
      • Pit father git daily operations
        • I made this mistake a few times, and share
          • If I modify a file in the master above, this modification may stage, but did not submit, then if this time you checkout to dev branch, the change will go along with you, if this change and dev above are in conflict, you may checkout is not the past. But the frightening thing is not conflict, then you will take these changes into the dev branch, if you are ready to commit in dev words above, these changes will be applied to dev branch to the top, while at the same time master will lose these commit out the code, resulting in less you somehow find some code. Of course, do not commit the code will be the go, but this caused confusion code, check up is very troublesome.
    • The basic file system
      • The relationship between each object looks like this, a snapshot of the file stored in the blob, tree is a collection of the blob (the equivalent of a snapshot of the project directory), commit point to a Tree,
        -
      • After the commit state is a multiple (branch), the respective set of snapshots corresponding to commit, commit has also the relationship between the recorded
      • branch is actually a pointer to a commit a
      • The current state of the file is actually a HEAD decision, it is a pointer to a pointer branch of
      • May develop over time it becomes a
      • Ok. . . It can be said is very clear point, these figures some knowledge from Pro Git , did not forget to praise carry water people drink water
    • Development Process
      • Finally mention git development process, to cooperate, then it
        • First look at the local git init environment, recommend the local public key to the web so do not lose the password to the Han
        • Next fork, you want to contribute code to your warehouse fork
        • clone to local
        • checkout dev # own development branch
        • Code Code
        • checkout main branch to a remote repository, fetch down to see if there are new changes, synchronization down
        • go dev
        • to push its own warehouse
        • Put a merge request, before the first compare it to check inspection
        • knock off

    Thanks: Pro Git

  • 网络操作
    • ifconfig wlan0 192.168.0.80 netmask 255.255.252.0 #设置ip/mask
    • ifconfig eth0 hw ether 00:1c:bf:23:aa:3d #设置mac地址
    • dns服务器: /etc/resolv.conf
      • host google.com
        nslookup google.com #查看DNS映射关系
      • /etc/hosts 保存了 IP_ADDRESS name1 name2 #可以自定义域名和ip的映射
        • echo "192.168..0.9 backserver" >> /etc/hosts #以后将backserver统一映射到192.168.0.9
    • route #打印路由表
      route有很多功能,通过面试题来简单了解一下
      • 如何用命令行方式给linux机器添加一个默认网关,假设网关地址为10.0.0.254?
        • route del default gw 10.0.0.123 #删除旧的默认网关
        • route add default gw 10.0.0.254
        • route add -net 0.0.0.0 netmask 0.0.0.0 gw 10.0.0.254 #和上一个等价
      • 192.168.1.0网段, 192.168.1.1网关的某一服务器想连入172.16.1.0/24段,该如何添加路由
        • route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1
        • route add -new 172.16.1.0/24 gw 192.168.0.1
        • 路由设置以后重启会失效,需要写入文件,不在赘述
      • 如果添加一个主机路由
        • route add host address gw 192.168.0.1
        • route add host address dev eth2
      • 路由表图示例:
    • traceroute destinationIP #跟踪路由状态
    • ssh
      • ssh user@host command1;command2 #远程执行 本地显示
      • ssh -X user@host command1 #执行远程命令,并使用本地主机上的X服务器
        更进一步说,X服务器是一个组件,它对图像数据流进行处理以后,对视窗进行操作,从而显示图像,图像显示功能,实际上并不是linux kernel的一部分
      • 端口转发:
        • ssh -L 8000:www.kernel.org:80 user@localhost #将localhost:8000转发到 www.kernel.org:80
        • ssh -L 8000:www.kernel.org:80 user@remote #将user@remote:8000转发到www.kernel.org:80
        • ssh -R 8000:localhost:80 user@remote #将远程主机端口8000转发到本地主机80,这个是提供反向代理用的,假设我有个server,ABC都无法直接访问,而server能够直接访问A,那么我可以在A和server之间建立一个反向代理,那么A就可以访问server,BC也就可以通过A对server进行访问了
    • lftp 使用ftp协议传输,可以 get filename 或者 put filename
    • lsof -n | awk '{print($2)}' | sort | uniq -c | sort -nr | more #当前打开句柄数
    • netstat -anp #这个很常用了
    • 一个添加网桥的例子,用来熟悉一下操作
      • ip link add br0 type bridge
      • ip link set dev eth1 master br0 #把eth1加入到网桥
      • ifconfig br0 10.0.0.2
      • echo 1> /proc/sys/net/ipv4/ip_forward #启用分组转发
      • 我们假设eth0连接到子网192.168.1.0,10.0.0.0/24的主机希望通过网桥到达这个子网
        • route add 192.168.1.0/16 gw 10.0.0.2
    • iptables详见iptables 详解
      • iptables -A OUTPUR -d 8.8.8,8 -j DROP
      • iptables -A OUTPUR -p tcp -dport 21 -j DROP
      • iptables -I INPUT -s 1.2.3.4 -j DROP
      • -A #向链中追加
        -I #向链头插入
        -d dst
        -s source
        -j #动作
  • 文件系统操作
    • fdisk -l #查看设备
    • mount /dev/sda1 / #挂载
    • df -h #查看文件系统状况

      ---恢复内容结束---


      下一篇会详细对一些复杂的命令进行分析和学习,加油啦!

Guess you like

Origin www.cnblogs.com/souther-blog/p/10926935.html