Linux命令

红荼發表於2024-04-01

Linux常用命令

mv:

移動或改名:

移動:檔案改變源路徑就是移動。

[root@localhost ~]# cd /data
[root@localhost data]# ls
2  3  aa  all.tar.gz  bb  cc  cls.tar.gz
[root@localhost data]# cd aa
[root@localhost aa]# 
[root@localhost ~]# cd /data
[root@localhost data]# ls
2  3  aa  all.tar.gz  bb  cc  cls.tar.gz
[root@localhost data]# cd aa
[root@localhost aa]# cd /root
[root@localhost ~]# mv /root/33 /data/aa
[root@localhost ~]# cd /data/aa
[root@localhost aa]# ll
總用量 0
drwxr-xr-x. 3 root root 16 3月  29 13:46 33

改名:檔案源路徑不改變就是改名。

[root@localhost ~]# ll
總用量 12
-rw-r--r--. 1 root root   34 3月   4 09:36 123
drwxr-xr-x. 3 root root   16 3月  29 13:46 13     #
-rw-------. 1 root root 1766 3月   4 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 1797 3月   4 17:19 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 3月   4 17:20 公共
drwxr-xr-x. 2 root root    6 3月   4 17:20 模板
drwxr-xr-x. 2 root root    6 3月   4 17:20 影片
drwxr-xr-x. 2 root root    6 3月   4 17:20 圖片
drwxr-xr-x. 2 root root    6 3月   4 17:20 文件
drwxr-xr-x. 2 root root    6 3月   4 17:20 下載
drwxr-xr-x. 2 root root    6 3月   4 17:20 音樂
drwxr-xr-x. 2 root root    6 3月   4 17:20 桌面
[root@localhost ~]# mv /root/13 /root/33
[root@localhost ~]# ll
總用量 12
-rw-r--r--. 1 root root   34 3月   4 09:36 123
drwxr-xr-x. 3 root root   16 3月  29 13:46 33    #
-rw-------. 1 root root 1766 3月   4 17:16 anaconda-ks.cfg
-rw-r--r--. 1 root root 1797 3月   4 17:19 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 3月   4 17:20 公共
drwxr-xr-x. 2 root root    6 3月   4 17:20 模板
drwxr-xr-x. 2 root root    6 3月   4 17:20 影片
drwxr-xr-x. 2 root root    6 3月   4 17:20 圖片
drwxr-xr-x. 2 root root    6 3月   4 17:20 文件
drwxr-xr-x. 2 root root    6 3月   4 17:20 下載
drwxr-xr-x. 2 root root    6 3月   4 17:20 音樂
drwxr-xr-x. 2 root root    6 3月   4 17:20 桌面

rename

可以批次修改同一字尾的檔案的名字

[root@localhost aa]# touch {1..9}.txt
[root@localhost aa]# ls
1.txt  2.txt  33  3.txt  4.txt  5.txt  6.txt  7.txt  8.txt  9.txt
[root@localhost aa]# rename txt txt.bak *
[root@localhost aa]# ls
1.txt.bak.bak  3.txt.bak.bak  6.txt.bak.bak  9.txt.bak.bak
2.txt.bak.bak  4.txt.bak.bak  7.txt.bak.bak
33             5.txt.bak.bak  8.txt.bak.bak

rm

刪除檔案或目錄,

檔案的查詢

which

查詢外部命令在/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin中查詢

locate

透過資料庫目錄查詢檔案,但是不能實時更新查詢。

-i 不區分大小寫

-n X 只列出前X個匹配專案

find

精確查詢,實時查詢

列:[root@localhost data]# find -name 33
./aa/33
[root@localhost data]# ls
2  3  aa  all.tar.gz  bb  cc  cls.tar.gz

常用可選項

查詢型別 關鍵字 列子 說明
按名稱查詢 -name [root@localhost data]# find / -name 123
/root/123
根據目標檔案的名稱進行查詢,允許使用“*”及“?”萬用字元 -iname "檔名稱" 不區分大小寫
按檔案大小查詢 -size [root@localhost data]# find / -size 30M
/boot/initramfs-3.10.0-693.el7.x86_64.img
/run/media/root/CentOS 7 x86_64/Packages/gimp-help-fr-2.8.2-1.el7.noarch.rpm
/run/media/root/CentOS 7 x86_64/Packages/oxygen-icon-theme-4.10.5-2.el7.noarch.rpm
/usr/lib64/libLLVM-3.9-mesa.so
根據目標檔案的大小進行查詢一般使用“+”、“-”號設定超過或小於指定的大小作為查詢條件常用的容量單位包括 kB(注意 k 是小寫)、MB、GB
按檔案屬主查詢 -user [root@localhost data]# find / -user asdjkl
/home/asdjkl
/home/asdjkl/.mozilla
/home/asdjkl/.mozilla/extensions
/home/asdjkl/.mozilla/plugins
/home/asdjkl/.bash_logout
/home/asdjkl/.bash_profile
/home/asdjkl/.bashrc
根據檔案是否屬於目標使用者進行查詢
按檔案型別查詢 -type [root@localhost data]# find / -type f -name *log
/proc/sys/kernel/compat-log
/proc/sys/net/core/netdev_max_backlog
/proc/sys/net/ipv4/tcp_max_syn_backlog
/proc/1/net/netfilter/nf_log
/proc/2/net/netfilter/nf_log
/proc/3/net/netfilter/nf_log
/proc/5/net/netfilter/nf_log
根據檔案的型別進行查詢檔案型別包括普通檔案(f)、目錄(d)、塊裝置檔案(b)、字元裝置檔案(c)等
按inode號 -inum [root@localhost data]# ls -i
100853269 2 100853091 all.tar.gz 100853092 cls.tar.gz
100853272 3 68193028 bb
33584932 aa 100853057 cc
[root@localhost data]# find -inum 100853057
./cc
根據檔案inode號查詢
按許可權查詢 -perm 按檔案許可權查詢
最大搜尋目錄深度 -maxdepth level 將你的檔案已分級的形式查詢
最小搜尋目錄深度 -mindepth level 同上 find /etc -maxdepth 2 -mindepth 2

cat

檢視檔案內容

選項 效果
-n 顯示行號包括空行
-b 跳過空白行編號
-s 將所有的連續的多個空行替換為一個空行(壓縮成一個空行)
-A 顯示隱藏字元

檢視檔案的前面部分,預設檢視檔案前十行

head -n

tail

檢視檔案或標準輸入的倒數行

tail -n

tail -f 實時檢視檔案後十行(用於檢視日誌)

相關文章