經常使用Linux命令

This is a linux command line reference for common operations.

Examples marked with ? are valid/safe to paste without modification into a terminal, so you may want to keep a terminal window open while reading this so you can cut & paste.
All these commands have been tested both on Fedora and Ubuntu.
html

個人實踐環境:redhat-release-5Server-5.4。java

apropos whatislinux

Show commands pertinent to string. See alsothreadsafeweb

man -t man | ps2pdf -> man.pdfshell

make a pdf of a manual pageexpress

which commandbash

Show full path name of commandless

time commandssh

See how long a command takeside

time cat

Start stopwatch. Ctrl-d to stop. See also sw

nice info

Run a low priority command (The 「info」 reader in this case)

renice 19 -p $$

Make shell (script) low priority. Use for non interactive tasks

man -t man | ps2pdf -> man.pdf 默認用不了。 到http://www.ghostscript.com/download/gsdnld.html下載ghostscript,當前最新版本時9.0.4。

配置的時候,告知須要更高版本的glibc。

image

升級一下glibc吧,到ftp://ftp.gnu.org/gnu/glibc/下載。配置的時候又報:

configure: error: gcc must provide the <cpuid.h> header

image 
一查,原來又要升級GCC,
https://bugs.gentoo.org/292174?id=292174

image

耐着性子去http://gcc.gnu.org/,又Download最新的GCC4.6.1,配置時又報一堆依賴的東東。

想一想何須呢,爲了這麼個ghostscript費這麼大週摺,找個低版本的試試吧。因而下載了ghostscript-9.02,配置,編譯,安裝,一切都很順利。

再來運行 man -t man | ps2pdf -&gt; man.pdf,這回就OK了。

image

看看pdf效果。

image

which command 也是極其有用的一個命令。 

公司的雲平臺不少東西都是預先設好的,起初我連java安裝在哪裏都不知道,找了半天。有了找個命令,一下搞定。

image

目錄導航 dir navigation

cd -

Go to previous directory

cd

Go to $HOME directory

(cd dir && command)

Go to dir, execute command and return to current dir

pushd .

Put current dir on stack so you can popd back to it

 

文件查找 file searching

alias l='ls -l -color=auto'

quick dir listing

ls -lrt

List files by date. See also newest andfind_mm_yyyy

ls /usr/bin | pr -T9 -W$COLUMNS

Print in 9 columns to width of terminal

find -name '*.[ch]' | xargs grep -E 'expr'

Search ‘expr’ in this dir and below. See alsofindrepo

find -type f -print0 | xargs -r0 grep -F 'example'

Search all regular files for ‘example’ in this dir and below

find -maxdepth 1 -type f | xargs grep -F 'example'

Search all regular files for ‘example’ in this dir

find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done

Process each item with multiple commands (in while loop)

find -type f ! -perm -444

Find files not readable by all (useful for website)

find -type d ! -perm -111

Find dirs not accessible by all (useful for web site)

locate -r 'file[^/]*.txt'

Search cached index for names. This re is like glob *file*.txt

look reference

Quickly search (sorted) dictionary for prefix

grep -color reference /usr/share/dict/words

Highlight occurances of regular expression in dictionary

alias l='ls -l -color=auto' 在RHEL下應爲 alias l='ls -l --color=auto'。原文中有不少這樣的錯誤,兩個「-」,變成了一個長的「—」估計做者也是在Windows下寫的。

歸檔壓縮 archives and compression

gpg -c file

Encrypt file

gpg file.gpg

Decrypt file

tar -c dir/ | bzip2 &gt; dir.tar.bz2

Make compressed archive of dir/

bzip2 -dc dir.tar.bz2 | tar -x

Extract archive (use gzip instead of bzip2 for tar.gz files)

tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'

Make encrypted archive of dir/ on remote machine

find dir/ -name '*.txt' | tar -c --files-from=- | bzip2 &gt; dir_txt.tar.bz2

Make archive of subset of dir/ and below

find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ --parents

Make copy of subset of dir/ and below

( tar -c /dir/to/copy ) | ( cd /where/to/ && tar -x -p )

Copy (with permissions) copy/ dir to /where/to/ dir

( cd /dir/to/copy && tar -c . ) | ( cd /where/to/ && tar -x -p )

Copy (with permissions) contents of copy/ dir to /where/to/

( tar -c /dir/to/copy ) | ssh -C user@remote cd /where/to/ && tar -x -p

Copy (with permissions) copy/ dir to remote:/where/to/ dir

dd bs=1M if=/dev/sda | gzip | ssh user@remote 'dd of=sda.gz'

Backup harddisk to remote machine

試試加密解密,就加密剛剛建立的man.pdf吧。

image 

重複輸入passphrase。

image

完後查看,會在當前目錄生成一個.gpg文件。

image

再來試着解密,輸入剛纔的passphrase。

image

tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg' 感受不是很實用,加密時會讓你輸入passphrase,ssh登陸也須要輸入用戶密碼,重疊到一塊兒,容易弄錯。

rsync (Network efficient file copier: Use the –dry-run option for testing)

rsync -P rsync://rsync.server.com/path/to/file file

Only get diffs. Do multiple times for troublesome downloads

rsync –bwlimit=1000 fromfile tofile

Locally copy with rate limit. It’s like nice for I/O

rsync -az -e ssh –delete ~/public_html/ remote.com:’~/public_html’

Mirror web site (using compression and encryption)

rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh .remote:/dir/

Synchronize current directory with remote one

上面的命令默認連遠程用戶的root 用戶,換成同事的帳號試了試。

rsync -P user@remote:/home/user/sync/hadoop-0.21.0.tar.gz test-hadoop.tar.gz

image

rsync --bwlimit=1000 hadoop-0.21.0.tar.gz local-hadoop-0.21.0.tar.gz

設成1000,速度還真是慢,現學現用了一把 time。

time rsync --bwlimit=1000000000 hadoop-0.21.0.tar.gz local-hadoop-0.21.0.tar.gz

image

rsync -az -e ssh --delete hadoop-0.21.0.tar.gz user@remote:'/home/user/sync/hadoop-0.21.0.tar.gz'

沒大弄明白這條命令到底作什麼用。

第四條命令,搞不懂後面的 && rsync -auz -e ssh .remote:/dir/ 是幹嗎的,下面這條就能夠同步遠程目錄到當前目錄。

rsync -auz -e ssh user@remote:/home/user/sync/ .

ssh (Secure SHell)

ssh $USER@$HOST command

Run command on $HOST as $USER (default command=shell)

ssh -f -Y $USER@$HOSTNAME xeyes

Run GUI command on $HOSTNAME as $USER

scp -p -r $USER@$HOST: file dir/

Copy with permissions to $USER’s home directory on $HOST

ssh -g -L 8080:localhost:80 root@$HOST

Forward connections to $HOSTNAME:8080 out to $HOST:80

ssh -R 1434:imap:143 root@$HOST

Forward connections from $HOST:1434 in to imap:143

ssh 屬於常見命令,再也不囉嗦,提一下xeyes,能夠參見 http://en.wikipedia.org/wiki/Xeyes

wget (multi purpose download tool)

(cd dir/ && wget -nd -pHEKkhttp://www.joinebook.com/cmdline.html)

Store local browsable version of a page to the current dir

wget -c http://www.example.com/large.file

Continue downloading a partially downloaded file

wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/

Download a set of files to the current directory

wget ftp://remote/file[1-9].iso/

FTP supports globbing directly

wget -q -O- http://www.joinebook.com/linux-command.html | grep 'a href' | head

Process output directly

echo 'wget url' | at 01:00

Download url at 1AM to current dir

wget –limit-rate=20k url

Do a low priority download (limit to 20KB/s in this case)

wget -nv –spider –force-html -i bookmarks.html

Check links in a file

wget –mirror http://www.example.com/

Efficiently update a local copy of a site (handy from cron)

第一條命令的連接已經失效,另外-pHEKk後還須要一個空格和URL分開。

wget -nd -pHEKk http://www.joinebook.com/linux-command.html

中斷下載測試,下載QQ Linux 版玩玩吧,wget http://3.duote.org/qq2011beta3.zip
image

中斷再繼續,wget -c http://3.duote.org/qq2011beta3.zip

echo 'wget http://www.joinebook.com/linux-command.html' | at 01:00

image

networking (Note ifconfig, route, mii-tool, nslookup commands are obsolete)

ethtool eth0

Show status of ethernet interface eth0

ethtool --change eth0 autoneg off speed 100 duplex full

Manually set ethernet interface speed

iwconfig eth1

Show status of wireless interface eth1

iwconfig eth1 rate 1Mb/s fixed

Manually set wireless interface speed

iwlist scan

List wireless networks in range

ip link show

List network interfaces

ip link set dev eth0 name wan

Rename interface eth0 to wan

ip link set dev eth0 up

Bring interface eth0 up (or down)

ip addr show

List addresses for interfaces

ip addr add 1.2.3.4/24 brd + dev eth0

Add (or del) ip and mask (255.255.255.0)

ip route show

List routing table

ip route add default via 1.2.3.254

Set default gateway to 1.2.3.254

tc qdisc add dev lo root handle 1:0 netem delay 20msec

Add 20ms latency to loopback device (for testing)

tc qdisc del dev lo root

Remove latency added above

host boykma.pro

Lookup DNS ip address for name or vice versa

hostname -i

Lookup local ip address (equivalent to host `hostname`)

whois joinebook.com

Lookup whois info for hostname or ip address

netstat -tupl

List internet services on a system

netstat -tup

List active connections to/from system

轉到這裏,已然有些倦了,留待下次完成剩餘部分。

原文地址:http://www.joinebook.com/linux-command.html

相關文章
相關標籤/搜索