查找所有可执行文件,然后使用bash复制(以及文件夹)到某些虚拟文件夹
我正在尝试从文件夹中收集所有可执行文件,并尝试将其复制到虚拟文件夹中。我尝试了几件事,但是找到了一些原因,它也正在复制.txt文件
I am trying to collect all executable from folders and trying to copy them into dummy folder. I tried few things using find but some reason it is copying .txt files too
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您知道这并不是那么简单:
在我的PC上,我会得到以下结果:
...这并不奇怪:
您可以看到:一个具有扩展名的“*.log”的文件可以可执行。
It's really not that simple, you know:
On my PC, I get following results:
... which is not that surprising:
As you can see: a file with extension "*.log" can be executable.
它也复制
txt
文件,因为它们是可执行的...但是您的查找
尝试还有其他问题:它也复制目录,而不仅仅是文件。并请注意,您可以在源目录的不同子目录中使用几个具有相同名称的可执行文件,例如,
srcflder/bar/bar/foo
srcflder/baz/baz/foo 。在目的地复制它们时,最后一个会覆盖其他人。如果要复制没有
.txt
扩展名的可执行文件,并且您不在乎具有相同名称的文件,您可以尝试:如果您有更多的文件扩展程序可以排除您可以添加它们:
最后,如果您关心具有相同名称的文件,则可以在目标目录中保留源层次结构。为此,您必须在复制之前创建目标目录(如果尚不存在)。因此,如果找到的文件之一是
srcflder/bar/foo
您mkdir -p destinatyFolder/bar
在复制srcflder/bar/foo
之前destinationFolder/bar/foo
。带有小型bash脚本的示例为exec
命令:It is copying
txt
files too because they are executable... But there are some more issues with yourfind
attempt: it copies directories too, not just files.And note that you could have several executable files with the same name in different sub-directories of your source directory, e.g.,
srcFlder/bar/foo
andsrcFlder/baz/foo
. When copying them at destination the last one would overwrite the others.If you want to copy only executable files that don't have the
.txt
extension, and you don't care about files with the same name you can try:If you have some more file extensions to exclude you can add them with:
Finally, if you care about files with the same name, you could preserve the source hierarchy in the destination directory. To do so you must create the target directory (if it does not exist yet) before copying. So, if one of the found files is
srcFlder/bar/foo
youmkdir -p destinationFolder/bar
before copyingsrcFlder/bar/foo
todestinationFolder/bar/foo
. Example with a small bash script asexec
command: