# cheatsheets **Repository Path**: peliu/cheatsheets ## Basic Information - **Project Name**: cheatsheets - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-05-26 - **Last Updated**: 2026-09-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # cheatsheets cheatsheets # bash & linux ```bash #!/bin/bash set -xeuo pipefail # http://ezprompt.net/ a tool for PS1 settings. PS1="\[\e[35m\]\j\s\v \D{%Y-%m-%dT%H:%M:%S%z} \u@\h:\w/\[\e[m\]" alias ls='ls -atlrh --color=auto' bash -c "ls -atlrh /tmp" readlink -f file.txt # show full path of a file ls -atlrhd name*tag ls -S # Sort by size mkdir -p /dir1/dir2/{dira,dirb} #Cp mutiple files to a directory(bash only) cp /home/ankur/folder/{file1,file2} /home/ankur/dest cp -t dest_dir file1 file2 file3 for i in {1..150} ; do echo "$i"; sleep 1; done for i in $(seq 1 10) ; do echo "aaa${i}aaa"; done for i in aaa bbb ccc ; do echo $i; done for i in [aaa bbb ccc] ; do echo $i; done for h in `cat containers.txt`; do echo $h; done for d in ./*;do [[ -f "$d" ]] && echo "${d##./}"; done for d in ./*;do [[ -d "$d" ]] && e="${d##./}" && echo $e; done while true; do printf .; sleep 5;done while [[ true ]]; do ls -atlrh; sleep 5; done mount -t glusterfs -o acl ip:/export_path /local_path/mnt mount -t glusterfs -o ro ip:/export_path /local_path/mnt # readonly mount -t nfs ip:/export_path /local_path/mnt env TZ=Asia/Shanghai date +"%Y-%m-%d %H:%M:%S %z" date +P%Y%m%d%H%M%S%N%z | sed "s/+/E/g" | sed "s/-/W/g" ps -eo pid,comm,lstart,etime,time,args | grep tomcat # Show line numbers nl file.txt cat -b file.txt # Same as above cmd cat -n file.txt # same result, pipe or <() nl <(cat *.tmp | sed '/^NAME/i\\' ) cat *.tmp | sed -n '/^LAST/, /^NAME/p' | sed '/^LAST/i\\' | nl # Cmd to Create & Append to a file from command line command. Convenint for shell scripts usage cat < tmp.txt file lines EOF # Similar as above. Keep indent format & append lines to existed file cat <<-EOF >> tmp.txt file lines EOF uuidgen cmd1 && cmd2 && cmd3 cmd1 || cmd2 || cmd3 echo "aaabbbccc" | base64 echo "YWFhYmJiY2NjCg==" | base64 --decode ln -s [OPTIONS] (target)FILE LINK # change own for link itself instead of the link target. chown -h user1 jakarta # show file full path. Show link target path(not link file path) readlink -f filename echo $PWD/20200908182742UTC.sql nohup myprogram myprogram.log 2>&1 & # curl with user:pwd curl -u username:password http://example.com # curl upload file curl -i -X PUT -H "Content-Type: multipart/form-data" -F "apivarname=@locfilepath" -vvv http://localhost:8080/api/v1/method # http://host:port(8080/8081/...)/swagger-ui.html # curl output and pipe redirect curl -X PUT http://localhost:8080/api/v1/method -o std.out > out.std 2>&1 & curl -g -G --data-urlencode 'match[]={label=~"label1|label2"}' http://host:port/federate > out.prom curl -ss http://google.com > temp.html 2>&1 # Don't show process bar. -s options: silent. But double ss for versions compatible & all output(err, out) so ending with 2>&1. curl -k -u user:pass -H "Content-Type:application/json" -H 'Authorization:bearer' -X POST -d '{"Id":"12330245","visitTimes":1,"docType":"sanwen","docId":"36e5854f5f0e4f80b7ccc6c52c063243"}' https://host:443/invoke/api/v2/method curl -k -u user:pass -H "Content-Type:application/json" -H 'Authorization:bearer' -X POST -d '@my.json' https://host:443/invoke/api/v2/method # Install package from source without root/sudo wget http://sourceforge.net/projects/sshpass/files/latest/download -O sshpass.tar.gz tar -xvf sshpass.tar.gz cd sshpass-1.06/ ./configure --help ./configure --prefix='/home/dir/user/sshpass/bin' make make install # ssh&scp usage alias ssh='ssh -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "ForwardAgent=yes" -o "TCPKeepAlive=yes" -o "ServerAliveInterval=30"' ssh -vvv -i private_key user@host # Verbose for debug. max v: 3. ssh -vC2qTnNf -D 6502 -i ~/.ssh/private.pem user@host # tunnel scp -o "ForwardAgent=yes" -i id_rsa -F config -oProxyJump=jmp user@host:/tmp/file locfile sshpass -e scp -J jumphost1,jumphost2 -F config -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "ForwardAgent=yes" deployment.yaml user@host:/directories/ scp -i private_key_path -oProxyJump=jum user@host:remote_file_path local_path sshpass -f filepath ssh -J jumphost1,jumphost2 -/F config -q -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "ForwardAgent=yes" user@host 'cmd' # Change the default setting for ssh login. Enable password access sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/selinux/config /etc/ssh/sshd_config systemctl restart sshd passwd shellcheck scripts.sh rsync -r --verbose --exclude 'exclude_pattern1' --exclude 'exclude_pattern2' source target rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude du -hs * | sort -hr # list subdirectory names in file # https://askubuntu.com/questions/994602/how-to-write-out-all-directory-names-into-a-text-file # Improving you current code, this command should be helpful: for d in ./*;do [[ -d "$d" ]] && echo "$d" >> dir.txt; done # To remove the ./ from the output: for d in ./*;do [[ -d "$d" ]] && echo "${d##./}" >> dir.txt; done for d in ./*;do [[ -f "$d" ]] && echo "${d##./}"; done for d in ./*;do [[ -d "$d" ]] && echo "${d##./}"; done for d in ./*;do [[ -d "$d" ]] && e="${d##./}" && zip -re -Ppasswd $e $e/ ; done # Show the items of the file for d in $(cat dir.txt); do echo $d; done for d in $(cat dir.txt); do zip -re $d $d/; done for d in $(cat dir.txt); do zip -re -Ppasswd $d $d/; done # https://stackoverflow.com/questions/2059794/what-is-the-meaning-of-the-0-syntax-with-variable-braces-and-hash-chara # See the section on Substring removal in the Advanced Bash-Scripting Guide—: ${string#substring} # Deletes shortest match of substring from front of $string. ${string##substring} # Deletes longest match of substring from front of $string. # The substring may include a wildcard *, matching everything. The expression ${0##/*} prints the value of $0 unless it starts with a forward slash, in which case it prints nothing. ``` ```bash # In case 'sudo cat' meet with 'Permission denied' issue please switch to 'sudo tee' sudo cat > /data/mysql/data_cnf/cnf/my.cnf << EOF [mysqld] ssl-ca=/var/lib/mysql/ca.pem ssl-cert=/var/lib/mysql/server-cert.pem ssl-key=/var/lib/mysql/server-key.pem EOF -bash: /data/mysql/data_cnf/cnf/my.cnf: Permission denied # 'sudo tee' does not have this issue sudo tee /data/mysql/data_cnf/cnf/my.cnf > /dev/null <<- EOF [mysqld] ssl-ca=/var/lib/mysql/ca.pem ssl-cert=/var/lib/mysql/server-cert.pem ssl-key=/var/lib/mysql/server-key.pem EOF ``` ## ssh config ```config Host jmp Hostname host User logonuser IdentityFile ~/.ssh/id_rsa HostKeyAlgorithms=+ssh-dss ForwardAgent yes LogLevel error StrictHostKeyChecking no UserKnownHostsFile=/dev/null TCPKeepAlive yes ServerAliveInterval 30 ``` ## cursor movement | Move by | Forward | Backward | | ------------------- | ------- | -------- | | word | M-f | M-b | | line | C-e | C-a | | switch line/current | C-x C-x | C-x C-x | ## delete characters | del by | End/Forward | Begin/Backward | | ------ | ----------- | -------------- | | word | | M-Backspace | | char | C-d | Backspace | | space | | C-w | | line | C-u | C-k | ## performance ```bash vmstat 5 10 sar 1 3 mpstat 1 3 # same with sar? ``` # Network ``` nmap hostname # check which ports are opened on the host ``` ## DNS ```bash # https://www.hostinger.com/tutorials/how-to-use-the-dig-command-in-linux/ dig +answer +short hostname.corp.com dig +answer +short -x ip.ip.ip.ip dig hostinger.com +trace https://www.cyberciti.biz/faq/linux-unix-dig-command-examples-usage-syntax/ dig Hostname dig DomaiNameHere dig @DNS-server-name Hostname dig @DNS-server-name IPAddress dig @DNS-server-name Hostname|IPAddress type nslookup ip # find hostname from ip nslookup hostname # find ip from hostname ``` [More](network.md) # springboot ```bash java -jar -Dspring.profiles.active=activeprofilename app.jar ``` # redis ```bash redis-server redis.conf redis-cli -c -p port -h host # scan scan 0 scan 17 sscan myset 0 match f* scan 176 MATCH *11* COUNT 1000 SCAN 0 TYPE zset hscan hash 0 docker run -it --rm redis redis-cli -h host -p 6379 # Just run it from commad line. It does NOT work to 'docker exec -it container_name sh' or 'docker run -it --rm image sh' to the container then run 'redis-cli -h host -p port' ``` # python ```bash docker run -it --rm -it --entrypoint=/bin/sh --name py3alpine python:3.8.3-alpine3.11 docker run -it --rm -it --entrypoint=/bin/sh --name py2 python:2 ``` ```python # pending... ``` # json ```bash cat json.txt | python -m json.tool yum install epel-release yum install jq cat json.txt | jd . ```