Linux ls查看目录文件命令集锦
? ? ? ? ? ? ? ? ? ? ? ? ? ?Linux Shell ls查看目錄匯總
聲明與簡介
本文的數據來自網絡,部分代碼也有所參照,這里做了注釋和延伸,旨在技術交流,如有冒犯之處請聯系博主及時處理。本文主要介紹Linux的命令ls的使用。
ls命令用于查看當前操作系統下的目錄、文件信息。
LS常見命令集錦
查看指定目錄的信息
#查看指定目錄,默認是當前目錄 ls /data/mysql/ ls ls .以區分目錄、文件形式查看
#以區分目錄、文件方式 ls –F /data/mysql注: 1 這里文件以后綴 “/”顯示,可執行文件以”*”
? ? ? ? 2 這里的F約等價于字母Format
以長信息展示目錄
#以長信息,詳細信息方式展示 ls -l主要展示以下信息:
縮略方式查看所有文件(含隱藏文件)
隱藏文件多數為配置文件,以.開頭,比如這里的.bash_profile、.bashrc環境變量配置文件;.kettle是KETTLE的配置文件。
# 顯示所有文件(含隱藏) ls –a /root遞歸式遍歷文件
#遞歸式查看文件和目錄 ls –F –R #也可以寫成 ls –FR這里的R參數是以遞歸式查看指定文件信息,即如果有的文件夾有子文件(夾)則會遍歷完全。
這里的參數R等價于recurcive
模糊匹配
#模糊匹配方式查看文件、目錄 ls -l sh.t?t ls -l sh*這里?是一個字符模糊匹配,*則是可以匹配多個字符。
中括號匹配
情景1:中括號多固定字符匹配,比如如下命令會匹配到txt和tgt。
#多字符組合匹配 ls -l sh.t[gx]t情景2:中括號多固定字符匹配,比如如下命令會匹配到txt和tgt。
#以正則表達式方式匹配 ls -l sh.t[a-zA-Z]t情景3:反向匹配,比如不含字符“g”的文件,則可以
#反向匹配 ls -l sh.t[!g]t合并目錄、文件名
#這里合并時以逗號作為分隔符。 ls -m . #shelldir, sh.Tgt, sh.txt引號包裹文件名
#以引號包裹方式展示 ls -Q . #"shelldir"? "sh.Tgt"? "sh.txt"查看目錄時僅顯示用戶及組ID
ls -n . #總用量 8 #drwxr-xr-x 2 0 0 23 5月? 14 16:19 shelldir #-rw-r--r-- 1 0 0? 7 5月? 14 16:26 sh.Tgt #-rw-r--r-- 1 0 0 12 5月? 14 16:16 sh.txt注:這里的0即是root的用戶和組ID??赏ㄟ^如下命令驗證:
#查看root的用戶信息 cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin指定時間格式顯示文件信息
#指定以年月日時分秒方式展示 ls -l --time-style='+%Y/%m/%d %H:%M:%S' .#總用量 8 #drwxr-xr-x 2 root root 23 2021/05/14 16:19:31 shelldir #-rw-r--r-- 1 root root? 7 2021/05/14 16:26:32 sh.Tgt #-rw-r--r-- 1 root root 12 2021/05/14 16:16:55 sh.txt其它幾種內置時間格式參數為full-iso、long-iso、iso、locale,以下以full-iso為例。
其中full-iso格式為年-月-日 時-分-秒.毫秒 時區。比如2021-05-14 16:19:31.574107937 +0800
Long-iso格式為年-月-日 時-分,比如2021-05-14 16:19
Iso格式為月-日 時-分,比如05-14 16:19
locale格式為依賴于系統的語言環境,當前為中文(zh_CN.utf8),所以顯示效果為:5月? 14 16:19。
[root@host_128 shellscrips]# ls -l --time-style=full-iso .
總用量 8
drwxr-xr-x 2 root root 23 2021-05-14 16:19:31.574107937 +0800 shelldir
-rw-r--r-- 1 root root? 7 2021-05-14 16:26:32.840134427 +0800 sh.Tgt
-rw-r--r-- 1 root root 12 2021-05-14 16:16:55.545098126 +0800 sh.txt
這里顯示的是時間格式里含了時區信息,當前CentOS系統是東八區(+0800)。
以文件大小顯示
#以文件大小排序,這里以文件大的排在前。 ls -S -l .通過時間類型查看
這里有三個參數mtime:modification time,即文件內容修改時。
ctime: status change time 即文件的狀態(權限和屬性)變化時
atime : access time 文件被訪問時,比如cat、more、ls等時
注:針對atime的說明:
這里特別注意,不是每次訪問時該時間都會更新,而是文件狀態或內容修改后最近的一次時間。
案例說明:
1 先修改文件sh.txt,查看通過修改時間查看
2 再修改文件的讀寫權限,然后通過狀態時間查看
3 通過cat訪問文件,然后通過訪問時間查看
4 再次cat訪問文件,然后通過訪問時間查看
詳細見下文:
ll
總用量 8
drwxr-xr-x 2 root root 23 5月? 14 16:19 shelldir
-rw-r--r-- 1 root root 60 5月? 14 21:34 sh.Tgt
-rw-r--r-- 1 root root 24 5月? 14 21:48 sh.txt
[root@host_128 shellscrips]# echo "new content" >> sh.txt
[root@host_128 shellscrips]# ls -l sh.txt
-rw-r--r-- 1 root root 36 5月? 14 22:19 sh.txt
[root@host_128 shellscrips]# chmod 744 sh.txt
[root@host_128 shellscrips]# ls -l --time=ctime sh.txt
-rwxr--r-- 1 root root 36 5月? 14 22:20 sh.txt
[root@host_128 shellscrips]# cat sh.txt
say nothing
modify now!
new content
[root@host_128 shellscrips]# ls -l --time=atime sh.txt
-rwxr--r-- 1 root root 36 5月? 14 22:20 sh.txt
[root@host_128 shellscrips]# date
2021年 05月 14日 星期五 22:21:05 CST
[root@host_128 shellscrips]# cat sh.txt
say nothing
modify now!
new content
[root@host_128 shellscrips]# ls -l --time=atime sh.txt
-rwxr--r-- 1 root root 36 5月? 14 22:20 sh.txt
以列的形式展示目錄信息
#這里的C等價于column,ls默認即以列形式展示。 ls –C .以單獨行展示
#每個結果1行,注意這里的參數是1 ls -1 .shelldir
sh.Tgt
sh.txt
以訪問時間展示
#以訪問時間格式展示 ls -u -lt .總用量 8
-rwxr--r-- 1 root root 36 5月? 14 22:20 sh.txt
-rw-r--r-- 1 root root 60 5月? 14 21:35 sh.Tgt
drwxr-xr-x 2 root root 23 5月? 14 16:22 shelldir
[root@host_128 shellscrips]# ls -lt .
總用量 8
-rwxr--r-- 1 root root 36 5月? 14 22:19 sh.txt
-rw-r--r-- 1 root root 60 5月? 14 21:34 sh.Tgt
drwxr-xr-x 2 root root 23 5月? 14 16:19 shelldir
[root@host_128 shellscrips]# stat sh.txt
? 文件:sh.txt
? 大小:36??????? ? 塊:8????????? IO 塊:4096?? 普通文件
設備:fd00h/64768d? Inode:68385119??? 硬鏈接:1
權限:(0744/-rwxr--r--)? Uid:(??? 0/??? root)?? Gid:(??? 0/??? root)
最近訪問:2021-05-14 22:20:41.599276005 +0800
最近更改:2021-05-14 22:19:53.233272963 +0800
最近改動:2021-05-14 22:20:24.664274940 +0800
創建時間:-
以可方便讀方式展示
#以可方便讀方式展示 ls -h -s .總用量 8.0K
?? 0 shelldir? 4.0K sh.Tgt? 4.0K sh.txt
以k為單位顯示
#以k為存儲單位方式展示文件信息 ls -sk .總用量 8
0 shelldir? 4 sh.Tgt? 4 sh.txt
按照指定類型排序
[root@host_128 shellscrips]# ls --sort=version .
sh.Tgt? sh.txt? shelldir
[root@host_128 shellscrips]# ls --sort=time .
sh.txt? sh.Tgt? shelldir
[root@host_128 shellscrips]# ls --sort=extension .
shelldir? sh.Tgt? sh.txt
[root@host_128 shellscrips]# ls --sort=size .
sh.Tgt? sh.txt? shelldir
類型可以是version(針對文件是版本號的如1.2.4,等同于-v)、time(等同于-t 修改時間)、extension、size(等同于-S)。
總結
以上是生活随笔為你收集整理的Linux ls查看目录文件命令集锦的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 其他应收款贷方余额表示什么
- 下一篇: 微信能贷款吗