Linux 中 仅列出指定目录下的所有文件,但是不包括目录

 

001、find命令仅仅列出文件,不包括目录

a、

[root@PC1 test1]# ls                 ## 测试目录
dir001  dir002  dir003  file1.map  file1.txt  file2.map  file2.txt
[root@PC1 test1]# find -type f       ## 仅仅列出文件
./file1.txt
./file2.txt
./file1.map
./file2.map

 

b、仅输出文件名,不包括路径

[root@PC1 test1]# ls                                  ## 测试目录
dir001  dir002  dir003  file1.map  file1.txt  file2.map  file2.txt
[root@PC1 test1]# find -type f                        ## 仅输出文件,但是包括路径
./file1.txt
./file2.txt
./file1.map
./file2.map
[root@PC1 test1]# find -type f -exec basename {} \;   ## 只输出文件名
file1.txt
file2.txt
file1.map
file2.map

 

002、ls -l + grep + awk实现

 

[root@PC1 test1]# ls                 ## 测试目录
dir001  dir002  dir003  file1.map  file1.txt  file2.map  file2.txt
[root@PC1 test1]# ls -l              ## 列出当前路径下的所有内容
total 0
drwxr-xr-x. 2 root root 6 Feb  7 12:41 dir001
drwxr-xr-x. 2 root root 6 Feb  7 12:41 dir002
drwxr-xr-x. 2 root root 6 Feb  7 12:41 dir003
-rw-r--r--. 1 root root 0 Feb  7 12:40 file1.map
-rw-r--r--. 1 root root 0 Feb  7 12:40 file1.txt
-rw-r--r--. 1 root root 0 Feb  7 12:40 file2.map
-rw-r--r--. 1 root root 0 Feb  7 12:40 file2.txt
[root@PC1 test1]# ls -l | grep "^-"   ## grep正则输出文件的行
-rw-r--r--. 1 root root 0 Feb  7 12:40 file1.map
-rw-r--r--. 1 root root 0 Feb  7 12:40 file1.txt
-rw-r--r--. 1 root root 0 Feb  7 12:40 file2.map
-rw-r--r--. 1 root root 0 Feb  7 12:40 file2.txt
[root@PC1 test1]# ls -l | grep "^-" | awk '{print $NF}'    ## 输出最后一列的文件名
file1.map
file1.txt
file2.map
file2.txt

 。

 

posted @ 2024-02-07 12:49  小鲨鱼2018  阅读(36)  评论(0编辑  收藏  举报