A wave of practical bash shell finishing

Time:2022-1-8

As a command line explorer, you may find yourself repeating the same command over and over again. If you always use SSH to enter the same computer, if you always connect a series of commands, if you always run a program with the same parameters, you may want to save a few seconds for your life in this constant repetition.

The solution is to use an alias. As you may know, aliases are a way for your shell to remember a specific command and give it a new name. In any case, aliases have some limitations. They are just shortcuts to shell commands and cannot pass or control parameters. So as a supplement, bash also allows you to create your own functions, which may be longer and more complex. It allows any number of parameters.

Of course, when you have delicious food, such as some kind of soup, you should share it with everyone. I have here a list of some of the most useful bash aliases and functions. Note that “the most useful” is just a statement. Whether an alias is useful depends on whether you need to use it in the shell every day.

Before you start your alias experience, here is an easy-to-use tip: if your alias is the same as the original command name, you can use the following tips to access the original command


  \command 

For example, if there is an alias ls that replaces the LS command. If you want to use the original LS command instead of the alias, call it:


  \ls 

Increase productivity

These aliases are really simple and really short, but most of them are to save a few seconds of your life, and maybe a few years of your life, maybe.


  alias ls="ls --color=auto"

Simple but very important. Make the LS command output in color.


  alias ll="ls --color -al"

List all the files in the directory in a color list.


  alias grep='grep --color=auto'

Similar, just output with color in grep.


  mcd() { mkdir -p "$1"; cd "$1";} 

One of my favorites. Create a directory and enter it: MCD [directory name].


  cls() { cd "$1"; ls;}

Similar to the previous function, enter a directory and list its contents: CLS [directory name].


  backup() { cp "$1"{,.bak};}

Simply create a backup of the file: Backup [file] will create [file] in the same directory bak。


  md5check() { md5sum "$1" | grep "$2";}

Because I hate manually comparing the MD5 check value of the file, this function calculates it and compares it: md5check [file] [check value].

2015629113812972.jpg (517×57)


  alias makescript="fc -rnl | head -1 >" 

It’s easy to create a script with the last command you ran: makescript [script name. Sh]


  alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo" 

It just produces a strong password in an instant.

2015629113917494.jpg (283×57)


  alias c="clear"

Can’t it be easier to clear your terminal screen?


  alias histg="history | grep"

Quickly search your command input history: histg [keyword]


  alias ..='cd ..'

Do you still need to enter CD to go back to the upper directory?


  alias ...='cd ../..'

Naturally, go to the upper two directories.

  


 extract() { 
    if [ -f $1 ] ; then 
     case $1 in 
      *.tar.bz2)  tar xjf $1   ;; 
      *.tar.gz)  tar xzf $1   ;; 
      *.bz2)    bunzip2 $1   ;; 
      *.rar)    unrar e $1   ;; 
      *.gz)    gunzip $1   ;; 
      *.tar)    tar xf $1   ;; 
      *.tbz2)   tar xjf $1   ;; 
      *.tgz)    tar xzf $1   ;; 
      *.zip)    unzip $1    ;; 
      *.Z)     uncompress $1 ;; 
      *.7z)    7z x $1    ;; 
      *)   echo "'$1' cannot be extracted via extract()" ;; 
       esac 
     else 
       echo "'$1' is not a valid file" 
     fi 
  }

It’s long, but it’s also the most useful. Extract any document type: extract: [compressed file]
system information

Want to know everything about your system as soon as possible?


  alias cmount="mount | column -t"

Output mount information in column format.

2015629113941637.jpg (640×344)


  alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/  /' -e 's/-/|/'" 

Recursively displays the directory structure in a tree structure.


  sbs() { du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';} 

The installation files are sorted by the size of the disk storage, and the list of files in the current directory is displayed.


  alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p" 

Take over the standard output and standard error of a process. Note that you need to install strace.


  alias meminfo='free -m -l -t'

See how much memory you have left.

2015629114001693.jpg (640×157)


  alias ps? = "ps aux | grep"

Can you easily find the PID: PS of a process? [name].


  alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"

Displays the current volume setting.

2015629114043981.jpg (127×59)

network

For all commands used on the Internet and local networks, there are also some magical aliases for them.


  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"

Download the entire website: websiteget [url].


  alias listen="lsof -P -i -n" 

Shows which application is connected to the network.

2015629114111928.jpg (640×158)


  alias port='netstat -tulanp'

Displays the active port.


  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}

Roughly display the number of unread messages in your Google Mail: Gmail [user name]


  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"

Get your public IP address and host name.


  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';} 

Returns the geographic location of your current IP address.
Maybe it’s useless

So what if some aliases are not all useful? They may still be interesting.


  kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}

Draw the dependency curve of kernel module. You can view pictures as needed.


  alias busy="cat /dev/urandom | hexdump -C | grep 'ca fe'" 

In the eyes of those non-technical people, you always look so busy and mysterious.

2015629114137208.jpg (640×442)

As a reward, here are plain text versions of all the aliases and functions I mentioned, which can be copied and pasted into your at any time bashrc。 (if you have copied here line by line, ha ha, you find that you have wasted a few seconds of your life ~)

   


#Productivity
  alias ls="ls --color=auto"
  alias ll="ls --color -al"
  alias grep='grep --color=auto'
  mcd() { mkdir -p "$1"; cd "$1";}
  cls() { cd "$1"; ls;}
  backup() { cp "$1"{,.bak};}
  md5check() { md5sum "$1" | grep "$2";}
  alias makescript="fc -rnl | head -1 >"
  alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo"
  alias c="clear"
  alias histg="history | grep"
  alias ..='cd ..'
  alias ...='cd ../..'
  extract() {
    if [ -f $1 ] ; then
     case $1 in
      *.tar.bz2)  tar xjf $1   ;;
      *.tar.gz)  tar xzf $1   ;;
      *.bz2)    bunzip2 $1   ;;
      *.rar)    unrar e $1   ;;
      *.gz)    gunzip $1   ;;
      *.tar)    tar xf $1   ;;
      *.tbz2)   tar xjf $1   ;;
      *.tgz)    tar xzf $1   ;;
      *.zip)    unzip $1    ;;
      *.Z)     uncompress $1 ;;
      *.7z)    7z x $1    ;;
      *)   echo "'$1' cannot be extracted via extract()" ;;
       esac
     else
       echo "'$1' is not a valid file"
     fi
  }
   
  #System info
  alias cmount="mount | column -t"
  alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/  /' -e 's/-/|/'"
  sbs(){ du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e';}
  alias intercept="sudo strace -ff -e trace=write -e write=1,2 -p"
  alias meminfo='free -m -l -t'
  alias ps?="ps aux | grep"
  alias volume="amixer get Master | sed '1,4 d' | cut -d [ -f 2 | cut -d ] -f 1"
   
  #Network
  alias websiteget="wget --random-wait -r -p -e robots=off -U mozilla"
  alias listen="lsof -P -i -n"
  alias port='netstat -tulanp'
  gmail() { curl -u "$1" --silent "https://mail.google.com/mail/feed/atom" | sed -e 's/<\/fullcount.*/\n/' | sed -e 's/.*fullcount>//'}
  alias ipinfo="curl ifconfig.me && curl ifconfig.me/host"
  getlocation() { lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\';}
   
  #Funny
  kernelgraph() { lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display -;}
  alias busy="cat /dev/urandom | hexdump -C | grep \"ca fe\""

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]