Linux學習之路3 2檔案操作(二)

2021-10-08 15:42:36 字數 4870 閱讀 9239

一、檢視檔案內容

(1)cat 命令:一般檢視小檔案,從第一行到最後一行列出來

常見選項:

-n:顯示行號

-a:顯示控制字元,如換行符、製表符等(linux和wi

ndow

sm

和windows ^m

和windo

wsm)

(2)tac 命令:一般檢視小檔案,從最後一行到第一行列出來

(3)more和less命令:一般檢視大檔案,q退出檢視,可以搜尋,建議使用less命令

(4)head命令:預設檢視檔案前10行

head -n 15或head -15表示檢視前15行

(5)tail命令:預設檢視檔案後10行

tail -n 15或tail -15表示檢視後15行,-f表示動態檢視

(6)ldd命令:一般用來檢視二進位制的命令檔案

#檢視/etc/passwd檔案內容

[root@node01 ~]

# cat /etc/passwd

#檢視/etc/passwd檔案內容,並列印行號

[root@node01 ~]

# cat -n /etc/passwd

#逆序檢視/etc/passwd檔案內容

[root@node01 ~]

# tac /etc/passwd

#檢視/etc/passwd檔案的前5行內容

[root@node01 ~]

# head -5 /etc/passwd

#檢視/etc/passwd檔案的後5行內容

[root@node01 ~]

# tail -5 /etc/passwd

[root@node01 ~]

# more /var/log/messages

[root@node01 ~]

# less /var/log/messages

#檢視mkdir命令檔案(二進位制)內容

[root@node01 ~]

# ldd /bin/mkdir

linux-vdso.so.1 =

>

(0x00007ffe37b9a000)

libselinux.so.1 =

> /lib64/libselinux.so.1 (0x00007f8732623000)

libc.so.6 =

> /lib64/libc.so.6 (0x00007f8732260000)

libpcre.so.1 =

> /lib64/libpcre.so.1 (0x00007f8731ffd000)

libdl.so.2 =

> /lib64/libdl.so.2 (0x00007f8731df9000)

/lib64/ld-linux-x86-64.so.2 (0x0000562c7afe5000)

libpthread.so.0 =

> /lib64/libpthread.so.0 (0x00007f8731bdd000)

二、拷貝檔案(cp)

注意:本地檔案拷貝

#常用選項

#-a 遞迴拷貝檔案,包括目錄檔案及檔案屬性資訊

#-r 拷貝目錄

#-p 拷貝檔案包含檔案的屬性資訊

#-v 顯示拷貝過程資訊

#用法#cp [選項] 需要拷貝的檔案 拷貝到**去

cp /root/file /home #拷貝/root 下的file1檔案到/home目錄下

cp -r /home/panda /root #拷貝/home/panda目錄到/root下

#-a與-p的區別

#相同點都是需要拷貝檔案的屬性資訊,比如擁有者(誰建立的等);不同點在於,-p只能拷貝檔案,-a既可以拷貝檔案也可以拷貝目錄

三、移動或重新命名檔案(mv)

#移動檔案用法(不同路徑下)

mv 需要移動的檔案 移動到新的路徑下

#注:檔案的路徑不一樣

#重新命名用法(相同路徑下)

mv 原檔名 新檔名

#注:老檔案和新檔案的路徑一樣

四、刪除檔案(rm)

#常用選項

#-r 遞迴刪除,一般用於刪除目錄

#-r 直接刪除,不提示

#注:使用時請自問:我是誰?我在哪?我在操作誰?

rm file1 #刪除當前目錄下的file1檔案,有提示

rm -r dir1 #刪除當前目錄下的dir1目錄,有提示

rm -f /root/file1 #強制刪除/root/file1檔案,無提示,直接刪除

五、查詢

(一)命令查詢

#which 命令:找出命令的絕對路徑

#whereis 命令:找出命令的路徑以及文件手冊資訊

[root@node01 home]

# which mkdir

/usr/bin/mkdir

[root@node01 home]

# whereis mkdir

mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz

(二)檔案查詢(find)

find命令:精確查詢,磁碟搜尋,io讀寫,cpu開銷大

用法一:找出來輸出到螢幕

find 查詢路徑 選項 關鍵字

常見選項	含義	備註

-name 按照檔名查詢檔案

-iname 按照檔名忽略大小寫查詢

-size 按照檔案大小查詢 +1m大於1m -1m小於1m 1m等於1m

-type 按照檔案型別查詢

-mtime 按照檔案修改時間查詢 -n指n天以內,+n指n天以前

-atime 按照檔案訪問時間查詢

-ctime 按照檔案建立時間查詢檔案

-perm 按照檔案許可權查詢檔案

舉例說明:

#0)環境準備

mkdir /test

touch /test/file1

touch /test/file1

useradd user1

touch /home/user1/file

cp -a /home/user1/* /test/

#1)根據檔名查詢

[root@node01 ~]

# find /test -name "file1"

/test/file1

[root@node01 ~]

# find /test -iname "file1"

/test/file1

/test/file1

[root@node01 ~]

# find /etc -name "*.conf"

#2)根據檔案型別查詢

[root@node01 ~]

# find /usr/bin/ -type

[root@node01 ~]

# find /dev -type b

[root@node01 ~]

# cd /test

[root@node01 test]

# find . -type d

[root@node01 test]

# find . -type f

#3)根據檔案大小查詢

[root@node01 test]

# find . -type f -size +1m

[root@node01 test]

# find . -type f -size -1m

[root@node01 test]

# find . -type f -size -1024k

[root@node01 test]

# find . -type f -size 1m

#4)根據檔案屬性(許可權,建立者和所屬組)

[root@node01 test]

# find . -user user1 -group user1 -type f

[root@node01 test]

# find . -type f -perm 644

[root@node01 test]

# find . -type f -mtime +2

[root@node01 test]

# find . -type f -mtime -2

[root@node01 test]

# find . -type f -mtime 2

用法二:找出來執行命令

根據需求查詢出來後執行某個動作(命令)

find 路徑 選項 關鍵字 動作

常見動作 說明

-exec	對查詢到的檔案直接執行該引數後的shell命令

-ok 對查詢到的檔案詢問式執行該引數後的shell命令

-delete 刪除查詢到的檔案

-ls 列出查詢到的檔案,詳細資訊

-print 列印出查詢到的檔案(預設選項)

舉例說明

#語法結構

#注意:

#1、-exec或者-ok後面寫完命令必須以空格反斜槓\;結尾( \;)

#2、{}表示find命令所找出來的內容

Qt學習之路21 檔案操作

io操作的本質就是讀寫一段連續的儲存空間。qfile file qstring c users song8023 desktop test.txt 定義乙個檔案qfile類物件時需要指定路徑和名字,if file.open qiodevice writeonly qiodevice text 以只寫...

Win32檔案操作

開啟檔案的示例 handle hsrcfile createfile text main.cpp generic read,file share read,null,open existing,file attribute normal,null if hsrcfile invalid handle...

Python學習之路4 檔案操作 編碼轉換

檔案操作 檔案操作大概分三步 把檔案開啟。操作檔案。把檔案關上。開啟檔案 開啟檔案用open 函式,開啟成功後返回乙個資源,具體語法如下。open 要開啟的檔案,開啟方式,開啟檔案的格式,預設為utf 8 例如f open passengers.txt r encoding utf 8 上例用ope...