经常使用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

相关文章
相关标签/搜索