Giter VIP home page Giter VIP logo

cheatsheet-shell-a4's Introduction

1 Shell CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

See more CheatSheets from Denny: #denny-cheatsheets

1.1 Basic

NameComment
Redirect stdout/stderrls /tmp >/dev/null 2>&1
Deal with filenamebasename $f, dirname $f
Use timeout: avoid command hangtimeout 10 sh -c ‘ls -lt’
Restart shell without killing terminalexec -l $SHELL
Run sub-shellecho $BASH_SUBSHELL; ( echo "Running in subshell: $BASH_SUBSHELL" )
Run static code checklink: shellcheck

1.2 GNU tools

1.2.1 Check file

NameComment
Show file contentcat /etc/hosts
Show file content with line numberscat -n /etc/hosts
Show with line numbers, while excluding blank linescat -b /etc/hosts
Show the first 3 lineshead -n3 /etc/hosts
Show the last 3 linestail -n3 /etc/hosts
Keep tailing log filestail -f /var/log/system.log, taif /var/log/system.log
Show the 4th linesed -n '4p' /etc/hosts
Show 4th, 5th, 7th and 8th linessed -n '4,5p;7,8p' /etc/hosts
Show matched string with 3 lines before and aftergrep -C 3 "127.0.0.1" /etc/hosts
For table-like files, show 2nd columnawk -F'\t' '{print $2}' /etc/hosts
For table-like files, swap 1st and 2nd columnsawk -F'\t' '{print $2,$1}' /etc/hosts

1.2.2 Echo string

NameComment
Echo red textecho -e “hello,\e[0;31m there \e[0;31m”
Echo multiple linesecho -e “hello,\ndenny”
Echo bold textecho -e hello, “\033[1mThis is bold text.\033[0m”
Echo underlined textecho -e hello, “\033[4mThis is underlined text.\033[0m”

1.2.3 Check process via /proc

NameComment
Check process start commandcat /proc/$pid/cmdline
Check process environment variablescat /proc/$pid/environ
Check process ulimits settingcat /proc/$pid/limits

1.2.4 Copy files

NameComment
Copy one filecp /etc/hosts /tmp/hosts
Copy one foldercp -r /usr/local/bin/ /tmp/bin/
Copy for backupcp /tmp/hosts{,.bak}, ls -lth /tmp/hosts*

1.3 Shell Basic

1.3.1 cd

NameComment
Go to given foldercd /var/log/
Go to homecd
Go to parent foldercd ..
Go to previous foldercd -

1.3.2 Numeric

NameComment
*expr 5 \* 4
+let z=x+y, z=$x+$y
==int1 -eq int2, =[ $? -eq 0 ] && echo “good”=
>=int1 -ge =int2
>int1 -gt =int2
<=int1 -le =int2
<int1 -lt =int2
!=int1 -ne =int2

1.3.3 xargs

# Run grep for files filtered by find
find /var/log -name "*.log" | xargs grep -i error

# Loop with pipes
cat /etc/passwd | awk -F':' '{print $1}' | xargs -I{} sudo -l -U {} | grep -v "not allowed to"

1.4 Scripts

  • Compare command output
[ 0 -eq $(find ./data -name "*.txt" -type f -print | wc -l) ]
  • get ip from eth0
/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
  • Check if a string contains a substring
#!/bin/bash

S="Pineapple"

if [[ "${S}" == *apple* ]]; then
    echo "Yes"
else
    echo "No"
fi
  • Check if a string in a list

stackoverflow link

#!/usr/bin/env bash
IAAS_TYPE="gcp|aws"
TYPE="gcp"

if [[ "gcp|aws" == *${TYPE}* ]]; then
    echo "yes"
else
    echo "no"
fi

1.5 More Resources

License: Code is licensed under MIT License.

linkedin github slack

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.