|
Explorer as a command line, you may find yourself over and over again to repeat the same command. If you always use ssh into the same computer, if you are always connected to a series of commands, if you always run a program with the same parameters, you may want to keep repeating this for your life savings under A few seconds.
Solution is to use an alias (alias). As you may know, with an alias to make your shell to remember a particular command and give it a new name way. Anyway, there are some limitations alias, it is just a shortcut to shell commands, which can not 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 food, such as some kind of soup you want to share it to others. I have here a list of some of the most useful bash aliases and functions. Note that the "most useful" just saying, alias usefulness depends whether you need every day inside the shell to use it.
Before you begin your alias experience tour, here's a little trick easy to use: If your alias and the original command of the same name, you can access the original command (LCTT Annotation with the following tips: You can also go directly original the full path to the command to access it.)
\ Command
For example, if there is a replacement of the ls command alias ls. If you want to use the original ls command instead of the alias by calling it:
\ Ls
Increase productivity
These aliases are really really simple and short, but most of them is to give your life to save a few seconds, you may end up saving this life a few years, maybe it.
alias ls = "ls --color = auto"
Simple but very important. So ls command with color output.
alias ll = "ls --color -al"
In color mode list all the files listed in the directory.
alias grep = 'grep --color = auto'
Similarly, just bring in the grep output in color.
mcd () {mkdir -p "$ 1"; cd "$ 1";}
One of my favorites. Create a directory and enter the directory: mcd [directory name].
cls () {cd "$ 1"; ls;}
On a similar function, enter a directory and list its contents: cls [directory name].
backup () {cp "$ 1" {, bak};.}
Simple to create a backup file: backup [file] created [File] in the same directory .bak.
md5check () {md5sum "$ 1" | grep "$ 2";}
Because I hate md5 checksum manually by comparing files, this function will be calculated and compared it: md5check [file] [checksum].
alias makescript = "fc -rnl | head -1>"
You can easily use the command to create a script that runs: makescript [script name .sh]
alias genpasswd = "strings / dev / urandom | grep -o '[[: alnum:]]' | head -n 30 | tr -d '\ n'; echo"
Just instantly generate a strong password.
alias c = "clear"
You can not clear the terminal screen easier, right?
alias histg = "history | grep"
Quick Search Enter your command history: histg [keyword]
alias .. = 'cd ..'
Back to the parent directory cd also need to enter it?
alias ... = 'cd ../ ..'
Naturally, the two go to the directory.
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' can not be extracted via extract ()" ;;
esac
else
echo " '$ 1' is not a valid file"
fi
}
Very long, but also the most useful. Extracting any type of document: extract: [archive]
system message
I would like to know as soon as possible all the information about your system?
alias cmount = "mount | column -t"
Output formatted by column mount information.
alias tree = "ls -R | grep": $ "| sed -e 's /: $ //' -e 's / [^ -] [^ \ /] * \ // - / g' -e 's / ^ / /' -e 's / - / | /' "
In a tree structure displays the directory structure recursively.
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 ';}
Installation file size disk storage sort the file list of the current directory.
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 over.
alias ps =? "ps aux | grep"
You can easily find a process PID: ps [name]?.
alias volume = "amixer get Master | sed '1,4 d' | cut -d [-f 2 | cut -d] -f 1"
Displays the current volume setting.
The internet
For all the use of the Internet and the local network commands, there are some amazing aliases to 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 applications to connect to the network.
alias port = 'netstat -tulanp'
Showing port activity.
gmail () {curl -u "$ 1" - silent "https://mail.google.com/mail/feed/atom" | sed -e '. s / < \ / fullcount * / \ n /' | sed -e 's /.* fullcount> //'}
Probably display your Google Mail in the number of unread messages: gmail [username]
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 \\ ';}
Return to your current location IP address.
Maybe useless
So, if some aliases are not all use value? They may still be funny.
kernelgraph () {lsmod | perl -e 'print "digraph \" lsmod \ "{"; < >; while (< >) {@ _ = split / \ s + /; print "\" $ _ [0] \ " -> \ "$ _ \" \ n "for split /, /, $ _ [3]} print"} " '| dot -Tpng | display -;}
Draw 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 staff are always so busy you look and mysterious.
Finally, a large part of these aliases and functions from my personal .bashrc. Like those very points and commandlinefu.com site alias.sh I had in my post best online tools for Linux which introduced. You can go and see, if you prefer, you can share with you. You are welcome to comment here, share your wisdom.
As a reward, there is a plain text version of all the aliases and functions I mentioned, you can always copy and paste into your .bashrc. (If you have copied line by line here, ha ha, that you're also a waste of life, a few seconds to)
#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' can not 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 \ "" |
|
|
|