linux 中find命令查找到文件仅显示文件名、路径名、完整路径

发布时间 2023-05-30 16:47:10作者: 小鲨鱼2018

 

001、

[root@PC1 test3]# ls
test1  test2
[root@PC1 test3]# tree                     ## 测试数据
.
├── test1
│   └── a.txt
└── test2
    └── b.txt

2 directories, 2 files
[root@PC1 test3]# find ./ -name "*.txt"   ## 一般显示模式
./test1/a.txt
./test2/b.txt

 

002、仅显示文件名

[root@PC1 test3]# ls
test1  test2
[root@PC1 test3]# tree
.
├── test1
│   └── a.txt
└── test2
    └── b.txt

2 directories, 2 files
[root@PC1 test3]# find ./ -name "*.txt"
./test1/a.txt
./test2/b.txt
[root@PC1 test3]# find ./ -name "*.txt" -exec basename {} \;        ## 仅显示文件名
a.txt
b.txt
[root@PC1 test3]# find ./ -name "*.txt" | xargs -i basename {}
a.txt
b.txt

 

003、显示绝对路径

[root@PC1 test3]# ls
test1  test2
[root@PC1 test3]# tree
.
├── test1
│   └── a.txt
└── test2
    └── b.txt

2 directories, 2 files
[root@PC1 test3]# find ./ -name "*.txt"
./test1/a.txt
./test2/b.txt
[root@PC1 test3]# find ./ -name "*.txt" -exec readlink -f {} \;      ## 显示绝对路径
/home/test3/test3/test1/a.txt
/home/test3/test3/test2/b.txt
[root@PC1 test3]# find ./ -name "*.txt" | xargs -i readlink -f {}     ## 显示绝对路径
/home/test3/test3/test1/a.txt
/home/test3/test3/test2/b.txt

 

004、进显示路径

[root@PC1 test3]# ls
test1  test2
[root@PC1 test3]# tree
.
├── test1
│   └── a.txt
└── test2
    └── b.txt

2 directories, 2 files
[root@PC1 test3]# find ./ -name "*.txt"
./test1/a.txt
./test2/b.txt                                ## 仅显示路径
[root@PC1 test3]# find ./ -name "*.txt" -exec readlink -f {} \; | xargs -i dirname {}
/home/test3/test3/test1
/home/test3/test3/test2