Linux命令学习-ls
Linux中,ls命令的全称是(list directory contents)
,主要作用是用于显示指定目录下的内容。
- 列出Linux根目录下的所有目录
ls /
ls /
Applications Users cores home sbin var
Library Volumes dev opt tmp
System bin etc private usr
- 将/bin目录以下所有目录及文件详细资料列出
ls -lR /bin
- 列出当前目录下所有文件夹和文件
ls
(base) ~/Desktop/testA ls
test1 test10 test2 test3 test4 test5 test6 test7 test8 test9 testB
- 列出当前目录下所有文件夹和文件(包括以“.”开头的文件)
ls -a
(base) ~/Desktop/testA ls -a
. test1 test2 test4 test6 test8 testB
.. test10 test3 test5 test7 test9
- 列出当前目录下所有文件详细信息(包括权限、所有者、文件大小、创建时间等)
ls -l
(base) ~/Desktop/testA ls -l
total 0
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test1
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test10
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test2
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test3
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test4
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test5
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test6
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test7
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test8
drwxr-xr-x 3 shiying staff 96 5 14 11:06 test9
drwxr-xr-x 4 shiying staff 128 5 14 11:08 testB
(1)在使用 ls -l
命令时,第一列的字符表示文件或目录的类型和权限。其中第一个字符表示文件类型,例如:
-表示普通文件
d表示目录
l表示符号链接
c表示字符设备文件
b表示块设备文件
s表示套接字文件
p表示管道文件
(2) 在使用 ls -l 命令时,第一列的其余 9 个字符表示文件或目录的访问权限,分别对应三个字符一组的 rwx 权限。例如:
r表示读取权限
w表示写入权限
x表示执行权限
-表示没有对应权限
(3)前三个字符表示所有者的权限,中间三个字符表示所属组的权限,后三个字符表示其他用户的权限。例如:表示文件名为计算机.csv的文件,所有者具有读写权限,所属组和其他用户只有读取权限。
-rw-r--r--@ 1 shiying staff 3 4 11 13:44 计算器.csv
- 列出当前目录下所有以 test 开头 或 以.txt结尾 的文件
- 以test开头:ls test*
(base) ~/Desktop/testA ls test*
test1:
test
test10:
test
test2:
test
test3:
test
test4:
test
test5:
test
test6:
test
test7:
test
test8:
test
test9:
test
testB:
testC testD
- 以txt结尾:ls *.txt
(base) ~/Desktop/testA ls *.txt
mobile.txt test.txt
- 按照修改时间排序显示当前目录中的文件和目录
ls -t
(base) ~/Desktop ls -t
截屏2024-05-14 09.44.07.png LMS 测试报告
截屏2024-05-13 15.18.47.png test
- 递归显示当前目录中的所有文件和子目录
ls -R
(base) ~/Desktop/testA ls -R
mobile.txt test1 test2 test4 test6 test8 testB
test.txt test10 test3 test5 test7 test9
./test1:
test
./test1/test:
./test10:
test
./test10/test:
./test2:
test
./test2/test:
./test3:
test
./test3/test:
./test4:
test
./test4/test:
./test5:
test
./test5/test:
./test6:
test
./test6/test:
./test7:
test
./test7/test:
./test8:
test
./test8/test:
./test9:
test
./test9/test:
./testB:
testC testD
./testB/testC:
./testB/testD:
- 列出当前工作目录下所有名称是test开头的文件,越新的排越后面
(base) ~/Desktop/testA ls -ltr test*
-rw-r--r--@ 1 shiying staff 94 4 10 19:08 test.txt
test1:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test2:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test3:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test4:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test5:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test6:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test7:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test8:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test9:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
test10:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 test
testB:
total 0
drwxr-xr-x 2 shiying staff 64 5 14 11:06 testC
drwxr-xr-x 2 shiying staff 64 5 14 11:08 testD
- 当文件名包含空格、特殊字符或者开始字符为破折号时,可以使用反斜杠(\)进行转义,或者使用引号将文件名括起来。
例如: 列出文件名为"my file.xlsx"的文件
(base) ~/Desktop ls my\ file.xlsx
my file.xlsx
(base) ✘ ~/Desktop ls "my file.xlsx"
my file.xlsx
- 列出文件名为"testA"的文件
(base) ~/Desktop ls -- testA
test1 test10 test2 test3 test4 test5 test6 test7 test8 test9 testB
(base) ~/Desktop ls testA/
test1 test10 test2 test3 test4 test5 test6 test7 test8 test9 testB