让“查找"仅打印文件名,而不打印完整路径 [英] Have Find print just the filenames, not full paths

查看:137
本文介绍了让“查找"仅打印文件名,而不打印完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ksh脚本中使用find命令,并且试图检索文件名而不是完整路径.在这种情况下,我希望它返回text.exe,而不是//severname/dir1/dir2/text.exe.

I'm using the find command in a ksh script, and I'm trying to retrieve just the filenames, rather than the full path. As in, I want it to return text.exe, not //severname/dir1/dir2/text.exe.

我将如何获得它?为了明确起见,我知道文件所在的目录,我只是在获取某个日期之前获取创建的文件,因此路径名无关紧要.

How would I go about getting that? To clarify, i know the directory the files are in, i am just grabbing the ones created befoee a ceetain date, so the pathname doesnt matter.

推荐答案

您可以执行以下操作:

find ..... |sed 's#.*/##'

但这真的有道理吗?如果有两个文件名相同但位于不同目录中的文件,如何区分它们?

however does it really make sense? if there are two files with same filename but located in different directories, how can you distinguish them?

例如

you are in /foo

/foo/a.txt
/foo/bar/a.txt

编辑

编辑答案以获得更好的文本格式.

edit the answer to gain some better text formatting.

正如您在评论中所述,所以您想

As you described in comment, so you want to

  1. 找到一些文件,
  2. 将它们复制到目录中,
  3. 将其gzip压缩到一个存档文件中,例如a.gz
  4. 仅在第2步成功后才删除复制的文件

这可以一次完成:

find ...|xargs tar -czf /path/to/your/target/a.gz 

这将查找文件,并在目标目录中创建一个tar(a.gz).

this will find files, make a tar (a.gz) to your target dir.

这篇关于让“查找"仅打印文件名,而不打印完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆