linuxæ件åç®å½ç®¡ç常ç¨å½ä»¤
linuxå½ä»¤æ¯å¯¹Linuxç³»ç»è¿è¡ç®¡ççå½ä»¤ã对äºLinuxç³»ç»æ¥è¯´ï¼æ 论æ¯ä¸å¤®å¤çå¨ãå åãç£ç驱å¨å¨ãé®çãé¼ æ ï¼è¿æ¯ç¨æ·çé½æ¯æ件ï¼Linuxç³»ç»ç®¡ççå½ä»¤æ¯å®æ£å¸¸è¿è¡çæ ¸å¿ï¼ä¸ä¹åçDOSå½ä»¤ç±»ä¼¼ãlinuxå½ä»¤å¨ç³»ç»ä¸æ两ç§ç±»åï¼å ç½®Shellå½ä»¤åLinuxå½ä»¤ãlinuxå½ä»¤å ä¹æ为ç¨æ·ä½¿ç¨linuxçå ¥å£ï¼ä¼ä½¿ç¨å¸¸ç¨å½ä»¤æ为äºå¦ä¹ å使ç¨linuxçåºç¡ã
cd ç®å½å½ä»¤ cd [-L|[-P [-e]] [[email protected]]] [dir]
cd /home è¿å
¥ â/homeâ ç®å½
cd ⦠è¿åä¸ä¸çº§ç®å½
cd â¦/⦠è¿åä¸ä¸¤çº§ç®å½
cd è¿å
¥å½åç¨æ·ç主ç®å½
cd ~ è¿å
¥å½åç¨æ·ç主ç®å½
cd â è¿å
¥å½åç¨æ·ç主ç®å½
cd ~user1 æå®è¿å
¥user1ç¨æ·ç主ç®å½
cd - è¿åä¸æ¬¡æå¨çç®å½
cdå½ä»¤çä½ç¨å°±æ¯ä»å½åç®å½è·³è½¬å°dirå¤, å¦ædirå¤ç空çè¯å°±ä¼è·³è½¬å°ç¯å¢åéHOMEä¸è®¾ç½®çå¼.
ç¯å¢åéCDPATH
对äºç¯å¢åéCDPATH,å¦æ没æ设置CDPATHçè¯,æ¯æ¬¡cdæ¥æ¾ç跳转ç®å½é½æ¯ä»¥å½åçç®å½ä¸ºç¶ç®å½.
è¥æ设置CDPATHçè¯,æ¯æ¬¡cdæ¥æ¾ç跳转ç®å½å°±æ¯ä»¥è®¾ç½®çç®å½ä¸ºç¶ç®å½æ¥æ¥æ¾.
e.g. å设å½åæç®å½ç»æ: ~/a/b/c è¥æ²¡æ设置CDPATH
[[email protected] ~]# cd a
[[email protected] a]# cd c
-bash: cd: c: No such file or directory
è¥å°CDPATH设为CDPATH=.:/root/a/bçè¯
[[email protected] a]# export CDPATH=.:/root/a/b
[[email protected] a]# cd c
/root/a/b/c
å°å¸¸ç¨ç®å½è®¾ç½®å°CDPATHéå°±è½æææé«cdçæç
CDPATHä¸ç®å½ä¹é´ä½¿ç¨:
éå¼
è¥ä¸å ä¸.
è·¯å¾(å³å½åè·¯å¾çè¯),以åé»è®¤çæ¬ç®å½ä½ä¸ºç¶ç®å½cdå°±ä¸å¯ç¨äº
è¥dirå¤æ¯ä»¥/å¼å¤´(å³æ¯ç»å¯¹è·¯å¾)çè¯,CDPATH就失æäº
cd -P, -eåæ°
è¿å
¥å°å¿«æ·æ¹å¼ç®å½çå®é
ç®å½ä¸. e.g. å设 ~ ç®å½ä¸æa,b两个ç®å½, åæ¶aä¸æä¸ä¸ªè¿æ¥å°bçåç®å½b(è¿ä¸ªbæ¯å¿«æ·æ¹å¼)
[[email protected] a]# cd b
/root/a/b
[[email protected] a]# cd -P b
./b
è¥å°ä¸ä¸ªæ件夹èªå·±çå¿«æ·æ¹å¼æ¾å°æ件夹é,è¿æ ·åèæ¬çæ¶åå°±æå¯è½ä¼åºç°æ é循ç¯,å½åè·¯å¾åå°±ä¼åå¾æ éé¿,ä½æ¯å ä¸äº-På½ä»¤åå°±å¯ä»¥é¿å
æ 线循ç¯çæ
åµ.
å¦æå¨-Påé¢å ä¸-eçè¯,è¥è·³è½¬åè·¯å¾ä¸åå¨å¯¹åºç©çè·¯å¾çè¯(æ¯å¦è¯´å¤¹æçå¤ä¸ªå¿«æ·æ¹å¼ä¸ºå称çè·¯å¾)å°±ä¼è¿åä¸ä¸ªå¤±è´¥çç¶æ.
cd -Låæ°
-Låæ°å-Pç¸å(两è
ä¸è½åæ¶åºç°,å³ -L|-P),æ¯è·³è½¬å°é¾æ¥ç®å½ä¸,类似äºä¸å¸¦åæ°çcd.
[[email protected] a]# cd -L b
/root/a/b
pwd æ¾ç¤ºå·¥ä½è·¯å¾ pwd [-LP]
[[email protected] c]# pwd
/root/a/b/c
pwd -Låæ°
Lï¼logicalï¼å½ç®å½ä¸ºè¿æ¥è·¯å¾æ¶ï¼æ¾ç¤ºè¿æ¥è·¯å¾
pwd -Påæ°
Pï¼physicalï¼æ¾ç¤ºå®é
ç©çè·¯å¾ï¼èé使ç¨è¿æ¥ï¼linkï¼è·¯å¾
[[email protected] java_sdk_1.8.0]# pwd
/etc/alternatives/java_sdk_1.8.0
[[email protected] java_sdk_1.8.0]# pwd -L
/etc/alternatives/java_sdk_1.8.0
[[email protected] java_sdk_1.8.0]# pwd -P
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-1.b14.el7_2.x86_64
lså½ä»¤ ls [OPTION]... [FILE]...
ls æ¥çç®å½ä¸çæ件
ls -F æ¥çç®å½ä¸çæ件
ls -l æ¾ç¤ºæ件åç®å½ç详ç»èµæ
ls -a æ¾ç¤ºéèæ件
ls [0-9] æ¾ç¤ºå
å«æ°åçæ件ååç®å½å
[[email protected] ~]# pwd
/root
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ls
a anaconda-ks.cfg test
[[email protected] ~]# ls -F
a/ anaconda-ks.cfg test/
[[email protected] ~]# ls -f
. .. .pm2
test .bashrc .vimrc
.npm .ssh a
[[email protected] ~]# ls -a
. .npm .pm2
.. a .bashrc
test .ssh .vimrc
treeå½ä»¤
æ¾ç¤ºæ件åç®å½ç±æ ¹ç®å½å¼å§çæ å½¢ç»æ
usage: tree [-acdfghilnpqrstuvxACDFQNSUX] [-H baseHREF] [-T title ] [-L level [-R]]
[-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
[--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
[--filelimit[=]#] [--si] [--timefmt[=]<f>] [<directory list>]
[[email protected] ~]# tree
.
âââ a
â âââ b
â âââ c
âââ anaconda-ks.cfg
âââ test
âââ aaa.txt
4 directories, 2 files
mkdir å建ç®å½å½ä»¤ mkdir [OPTION]... DIRECTORY...
mkdir dir1 å建ä¸ä¸ªå«ådir1
çç®å½
mkdir dir1 dir2 åæ¶å建两个ç®å½
mkdir -p /tmp/dir1/dir2 å建ä¸ä¸ªç®å½æ
Try 'mkdir --help' for more information.
rm 移é¤å½ä»¤ rm [OPTION]... FILE...
rm -f file1 å é¤ä¸ä¸ªå«åfile1
çæ件
rmdir dir1 å é¤ä¸ä¸ªå«ådir1
çç®å½
rm -rf dir1 å é¤ä¸ä¸ªå«ådir1
çç®å½å¹¶åæ¶å é¤å
¶å
容
rm -rf dir1 dir2 åæ¶å é¤ä¸¤ä¸ªç®å½åå®ä»¬çå
容
Try 'rm --help' for more information.
mv 移å¨å½ä»¤
mv dir1 new_dir éå½å/ç§»å¨ ä¸ä¸ªç®å½
Usage: mv [OPTION]... [-T] SOURCE DEST
or: mv [OPTION]... SOURCE... DIRECTORY
or: mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
cp æ·è´å½ä»¤
cp file1 file2 å¤å¶ä¸ä¸ªæ件
cp dir/* . å¤å¶ä¸ä¸ªç®å½ä¸çæææ件å°å½åå·¥ä½ç®å½
cp -a /tmp/dir1 . å¤å¶ä¸ä¸ªç®å½å°å½åå·¥ä½ç®å½
cp -a dir1 dir2 å¤å¶ä¸ä¸ªç®å½
Usage: cp [OPTION]... [-T] SOURCE DEST
or: cp [OPTION]... SOURCE... DIRECTORY
or: cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
ln é¾æ¥å½ä»¤
ln -s file1 lnk1 å建ä¸ä¸ªæåæ件æç®å½ç软é¾æ¥
ln file1 lnk1 å建ä¸ä¸ªæåæ件æç®å½çç©çé¾æ¥
touch å½ä»¤ touch [OPTION]... FILE...
ä¿®æ¹æ件æ¶é´æ³ï¼æè
æ°å»ºä¸ä¸ªä¸åå¨çæ件
-a æâtime=atimeæâtime=accessæâtime=use ãåªæ´æ¹ååæ¶é´
-c æâno-create ãä¸å»ºç«ä»»ä½ææ¡£
-d 使ç¨æå®çæ¥ææ¶é´ï¼èéç°å¨çæ¶é´
-f æ¤åæ°å°å¿½ç¥ä¸äºå¤çï¼ä»
è´è´£è§£å³BSDçæ¬touchæ令çå
¼å®¹æ§é®é¢
-m æâtime=mtimeæâtime=modify ãåªæ´æ¹åå¨æ¶é´
-r ææå®ææ¡£æç®å½çæ¥ææ¶é´ï¼ç»ç»è®¾æååèææ¡£æç®å½çæ¥ææ¶é´ç¸å
-t 使ç¨æå®çæ¥ææ¶é´ï¼èéç°å¨çæ¶é´
eg: touch -t 0712250000 file1
file å½ä»¤
该å½ä»¤ç¨æ¥è¯å«æ件类åï¼ä¹å¯ç¨æ¥è¾¨å«ä¸äºæ件çç¼ç æ ¼å¼ãå®æ¯éè¿æ¥çæ件ç头é¨ä¿¡æ¯æ¥è·åæ件类åï¼èä¸æ¯åWindowséè¿æ©å±åæ¥ç¡®å®æ件类åçã
[[email protected] a]# file aaa.txt
aaa.txt: UTF-8 Unicode text
[[email protected] a]# file -i aaa.txt
aaa.txt: text/plain; charset=utf-8
[[email protected] a]# file -i hello.html
hello.html: text/html; charset=us-ascii
//æ¥ç软è¿æ¥å¯¹åºæ件çæ件类å
[[email protected] a]# ln -s aaa.txt saaa
[[email protected] a]# file saaa
saaa: symbolic link to `aaa.txt'
[[email protected] a]# file -L saaa
saaa: UTF-8 Unicode text
iconv å½ä»¤
ç¨æ¥è½¬æ¢æ件çç¼ç æ¹å¼ï¼æ¯å¦å®å¯ä»¥å°UTF8ç¼ç ç转æ¢æGB18030çç¼ç ï¼åè¿æ¥ä¹è¡ã
-f encoding :æå符ä»encodingç¼ç å¼å§è½¬æ¢ã
-t encoding :æå符转æ¢å°encodingç¼ç ã
-l :ååºå·²ç¥çç¼ç å符éå
-o file :æå®è¾åºæ件
-c :忽ç¥è¾åºçéæ³å符
-s :ç¦æ¢è¦åä¿¡æ¯ï¼ä½ä¸æ¯é误信æ¯
âverbose :æ¾ç¤ºè¿åº¦ä¿¡æ¯
-få-tæè½æå®çåæ³å符å¨-lé项çå½ä»¤éé¢é½ååºæ¥äº
iconv -l ååºå·²ç¥çç¼ç
[[email protected] a]# file hello.html
hello.html: HTML document, ISO-8859 text
[[email protected] a]# iconv -f ISO-8859-1 -t UTF-8 hello.html -o hello2.html
[[email protected] a]# file hello2.html
hello2.html: HTML document, UTF-8 Unicode text
find å½ä»¤ find pathname -options [-print -exec -ok ...]
#æ¥æ¾å½åç®å½ä¸æ件å
容ä¸å«æ html åè¯çå
容
[[email protected] a]# find . -type f | xargs grep html
./hello.html:<!DOCTYPE html>
./hello.html:<html>
./hello.html:<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
./hello.html:</html>
./convert1.html:<html>
./convert1.html:</html>
./hello2.html:<!DOCTYPE html>
./hello2.html:<html>
./hello2.html:<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
./hello2.html:</html>
find å½ä»¤åè½å¼ºå¤§ï¼ä½¿ç¨å¤åï¼éä¸ä¸ä¸ªè¿æ¥ï¼è®²çé常好ãfindå½ä»¤æ¥æ¾å å«æå®å 容çæ件
cat å½ä»¤ cat [OPTION]... [FILE]...
-næ-numberï¼æ1å¼å§å¯¹ææè¾åºçè¡æ°ç¼å·ï¼
-bæânumber-nonblankï¼å-nç¸ä¼¼ï¼åªä¸è¿å¯¹äºç©ºç½è¡ä¸ç¼å·ï¼
-sæâsqueeze-blankï¼å½éå°æè¿ç»ä¸¤è¡ä»¥ä¸ç空ç½è¡ï¼å°±ä»£æ¢ä¸ºä¸è¡ç空ç½è¡ï¼
-Aï¼æ¾ç¤ºä¸å¯æå°å符ï¼è¡å°¾æ¾ç¤ºâ$âï¼
-eï¼çä»·äº"-vE"é项ï¼
-tï¼çä»·äº"-vT"é项ï¼
[[email protected] a]# touch f1 f2
[[email protected] a]# echo "hello" > f1
[[email protected] a]# echo "world" > f2
[[email protected] a]# cat f1
hello
[[email protected] a]# cat f1 f2 #åæ¶æ¾ç¤ºæ件flåf2çå
容
hello
world
[[email protected] a]# cat f1 f2 > f3 #å°æ件flåf2å并åæ¾å
¥æ件f3ä¸
[[email protected] a]# cat f3
hello
world
ä¸è¡å½ä»¤å®ç°ä»£ç è¡æ°ç»è®¡
1. åºæ¬ç
# find . -name "*.cpp"|xargs wc -l|grep "total"|awk '{print $1}'
# find . -name "*.cpp"|xargs cat|wc -l
2. å»ç©ºè¡ç
# find . -name "*.java"|xargs cat|grep -v ^$|wc -l
ä¸é¢ç^$æ¯æ£å表达å¼ï¼^æ¯ä»¥å¹é
å¼å¤´ï¼$æ¯å¹é
ç»å°¾ï¼æ以^$æ¯å¹é
ä¸ä¸ªç©ºè¡ã
3. å»æ³¨éç
# find . -name "*.java"|xargs cat|grep -v -e ^$ -e ^\s*\/\/.*$|wc -l
ä¸é¢ç^\s*\/\/.*$æ¯æ£å表达å¼ï¼å
¶ä¸^\s*表æä»¥ç©ºæ ¼(å¤ä¸ª)å¼å¤´ï¼\/\/表ææ¯//å¼å¤´ç注éï¼â.*$â表æå¯è½ä»¥â.âç»å°¾
å»æ³¨éçåªæ¯å»æ以//å¼å¤´ç注éï¼å¯¹äºä»¥/* */å
å«ç注é并没æå»æã
ä¸é¢æ¯åªè¯å«.cppæ件ï¼å¦ææ³ä¸æ¬¡ç»è®¡å¤ç§ç±»åçæ件ï¼æä¹åå¢ï¼
4. ç»è®¡æ¬æ件夹ä¸.hï¼.cppï¼.shä¸ç§ç±»åçæ件代ç è¡æ°
æ¹å¼ä¸ï¼
# find -name "*[.h|.cpp|.sh]" -type f | xargs cat | wc -l
æ¹å¼äºï¼
# find -name "*[.h|.cpp|.sh]" -type f -exec cat {} \; | wc -l
æ¹å¼ä¸ï¼
# find -regex ".*\.h\|.*\.cpp\|.*\.sh" | xargs cat |wc -l
å
¶å®ä¸é¢ä¸ç§æ¹æ³çåºå«å°±æ¯findå°ææçç®æ æ件çæ¹æ³ï¼è¿æä¸é¢ä¸¤ç§æ¹æ³å¯ä»¥æ¾å°æ³è¦çç»æã
æ¹æ³åï¼
# find -name "*[.h|.cpp|.sh]" ! -name "."
ä¸é¢!å·ååé¨åæ¾åºä¼å
å«"."è¿ä¸ªç®å½ï¼ç¨"!"æé¤è¿ä¸ªç®å½ï¼å½ç¶äºï¼è¿ä¸ªåä¸é¢æ¹å¼ä¸ï¼äºä¸ç-type fæ¯ç¸è¿ä¼¼çæ¹æ³ï¼-type fæ¯æå®ç®æ ç±»å为æ件ã
æ¹æ³äºï¼æ笨çæ¹æ³ï¼
# find -name "*.h" -o -name "*.sh" -o -name "*.cpp"
æ¤å¤ï¼å¦æå½åç®å½ä¸å
å«åç®å½ï¼åä¸å¹¶ä¼è¢«findåºæ¥ï¼å¦æä¸æ³findåç®å½ï¼åå¯ä»¥ç¨-maxdepthé项ï¼
# find -maxdepth 1 -name "*[.h|.cpp|.sh]" -type f
上一篇: Linux常用命令——文件目录