目錄
1 查看文件相關命令
1.1 常用命令
1.2 硬鏈接和軟鏈接
2 文件搜索相關命令
2.1?查找文件命令
2.2 查找文件內(nèi)容命令
2.3 其他相關命令
3 文件處理相關命令
3.1?cut?
3.2 sed 過濾
3.3 awk 匹配
4 解壓縮相關命令
4.1?解壓縮文件的意義
4.2?解壓縮相關命令
1 查看文件相關命令
1.1 常用命令
cat - concatenate files and print on the standard output
tac - concatenate and print files in reverse 逆序
more — file perusal filter for crt viewing 不可以回去,就是不可以向前,只能向后,按回車鍵
less - opposite of more less可以上下翻動 less不必讀整個文件,加載速度會比more更快。按上下或jk,按右結束
head - output the first part of files 默認顯示前10行
cat file
tac file
more file
less file
head file
1.2 硬鏈接和軟鏈接
Linux中有兩種類型的鏈接: ?
硬鏈接是利用Linux中為每個文件分配的物理編號——inode建立鏈接。因此,硬鏈接不能跨越文件系統(tǒng)。
linux@linux:~$ ls -i file
1448347 file
軟鏈接(符號鏈接)是利用文件的路徑名建立鏈接。通常建立軟鏈接使用絕對路徑而不是相對路徑,以最大限度增加可移植性。
ln命令:
ln [ -s ] target link_name
選項“-s”表示為創(chuàng)建軟鏈接。在缺省情況下,創(chuàng)建硬鏈接。參數(shù)target為目標文件,link_name為鏈接文件名。如果鏈接文件名已經(jīng)存在但不是目錄,將不做鏈接
linux@ubuntu:~$ ln -s /proc/cpuinfo mycpuinfo
linux@ubuntu:~$ ls –l mycpuinfo
lrwxrwxrwx 1 wdl wdl 13 2007-09-22 00:43 mycpuinfo -> /proc/cpuinfo
以上命令為/proc/cpuinfo文件創(chuàng)建了一個軟鏈接文件。使用“l(fā)s –l”命令可以查看到新創(chuàng)建的鏈接文件所指向的目標文件名。
硬鏈接示例
linux@linux:~$ ls -i file
1448347 file
linux@linux:~$ ln file file_hard
linux@linux:~$ ls -i file_hard
1448347 file
軟鏈接示例
linux@linux:~$ ls -i file
1448347 file
linux@linux:~$ ls -i file
1448347 file
linux@linux:~$ ln -s file file_soft
linux@linux:~$ ls -i file_soft
1448348 file_soft
硬鏈接和軟鏈接的區(qū)別:
- 如果是修改硬鏈接的目標文件名,鏈接依然有效;
- 如果修改軟鏈接的目標文件名, 則鏈接將斷開;
- 對一個已存在的鏈接文件執(zhí)行移動或刪除操作,有可能導致鏈接的斷開。 ?
- 假如刪除目標文件后,重新創(chuàng)建一個同名文件,軟鏈接將恢復,硬鏈接不再有效,因為文件的inode已經(jīng)改變。
2 文件搜索相關命令
2.1?查找文件命令
?find - search for files in a directory hierarchy ?語法:find [起始目錄] 尋找條件 操作
-name '字串' 查找文件名匹配所給字串的所有文件,字串內(nèi)可用通配符 *、?、[ ]。
-iname '字串' 忽略大小寫的方式查找
-type x 查找類型為 x 的文件
-exec 命令名稱 {} 對符合條件的文件執(zhí)行所給的Linux 命令,而不詢問用戶是否需要執(zhí)行該命令。{}表示命令的參數(shù)即為所找到的文件;命令的末尾必須以“ \;”結束。
示例
find . -name "file2" //.代表當前目錄下找
fine /home -name "file2" //在home目錄下找
find . -iname "file2" //忽略大小寫
find . -type f //f代表普通文件
尋找條件可以是一個用邏輯運算符 not、and、or 組成的復合條件。?
and:邏輯與,在命令中用 -a 表示,是系統(tǒng)缺省的選項,表示只有當所給的條 件都滿足時,尋找條件才算滿足
find . -name 'tmp' -type c -user 'inin'
or:邏輯或,在命令中用 -o 表示。該運算符表示只要所給的條件中有一個滿足 時,尋找條件就算滿足OPTIONS:描述命令所有的參數(shù)或選項
find . -name 'tmp' -o -name 'mina*'
not:邏輯非,在命令中用 ! 表示。該運算符表示查找不滿足所給條件的文件
find . ! -name 'tmp'
示例
find . -name "file" -type l //當前目錄查找名稱file,文件類型是鏈接文件
find . -name "file" -a -type l //也可以加-a 與上面相同
find . ! -name "file" -exec rm {} \; //找到名稱不是file的文件,并且執(zhí)行刪除,exec代表刪除{} \;固定用法加最后
2.2 查找文件內(nèi)容命令
grep, egrep, fgrep, rgrep - print lines that match patterns
語法:grep [選項] 要查找的內(nèi)容 查找的位置
常用選項:
-A 除了顯示符合范本樣式的那一列之外,并顯示該行之后的內(nèi)容。
-B 除了顯示符合樣式的那一行之外,并顯示該行之前的內(nèi)容。
-C 除了顯示符合樣式的那一行之外,并顯示該行之前后的內(nèi)容。
-i 忽略字符大小寫的差別。
-r 查找位置是目錄時用
-n 在顯示符合樣式的那一行之前,標示出該行的列數(shù)編號。
示例
linux@linux:~$ cat file2
hello
World
11
22
33
linux@linux:~$ grep -A 1 "hello" file2
hello
World
linux@linux:~$ grep -A 2 "hello" file2
hello
World
11
linux@linux:~$ grep -B 2 "22" file2
World
11
22
linux@linux:~$ grep -C 1 "22" file2
11
22
33
linux@linux:~$ grep -i -C 1 "World" file2
hello
World
11
linux@linux:~$ grep -in -C 1 "World" file2
1-hello
2:World
3-11
linux@linux:~$ grep -irn -C 1 "World" . //查看所有目錄下 有world單詞的內(nèi)容
./file2:1-hello
./file2:2World
./file2:3-11
linux@linux:~$ grep -irn -C 1 "World" /home/linux
2.3 其他相關命令
which - locate a command 定為命令
whereis - locate the binary, source, and manual page files for a command? 定位命令和操作手冊
linux@linux:~$ which ls
/bin/ls
linux@linux:~$ which pwd
/bin/pwd
linux@linux:~$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
linux@linux:~$ whereis pwd
pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
3 文件處理相關命令
3.1?cut?
cut - remove sections from each line of files??從每行文件中刪除部分
-b: 以字節(jié)為單位取出固定字符區(qū)間
-c :以字符 的單位取出固定字符區(qū)間;
-d :后面接分隔字符。與 -f 一起使用;
-f :依據(jù) -d 的分隔字符將一段信息分割成為數(shù)段,用 -f 取出第幾段的意思;
?示例
3.2 sed 過濾
sed - stream editor for filtering and transforming text? ?流編輯器過濾和轉(zhuǎn)換文本
-r:使用擴展正則表達式
-e:它告訴sed將下一個參數(shù)解釋為一個sed指令,只有當命令行上給出多個sed指令時才需要使用-e選項
-i:直接對內(nèi)容進行修改,不加-i時默認只是預覽,不會對文件做實際修改
-n:取消默認輸出,sed默認會輸出所有文本內(nèi)容,使用-n參數(shù)后只顯示處理過的行
編輯命令
a: 追加
i: 插入
d: 刪除
s: 替換
p: 打印
?示例:
linux@linux:~/Desktop$ sed -n '1p' passwd //打印1行
root:x:0:0:root:/root:/bin/bash
linux@linux:~/Desktop$ sed -n '1,4p' passwd //打印passwd中1到4行
linux@linux:~/Desktop$ sed '1d' passwd //預覽刪除passwd文件中第一行
linux@linux:~/Desktop$ sed -i '1d' passwd //刪除passwd文件中第一行
linux@linux:~/Desktop$ sed -i '1,3d' passwd //刪除passwd文件中第一行至第三行
linux@linux:~/Desktop$ sed -i '$ahi' passwd //在passwd文件中最后一行追加一個hi
linux@linux:~/Desktop$ sed -i '1ahi' passwd //在passwd文件中第一行追加一個hi
linux@linux:~/Desktop$ sed -i '1ihi' passwd //在passwd文件中第一行插入一個hi
linux@linux:~/Desktop$ sed -i 's/linux/Linux/' passwd //在passwd中替換一次linux->Linux
linux@linux:~/Desktop$ sed -i 's/linux/Linux/g' passwd //在passwd中替換所有l(wèi)inux->Linux
linux@linux:~/Desktop$ sed -i '20,34s/linux/Linux/g' passwd //在passwd的24-34行中替換所有l(wèi)inux->Linux
3.3 awk 匹配
?awk '{[pattern] action}' {filenames} ? # 行匹配語句 awk '' 只能用單引號
示例:?
linux@linux:~/Desktop$ awk '{print $0}' file //打印全部
linux@linux:~/Desktop$ awk '{print $1}' file //打印file文件中第一列
linux@linux:~/Desktop$ awk '{print $1,$2}' file //打印file文件中第一列,第二列
linux@linux:~/Desktop$ awk '{print NR,$1}' file //打印file文件中第一列,包括行號
linux@linux:~/Desktop$ awk '{print NF,$1}' file //打印file文件中第一列,包括列數(shù)
linux@linux:~/Desktop$ awk '{print $NF}' file //打印file文件中最后一列數(shù)據(jù)
?注意awk默認是空格進行分割的,如果要指定別的符號分割采取如下命令
awk -F ':' '{print $1}' passwd //打印passwd中以:為分隔符,第1列數(shù)據(jù)
運算:
==? > <? =賦值
awk -F ':' '$1=="linux" {print $0}' passwd //判斷第一列數(shù)據(jù)是否等于linux,如果是都打印出來
4 解壓縮相關命令
4.1?解壓縮文件的意義
歸檔文件是將一組文件或目錄保存在一個文件中。 ?壓縮文件也是將一組文件或目錄保存一個文件中,并按照某種存儲格式保存在磁盤上,所占磁盤空間比其中所有文件總和要少。 ?歸檔文件仍是沒有經(jīng)過壓縮的,它所使用的磁盤空間仍等于其所有文件的總和。因而,用戶可以將歸檔文件再進行壓縮,使其容量更小。
用戶在進行數(shù)據(jù)備份時,需要把若干文件整合為一個文件以便保存。盡管整合為一個文件進行管理,但文件大小仍然沒變。若需要網(wǎng)絡傳輸文件時,就希望將其壓縮成較小的文件,以節(jié)省在網(wǎng)絡傳輸?shù)臅r間。因此本節(jié)介紹文件的歸檔與壓縮。
4.2?解壓縮相關命令
gzip 和 gunzip
將一個文件壓縮成.gz格式的壓縮文件,移植性好 ?
缺點:這個命令沒有歸檔功能,每次只能壓縮一個文件。生成相應名字的壓縮包
linux@DESKTOP-6C99IPF:~/share$ ls
test
linux@DESKTOP-6C99IPF:~/share$ gzip test
linux@DESKTOP-6C99IPF:~/share$ ls
test.gz
linux@DESKTOP-6C99IPF:~/share$ gunzip test test.gz
linux@DESKTOP-6C99IPF:~/share$ ls
test
linux@DESKTOP-6C99IPF:~/share$
bzip2和bunzip2
bzip2命令是.bz2文件的壓縮程序 bzip2在性能上優(yōu)于gzip,提供了最大限度的壓縮比率。
linux@DESKTOP-6C99IPF:~/share$ ls
test test1 test2
linux@DESKTOP-6C99IPF:~/share$ bzip2 *
linux@DESKTOP-6C99IPF:~/share$ ls
test.bz2 test1.bz2 test2.bz2
linux@DESKTOP-6C99IPF:~/share$ bunzip2 *
linux@DESKTOP-6C99IPF:~/share$ ls
test test1 test2
zip和unzip
壓縮后的文件后綴名為 .zip,可以壓縮目錄,原文件保留。 ?方便在Linux和微軟Windows間交換文件
linux@DESKTOP-6C99IPF:~/share$ ls
hello test test1 test2
linux@DESKTOP-6C99IPF:~/share$ zip test.zip *
linux@DESKTOP-6C99IPF:~/share$ ls
hello test test.zip test1 test2
linux@DESKTOP-6C99IPF:~/share$ rm hello test test1 test2 -r
linux@DESKTOP-6C99IPF:~/share$ ls
test.zip
linux@DESKTOP-6C99IPF:~/share$ unzip test.zip
linux@DESKTOP-6C99IPF:~/share$ ls
hello test test.zip test1 test2
tar
tar命令主要用于將若干文件或目錄合并為一個文件,以便備份和壓縮。當然,之后出現(xiàn)tar程序的改進版本,可以實現(xiàn)在合并歸檔的同時進行壓縮。? ? ? ? ??文章來源:http://www.zghlxwxcb.cn/news/detail-693336.html
tar命令符號模式的一般語法格式為:
tar [ -t | -x | -r | -c | -v | -f | -j | -z ] tarfile filelist
-t 顯示歸檔文件的內(nèi)容
-x 釋放歸檔文件
-c 創(chuàng)建一個新的歸檔文件
-r 追加的方式歸檔文件
-v 顯示歸檔和釋放的過程信息
-f 用戶指定歸檔文件的文件名,否則使用默認名稱
-j 由tar生成歸檔,然后由bzip2壓縮
-z 由tar生成歸檔,然后由gzip壓縮
示例?文章來源地址http://www.zghlxwxcb.cn/news/detail-693336.html
linux@ubuntu:~$ tar –cf myExamples.tar myExamples
linux@ubuntu:~$ tar -cjf myExamples.tar.bz2 myExamples
linux@ubuntu:~$ tar -czf myExamples.tar.gz myExamples
linux@ubuntu:~$ ls -lh myExamples.tar*
-rw-r--r-- 1 wdl wdl 9.3M 2007-09-23 00:42 myExamples.tar
-rw-r--r-- 1 wdl wdl 8.6M 2007-09-23 00:43 myExamples.tar.bz2
-rw-r--r-- 1 wdl wdl 8.5M 2007-09-23 00:44 myExamples.tar.gz
linux@ubuntu~$ tar –tvf myExamples.tar.gz
linux@ubuntu:~$ tar –xvf myExamples.tar
linux@ubuntu:~$ tar -xvjf myExamples.tar.bz2
linux@ubuntu:~$ tar -xvzf myExamples.tar.gz
linux@linux:~/Desktop$ tar -cf file.tar file1 file2 file3
linux@linux:~/Desktop$ ls
file1 file2 file3 file.tar
linux@linux:~/Desktop$ rm file1 file2 file3
linux@linux:~/Desktop$ ls
file.tar
linux@linux:~/Desktop$ tar -xvf file.tar
file1
file2
file3
linux@linux:~/Desktop$ ls
file1 file2 file3 file.tar
到了這里,關于lv3 嵌入式開發(fā)-3 linux shell命令(文件搜索、文件處理、壓縮)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!