Useful bash aliases and functions

Source: Internet
Author: User
Tags uncompress strong password egrep

This article comes from: The program engineer

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

The solution is to use a nickname (alias). As you might know, aliases use a way to let your shell remember a specific command and give it a new name. In any case, the alias has some limitations, it is just a shortcut to the shell command, and cannot pass or control its parameters. So as a supplement, Bash also allows you to create your own functions, which may be a bit longer and more complex, and it allows any number of arguments.

Of course, when you have food, such as some kind of soup, you have to share it to everyone. I have a list here that lists some of the most useful bash aliases and functions. Note that the "most useful" is just a saying, the usefulness of aliases depends on whether you need to use it in the shell every day.

Before you start your alias experience, here's a handy tip: If your alias is the same as the original command, you can use the following techniques to access the original command (LCTT: You can also access it directly from the full path of the original command. )


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


Increase productivity

These aliases are really simple and really short, but most of them are to save your life for a few seconds, and in the end it may save you a few years, perhaps.

Alias ls= "LS--color=auto"

Simple but very important. Bring the LS command with color output.

Alias ll= "LS--color-al"

Lists all the files in the directory in a colored list.

Alias grep= ' grep--color=auto '

Similarly, just output the color in grep.

MCD () {mkdir-p "$"; CD "$";}

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

CLS () {CD "$"; ls;}

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

Backup () {CP "$" {,. bak};}

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

Md5check () {md5sum "$" | grep "$";}

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

Alias makescript= "FC-RNL | Head-1 > "

It's easy to create a script with your last Run command: makescript [script name. Sh]

Alias genpasswd= "Strings/dev/urandom | Grep-o ' [[: Alnum:]] ' | Head-n 30 | Tr-d ' \ n '; Echo

Just an instant to produce a strong password.

Alias c= "Clear"

Doesn't it make it easier to clear your terminal screen?

Alias histg= "History | grep

Quick Search for your command input history: HISTG [keywords]

Alias.. = ' CD. '

Do I need to enter a CD to go back to the upper directory?

Alias ... = ' CD. /..‘

Naturally, go to the top two-level catalogue.

Extract () {     If [-F $]; then case $ in         *.tar.bz2)   tar xjf $     ;;         *.tar.gz)    tar xzf $     ;;         *.BZ2)       bunzip2 $     ;;         *.rar)       Unrar e $     ;;         *.gz)        gunzip $      ;;         *.tar)       Tar xf $      ;;         *.TBZ2)      tar xjf $     ;;         *.tgz)       tar xzf $     ;;         *.zip)       unzip $       ;;         *. Z)         uncompress $  ;;         *.7z)        7z x $        ;;         *)     echo "' $ ' cannot be extracted via extract ()"          ; Esac      Else          echo "' $ ' is not a valid file"      fi}

Very long, but also the most useful. Unzip any document type: extract: [Compressed file]

System Information

Want to know all the information about your system as soon as possible?

Alias cmount= "Mount | Column-t "

Formats output mount information by column.

Alias tree= "Ls-r | grep ": $" | Sed-e ' s/:$//'-e ' s/[^-][^\/]*\//--/g '-e ' s/^//'-E ' s/-/|/' "

Displays the directory structure recursively, in a tree-shaped 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"): ($, "")}e ';}

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

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

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

Alias meminfo= ' Free-m-l-t '

See how much memory you have left.

Alias PS? = "PS aux | grep

Is it easy to 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 settings.


For all commands used in 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 site: websiteget [URL].

Alias listen= "Lsof-p-i-n"

Shows which application is connected to the network.

Alias port= ' NETSTAT-TULANP '

Displays the active port.

Gmail () {curl-u "--silent" Https:// "| sed-e ' s/<\/fullcount.*/\n/' | sed-e ' s/.* fullcount>//'}

Probably shows the number of unread messages in your Google mail: gmail [username]

Alias ipinfo= "Curl && Curl"

Get your public IP address and host name.

GetLocation () {lynx-dump 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 useless.

So what if some aliases don't all have value? 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 kernel module dependency graph. You need to be able to view pictures.

Alias busy= "Cat/dev/urandom | hexdump-c | grep ' CA fe ' "

In the eyes of those non-technical people you seem to be always so busy and mysterious.

Finally, a large part of these aliases and functions come from my personal. Bashrc. And those awesome sites and I've already covered in my post best online tools for Linux. You can take a look, if you like, you can share your. You are also welcome to comment here and share your wisdom.

As a bonus, here's a plain text version of all the aliases and functions I mentioned, ready to copy and paste into your. BASHRC. (If you have copied a row here, haha, you find that you have wasted a few seconds of life ~)

#Productivityalias ls="ls--color=auto"alias LL="ls--color-al"alias grep='grep--color=auto'MCD () {mkdir-P" $"; Cd" $";} CLS () {CD" $"; LS;} Backup () {CP" $"{,. bak};} Md5check () {md5sum" $"| Grep" $";} Alias Makescript="FC-RNL | head-1 >"alias genpasswd="strings/dev/urandom | grep-o ' [[: Alnum:] ' | head-n | tr-d ' \ n '; Echo"alias C="Clear"alias Histg="History | grep"alias..='CD..'alias ...='CD.. /..'Extract () {if[-F $1 ] ; Then Case$1 inch*.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"' $ ' cannot be extracted via extract ()" ;; EsacElseEcho"' $ ' is not a valid file"fi} #System infoalias cmount="Mount | column-t"alias Tree="Ls-r | grep":$"| sed-e ' s/:$//'-e ' s/[^-][^\/]*\//--/g '-e ' s/^//'-e ' s/-/|/'"SBS () {du-B--max-depth1| 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"): ($, "")}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"#Networkalias Websiteget="wget--random-wait-r-p-e robots=off-u Mozilla"Alias Listen="lsof-p-i-n"alias Port='NETSTAT-TULANP'Gmail () {Curl-U" $"--silent"Https://"| Sed-e's/<\/fullcount.*/\n/'| Sed-e's/.*fullcount>//'}alias Ipinfo="Curl && Curl"getlocation () {Lynx-dump Qry=$1|grep address|egrep ' city|state|country ' |awk ' {print $3,$4,$5,$6,$7,$8} ' |sed ' S\IP address flag \ \ ' |sed ' s\My\\ ';} #Funnykernelgraph () {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\""

Useful bash aliases and functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.