Shell實用技巧

以下內容大部分來自於All commands,感謝那些分享的人們!
Run the last command as root

$ sudo !!

Serve current directory tree at http://$HOSTNAME:8000/

$ python -m SimpleHTTPServer

Runs previous command but replacing

$ ^foo^bar

Rapidly invoke an editor to write a long, complex, or tricky command

$ ctrl-x e

Place the argument of the most recent command on the shell!

$ 'ALT+.' or <ESC> .'

Get your external IP address

curl ifconfig.me/ip -> IP Adress

curl ifconfig.me/host -> Remote Host

curl ifconfig.me/ua ->User Agent

curl ifconfig.me/port -> Port

$ curl ifconfig.me

Quick access to the ascii table

$ man ascii

Download an entire website!

-p parameter tells wget to include all files, including images.

-e robots=off you don’t want wget to obey by the robots.txt file

-U mozilla as your browsers identity.<\ br>
–random-wait to let wget chose a random number of seconds to wait, avoid get into black list.

Other Useful wget Parameters:

–limit-rate=20k limits the rate at which it downloads files.

-b continues wget after logging out.

-o $HOME/wget_log.txt logs the output

$ wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com

A very simple and useful stopwatch

$ time read

Put a console clock in top right corner

$ while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &

List of commands you use most often

$ history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

32bits or 64bits?

$ getconf LONG_BIT

Display the top ten running processes - sorted by memory usage!

$ ps aux | sort -nk +4 | tail

Quickly rename a file

$ mv filename.{old,new}

Push your present working directory to a stack that you can pop later

If are a Bash user and you are in a directory and need to go else where for a while but don’t want to lose where you were, use pushd instead of cd.

cd /home/complicated/path/.I/dont/want/to/forget

pushd /tmp

cd thing/in/tmp

popd (returns you to /home/complicated/path/.I/dont/want/to/forget)

$ pushd /tmp

Create a script of the last executed command!

$ echo "!!" > foo.sh

Delete all files in a folder that don’t match a certain file extension!

$ rm !(*.foo|*.bar|*.baz)

Reuse all parameter of the previous command line!

$ !*

Remove duplicate entries in a file without sorting!

$ awk '!x[$0]++' <file>

Display which distro is installed!

$ cat /etc/issue

Google Translate

Usage: translate \ \ \

$ translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; }

Replace spaces in filenames with underscores!

$ rename 'y/ /_/' *

Define a quick calculator function!

$ ? () { echo "$*" | bc -l; }

mkdir & cd into it as single command

$ mkdir /home/foo/doc/bar && cd $_

Show numerical values for each of the 256 colors in bash

$ for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done

Get the 10 biggest files/folders for the current direcotry!

$ du -s * | sort -n | tail

Create a quick back-up copy of a file

$ cp file.txt{,.bak}

Convert single digit to double digits

$ for i in ?.ogg; do mv $i 0$i; done

Create a single PDF from multiple images with ImageMagick

$ convert *.jpg output.pdf

Delete all empty lines from a file with vim!

$ :g/^$/d

Save man-page as pdf

$ man -t awk | ps2pdf - awk.pdf

Append content to file

$ cat footnotes.txt >> file

Generate a random password 30 characters long

$ Generate a random password 30 characters long

Remove all files previously extracted from a tar(.gz) file

$ tar -tf <file.tar.gz> | xargs rm -r

Retry the previous command until it exits successfully

$ until !!; do :; done

List only the directories!

$ ls -d */

Draw kernel module dependancy graph

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

Remind yourself to leave in 15 minutes(install leave before using)

$ leave +15

Download all images from a site!

$ wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images

Bring the word under the cursor on the :ex line in Vim!

$ :<C-R><C-W>

Compare two directory trees!

$ diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)

Add timestamp to history

$ export HISTTIMEFORMAT="%F %T "

Recursively change permissions on files, leave directories alone!

$ find ./ -type f -exec chmod 644 {} \;

Find files that have been modified on your system in the past 60 minutes!

$ sudo find / -mmin 60 -type f

find files in a date range

$ find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01"

Remove blank lines from a file using grep and save output to new file!

$ grep . filename > newfilename

Random Number Between 1 And X!
If X is 5, it will about a number between 1 and 5 inclusive.
This works in bash and zsh.
If you want between 0 and 4, remove the +1.

$ echo $[RANDOM%X+1]

prints file with line numbers!

$ nl

Go to parent directory of filename edited in last command!

$ cd !$:h

convert filenames in current directory to lowercase!

$ rename 'y/A-Z/a-z/' *

Using awk to sum/count a column of numbers!

$ cat count.txt | awk '{ sum+=$1} END {print sum}'

List all authors of a particular git project!

$ git log --format='%aN' | sort -u
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章