璺宠嚦涓昏鍏у

甯哥敤鍛戒护

Bing馃悾甯哥敤鍛戒护澶х害 8 鍒嗛挓

甯哥敤鍛戒护

ls鍛戒护

ls -a # 鍒楀嚭鐩綍鎵鏈夋枃浠讹紝鍖呭惈浠.寮濮嬬殑闅愯棌鏂囦欢
ls -A # 鍒楀嚭闄.鍙..鐨勫叾瀹冩枃浠
ls -r # 鍙嶅簭鎺掑垪
ls -t # 浠ユ枃浠朵慨鏀规椂闂存帓搴
ls -S # 浠ユ枃浠跺ぇ灏忔帓搴
ls -h # 浠ユ槗璇诲ぇ灏忔樉绀
ls -l # 闄や簡鏂囦欢鍚嶄箣澶栵紝杩樺皢鏂囦欢鐨勬潈闄愩佹墍鏈夎呫佹枃浠跺ぇ灏忕瓑淇℃伅璇︾粏鍒楀嚭鏉

cd 鍛戒护

# 闇瑕佽繘鍏ョ殑鐩綍鍦板潃
cd [鐩綍鍚]
# 杩涘叆涓荤洰褰
cd /
# 杩涘叆home鐩綍
cd ~
# 杩涘叆涓婁竴娆″伐浣滅洰褰
cd -
# 杩涘叆涓婄骇鐩綍
cd ..

pwd 鍛戒护

# 鏌ョ湅褰撳墠璺緞
pwd
# 鏌ョ湅杞摼鎺ョ殑瀹為檯璺緞
pwd -P

mkdir 鍛戒护

# 褰撳墠鐩綍涓嬪垱寤烘柊鏂囦欢澶
mkdir [鏂囦欢澶瑰悕绉]
# 鍦 tmp 鐩綍涓嬪垱寤鸿矾寰勪负 test/t1/t 鐨勭洰褰曪紝鑻ヤ笉瀛樺湪锛屽垯鍒涘缓
mkdir -p /tmp/test/t1/t

rm 鍛戒护

rm [閫夐」] 鏂囦欢
# 鍒犻櫎浠讳綍 .log 鏂囦欢锛屽垹闄ゅ墠閫愪竴璇㈤棶纭
rm -i *.log
# 鍒犻櫎 test 瀛愮洰褰曞強瀛愮洰褰曚腑鎵鏈夋。妗堝垹闄わ紝鐩存帴鍒犻櫎锛屼笉浼氱‘璁
rm -rf test

mv 鍛戒护

# 灏嗘枃浠 test.log 閲嶅懡鍚嶄负 test1.txt
mv test.log test1.txt
# 灏嗘枃浠 log1.txt,log2.txt,log3.txt 绉诲姩鍒版牴鐨 test3 鐩綍涓
mv llog1.txt log2.txt log3.txt /test3
# 灏嗘枃浠 file1 鏀瑰悕涓 file2锛屽鏋 file2 宸茬粡瀛樺湪锛屽垯璇㈤棶鏄惁瑕嗙洊
mv -i log1.txt log2.txt
# 灏嗘枃浠 file1 鏀瑰悕涓 file2锛屽鏋 file2 宸茬粡瀛樺湪锛屼笉浼氳闂洿鎺ヨ鐩栵紝璋ㄦ厧浣跨敤
mv -f log1.txt log2.txt
# 绉诲姩褰撳墠鏂囦欢澶逛笅鐨勬墍鏈夋枃浠跺埌涓婁竴绾х洰褰
mv * ../

cp 鍛戒护

# -i 鎻愮ず
# -r 澶嶅埗鐩綍鍙婄洰褰曞唴鎵鏈夐」鐩
# -a 澶嶅埗鐨勬枃浠朵笌鍘熸枃浠舵椂闂翠竴鏍

# 澶嶅埗 a.txt 鍒 test 鐩綍涓嬶紝淇濇寔鍘熸枃浠舵椂闂达紝濡傛灉鍘熸枃浠跺瓨鍦ㄦ彁绀烘槸鍚﹁鐩栥
cp -ai a.txt test
# 涓 a.txt 寤虹珛涓涓摼鎺ワ紙蹇嵎鏂瑰紡锛
cp -s a.txt link_a.txt

cat 鍛戒护

# 涓娆℃樉绀烘暣涓枃浠
cat filename
# 浠庨敭鐩樺垱寤轰竴涓枃浠
cat > filename
# 灏嗗嚑涓枃浠跺悎骞朵负涓涓枃浠
cat file1 file2 > file

# -b 瀵归潪绌鸿緭鍑鸿鍙
# -n 杈撳嚭鎵鏈夎鍙
# 鎶 log2012.log 鐨勬枃浠跺唴瀹瑰姞涓婅鍙峰悗杈撳叆 log2013.log 杩欎釜鏂囦欢閲
cat -n log2012.log log2013.log
# 鎶 log2012.log 鍜 log2013.log 鐨勬枃浠跺唴瀹瑰姞涓婅鍙凤紙绌虹櫧琛屼笉鍔狅級涔嬪悗灏嗗唴瀹归檮鍔犲埌 log.log 閲
cat -b log2012.log log2013.log log.log

more 鍛戒护

鍛戒护鍙傛暟

+n      浠庣 n 琛屽紑濮嬫樉绀
-n       瀹氫箟灞忓箷澶у皬涓簄琛
+/pattern 鍦ㄦ瘡涓。妗堟樉绀哄墠鎼滃璇ュ瓧涓诧紙pattern锛夛紝鐒跺悗浠庤瀛椾覆鍓嶄袱琛屼箣鍚庡紑濮嬫樉绀
-c       浠庨《閮ㄦ竻灞忥紝鐒跺悗鏄剧ず
-d       鎻愮ず鈥淧ress space to continue锛屸檘鈥 to quit锛堟寜绌烘牸閿户缁紝鎸塹閿鍑猴級鈥濓紝绂佺敤鍝嶉搩鍔熻兘
-l        蹇界暐Ctrl+l锛堟崲椤碉級瀛楃
-p       閫氳繃娓呴櫎绐楀彛鑰屼笉鏄粴灞忔潵瀵规枃浠惰繘琛屾崲椤碉紝涓-c閫夐」鐩镐技
-s       鎶婅繛缁殑澶氫釜绌鸿鏄剧ず涓轰竴琛
-u       鎶婃枃浠跺唴瀹逛腑鐨勪笅鐢荤嚎鍘绘帀

鎿嶄綔鍛戒护

Enter    鍚戜笅 n 琛岋紝闇瑕佸畾涔夈傞粯璁や负 1 琛
Ctrl+F   鍚戜笅婊氬姩涓灞
绌烘牸閿  鍚戜笅婊氬姩涓灞
Ctrl+B  杩斿洖涓婁竴灞
=       杈撳嚭褰撳墠琛岀殑琛屽彿
:f     杈撳嚭鏂囦欢鍚嶅拰褰撳墠琛岀殑琛屽彿
V      璋冪敤vi缂栬緫鍣
!鍛戒护   璋冪敤Shell锛屽苟鎵ц鍛戒护
q       閫鍑簃ore
# 鏄剧ず鏂囦欢涓粠绗3琛岃捣鐨勫唴瀹
more +3 text.txt
# 鍦ㄦ墍鍒楀嚭鏂囦欢鐩綍璇︾粏淇℃伅锛屽熷姪绠¢亾浣挎瘡娆℃樉绀 5 琛
ls -l | more -5

less 鍛戒护

less 涓 more 绫讳技锛屼絾浣跨敤 less 鍙互闅忔剰娴忚鏂囦欢锛岃 more 浠呰兘鍚戝墠绉诲姩锛屽嵈涓嶈兘鍚戝悗绉诲姩锛岃屼笖 less 鍦ㄦ煡鐪嬩箣鍓嶄笉浼氬姞杞芥暣涓枃浠躲

鍛戒护鍙傛暟

-i  蹇界暐鎼滅储鏃剁殑澶у皬鍐
-N  鏄剧ず姣忚鐨勮鍙
-o  <鏂囦欢鍚> 灏唋ess 杈撳嚭鐨勫唴瀹瑰湪鎸囧畾鏂囦欢涓繚瀛樿捣鏉
-s  鏄剧ず杩炵画绌鸿涓轰竴琛
/瀛楃涓诧細鍚戜笅鎼滅储鈥滃瓧绗︿覆鈥濈殑鍔熻兘
?瀛楃涓诧細鍚戜笂鎼滅储鈥滃瓧绗︿覆鈥濈殑鍔熻兘
n锛氶噸澶嶅墠涓涓悳绱紙涓 / 鎴 ? 鏈夊叧锛
N锛氬弽鍚戦噸澶嶅墠涓涓悳绱紙涓 / 鎴 ? 鏈夊叧锛
-x <鏁板瓧> 灏嗏渢ab鈥濋敭鏄剧ず涓鸿瀹氱殑鏁板瓧绌烘牸
b  鍚戝悗缈讳竴椤
d  鍚戝悗缈诲崐椤
h  鏄剧ず甯姪鐣岄潰
Q  閫鍑簂ess 鍛戒护
u  鍚戝墠婊氬姩鍗婇〉
y  鍚戝墠婊氬姩涓琛
绌烘牸閿 婊氬姩涓琛
鍥炶溅閿 婊氬姩涓椤
[pagedown]锛 鍚戜笅缈诲姩涓椤
[pageup]锛   鍚戜笂缈诲姩涓椤
# ps 鏌ョ湅杩涚▼淇℃伅骞堕氳繃 less 鍒嗛〉鏄剧ず
ps -aux | less -N
# 鏌ョ湅澶氫釜鏂囦欢
less 1.log 2.log

find 鍛戒护

find pathname -options [-print -exec -ok ...]

鍛戒护鍙傛暟

pathname: find鍛戒护鎵鏌ユ壘鐨勭洰褰曡矾寰勩備緥濡傜敤.鏉ヨ〃绀哄綋鍓嶇洰褰曪紝鐢/鏉ヨ〃绀虹郴缁熸牴鐩綍銆
-print锛 find鍛戒护灏嗗尮閰嶇殑鏂囦欢杈撳嚭鍒版爣鍑嗚緭鍑恒
-exec锛 find鍛戒护瀵瑰尮閰嶇殑鏂囦欢鎵ц璇ュ弬鏁版墍缁欏嚭鐨剆hell鍛戒护銆傜浉搴斿懡浠ょ殑褰㈠紡涓'command' {  } \;锛屾敞鎰弡   }鍜孿锛涗箣闂寸殑绌烘牸銆
-ok锛 鍜-exec鐨勪綔鐢ㄧ浉鍚岋紝鍙笉杩囦互涓绉嶆洿涓哄畨鍏ㄧ殑妯″紡鏉ユ墽琛岃鍙傛暟鎵缁欏嚭鐨剆hell鍛戒护锛屽湪鎵ц姣忎竴涓懡浠や箣鍓嶏紝閮戒細缁欏嚭鎻愮ず锛岃鐢ㄦ埛鏉ョ‘瀹氭槸鍚︽墽琛屻

鍛戒护閫夐」

-name 鎸夌収鏂囦欢鍚嶆煡鎵炬枃浠
-perm 鎸夋枃浠舵潈闄愭煡鎵炬枃浠
-user 鎸夋枃浠跺睘涓绘煡鎵炬枃浠
-group  鎸夌収鏂囦欢鎵灞炵殑缁勬潵鏌ユ壘鏂囦欢銆
-type  鏌ユ壘鏌愪竴绫诲瀷鐨勬枃浠讹紝璇稿锛
   b - 鍧楄澶囨枃浠
   d - 鐩綍
   c - 瀛楃璁惧鏂囦欢
   l - 绗﹀彿閾炬帴鏂囦欢
   p - 绠¢亾鏂囦欢
   f - 鏅氭枃浠

-size n :[c] 鏌ユ壘鏂囦欢闀垮害涓簄鍧楁枃浠讹紝甯︽湁c鏃惰〃鏂囦欢瀛楄妭澶у皬
-amin n   鏌ユ壘绯荤粺涓渶鍚嶯鍒嗛挓璁块棶鐨勬枃浠
-atime n  鏌ユ壘绯荤粺涓渶鍚巒*24灏忔椂璁块棶鐨勬枃浠
-cmin n   鏌ユ壘绯荤粺涓渶鍚嶯鍒嗛挓琚敼鍙樻枃浠剁姸鎬佺殑鏂囦欢
-ctime n  鏌ユ壘绯荤粺涓渶鍚巒*24灏忔椂琚敼鍙樻枃浠剁姸鎬佺殑鏂囦欢
-mmin n   鏌ユ壘绯荤粺涓渶鍚嶯鍒嗛挓琚敼鍙樻枃浠舵暟鎹殑鏂囦欢
-mtime n  鏌ユ壘绯荤粺涓渶鍚巒*24灏忔椂琚敼鍙樻枃浠舵暟鎹殑鏂囦欢
(鐢ㄥ噺鍙-鏉ラ檺瀹氭洿鏀规椂闂村湪璺濅粖n鏃ヤ互鍐呯殑鏂囦欢锛岃岀敤鍔犲彿+鏉ラ檺瀹氭洿鏀规椂闂村湪璺濅粖n鏃ヤ互鍓嶇殑鏂囦欢銆 )
-maxdepth n 鏈澶ф煡鎵剧洰褰曟繁搴
-prune 閫夐」鏉ユ寚鍑洪渶瑕佸拷鐣ョ殑鐩綍銆傚湪浣跨敤-prune閫夐」鏃惰褰撳績锛屽洜涓哄鏋滀綘鍚屾椂浣跨敤浜-depth閫夐」锛岄偅涔-prune閫夐」灏变細琚玣ind鍛戒护蹇界暐
-newer 濡傛灉甯屾湜鏌ユ壘鏇存敼鏃堕棿姣旀煇涓枃浠舵柊浣嗘瘮鍙︿竴涓枃浠舵棫鐨勬墍鏈夋枃浠讹紝鍙互浣跨敤-newer閫夐」
# 鏌ユ壘 48 灏忔椂鍐呬慨鏀硅繃鐨勬枃浠
find -atime -2
# 鍦ㄥ綋鍓嶇洰褰曟煡鎵 浠 .log 缁撳熬鐨勬枃浠躲 . 浠h〃褰撳墠鐩綍
find ./ -name '*.log'
# 鏌ユ壘 /opt 鐩綍涓 鏉冮檺涓 777 鐨勬枃浠
find /opt -perm 777
# 鏌ユ壘澶т簬 1K 鐨勬枃浠
find -size +1000c
# 鏌ユ壘绛変簬 1000 瀛楃鐨勬枃浠
find -size 1000c
# 鍦ㄥ綋鍓嶇洰褰曚腑鏌ユ壘鏇存敼鏃堕棿鍦10鏃ヤ互鍓嶇殑鏂囦欢骞跺垹闄ゅ畠浠(鏃犳彁閱掞級
find . -type f -mtime +10 -exec rm -f {} \;
# 褰撳墠鐩綍涓嬫煡鎵炬枃浠跺悕浠 passwd 寮澶达紝鍐呭鍖呭惈 "pkg" 瀛楃鐨勬枃浠
find . -f -name 'passwd*' -exec grep "pkg" {} \;
# 鐢 exec 閫夐」鎵ц cp 鍛戒护
find . -name '*.log' -exec cp {} test3 \;
# 鍒犻櫎lib鐩綍涓嬮潪test寮澶村拰闈瀗ode_modules鐨勫叾浠栨墍鏈夋枃浠
find /var/lib/ -mindepth 1 -maxdepth 1 ! -name 'test*' ! -name 'node_modules' -exec rm -rf {} \;

chmod 鍛戒护

甯哥敤鍙傛暟

-c 褰撳彂鐢熸敼鍙樻椂锛屾姤鍛婂鐞嗕俊鎭
-R 澶勭悊鎸囧畾鐩綍浠ュ強鍏跺瓙鐩綍涓嬫墍鏈夋枃浠

鏉冮檺鑼冨洿

u 锛氱洰褰曟垨鑰呮枃浠剁殑褰撳墠鐨勭敤鎴
g 锛氱洰褰曟垨鑰呮枃浠剁殑褰撳墠鐨勭兢缁
o 锛氶櫎浜嗙洰褰曟垨鑰呮枃浠剁殑褰撳墠鐢ㄦ埛鎴栫兢缁勪箣澶栫殑鐢ㄦ埛鎴栬呯兢缁
a 锛氭墍鏈夌殑鐢ㄦ埛鍙婄兢缁

鏉冮檺浠e彿

r 锛氳鏉冮檺锛岀敤鏁板瓧4琛ㄧず
w 锛氬啓鏉冮檺锛岀敤鏁板瓧2琛ㄧず
x 锛氭墽琛屾潈闄愶紝鐢ㄦ暟瀛1琛ㄧず
- 锛氬垹闄ゆ潈闄愶紝鐢ㄦ暟瀛0琛ㄧず
s 锛氱壒娈婃潈闄
# 澧炲姞鏂囦欢 t.log 鎵鏈夌敤鎴峰彲鎵ц鏉冮檺
chmod a+x t.log
# 鎾ら攢鍘熸潵鎵鏈夌殑鏉冮檺锛岀劧鍚庝娇鎷ユ湁鑰呭叿鏈夊彲璇绘潈闄,骞惰緭鍑哄鐞嗕俊鎭
chmod u=r t.log -c
# 缁 file 鐨勫睘涓诲垎閰嶈銆佸啓銆佹墽琛(7)鐨勬潈闄愶紝缁檉ile鐨勬墍鍦ㄧ粍鍒嗛厤璇汇佹墽琛(5)鐨勬潈闄愶紝缁欏叾浠栫敤鎴峰垎閰嶆墽琛(1)鐨勬潈闄
chmod 751 t.log -c锛堟垨鑰咃細chmod u=rwx,g=rx,o=x t.log -c)
# 灏 test 鐩綍鍙婂叾瀛愮洰褰曟墍鏈夋枃浠舵坊鍔犲彲璇绘潈闄
chmod u+r,g+r,o+r -R text/ -c

tar 鍛戒护

甯哥敤鍙傛暟

-c 寤虹珛鏂扮殑鍘嬬缉鏂囦欢
-f 鎸囧畾鍘嬬缉鏂囦欢
-r 娣诲姞鏂囦欢鍒板凡缁忓帇缂╂枃浠跺寘涓
-u 娣诲姞鏀逛簡鍜岀幇鏈夌殑鏂囦欢鍒板帇缂╁寘涓
-x 浠庡帇缂╁寘涓娊鍙栨枃浠
-t 鏄剧ず鍘嬬缉鏂囦欢涓殑鍐呭
-z 鏀寔gzip鍘嬬缉
-j 鏀寔bzip2鍘嬬缉
-Z 鏀寔compress瑙e帇鏂囦欢
-v 鏄剧ず鎿嶄綔杩囩▼
# 灏嗘枃浠跺叏閮ㄦ墦鍖呮垚 tar 鍖
tar -cvf log.tar 1.log,2.log 鎴杢ar -cvf log.*
# 灏 /etc 涓嬬殑鎵鏈夋枃浠跺強鐩綍鎵撳寘鍒版寚瀹氱洰褰曪紝骞朵娇鐢 gz 鍘嬬缉
tar -zcvf /tmp/etc.tar.gz /etc
# 鏌ョ湅鍒氭墦鍖呯殑鏂囦欢鍐呭
tar -ztvf /tmp/etc.tar.gz
# 鍘嬬缉鎵撳寘 /home, /etc 锛屼絾涓嶈 /home/dmtsai
tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc