查找所有可执行文件,然后使用bash复制(以及文件夹)到某些虚拟文件夹

发布于 2025-02-11 01:09:49 字数 72 浏览 2 评论 0原文

我正在尝试从文件夹中收集所有可执行文件,并尝试将其复制到虚拟文件夹中。我尝试了几件事,但是找到了一些原因,它也正在复制.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2025-02-18 01:09:49

您知道这并不是那么简单:

find ./ -executable

在我的PC上,我会得到以下结果:

./20211214/Logs/2021-02-13.log
./20211214/Logs/2021-02-14.log
./20211214/Logs/2021-02-15.log
./20211214/Logs/2021-02-16.log
./20211214/Logs/2021-02-17.log

...这并不奇怪:

ls -ltra 2021-02-17.log
 -rwxrwxrwx 1 usr usr 441793 Feb 26  2021./2021-02-17.log

您可以看到:一个具有扩展名的“*.log”的文件可以可执行。

It's really not that simple, you know:

find ./ -executable

On my PC, I get following results:

./20211214/Logs/2021-02-13.log
./20211214/Logs/2021-02-14.log
./20211214/Logs/2021-02-15.log
./20211214/Logs/2021-02-16.log
./20211214/Logs/2021-02-17.log

... which is not that surprising:

ls -ltra 2021-02-17.log
 -rwxrwxrwx 1 usr usr 441793 Feb 26  2021./2021-02-17.log

As you can see: a file with extension "*.log" can be executable.

冷心人i 2025-02-18 01:09:49

它也复制txt文件,因为它们是可执行的...但是您的查找尝试还有其他问题:它也复制目录,而不仅仅是文件。

并请注意,您可以在源目录的不同子目录中使用几个具有相同名称的可执行文件,例如,srcflder/bar/bar/foo srcflder/baz/baz/foo 。在目的地复制它们时,最后一个会覆盖其他人。

如果要复制没有.txt扩展名的可执行文件,并且您不在乎具有相同名称的文件,您可以尝试:

find srcFlder -type f -name '*.txt' -prune -o \
  -type f -perm /a+x -exec cp -f {} destinationFolder \;

如果您有更多的文件扩展程序可以排除您可以添加它们:

find srcFlder -type f \( -name '*.txt' -o -name '*.log' \) -prune -o \
  -type f -perm /a+x -exec cp -f {} destinationFolder \;

最后,如果您关心具有相同名称的文件,则可以在目标目录中保留源层次结构。为此,您必须在复制之前创建目标目录(如果尚不存在)。因此,如果找到的文件之一是srcflder/bar/foomkdir -p destinatyFolder/bar在复制srcflder/bar/foo之前destinationFolder/bar/foo。带有小型bash脚本的示例为exec命令:

find srcFlder -type f -name '*.txt' -prune -o \
  -type f -perm /a+x -exec bash -c 'f="${1/#srcFlder/destinationFolder}"; \
  mkdir -p $(dirname "$f"); cp "$1" "$f"' _ {} \;

It is copying txt files too because they are executable... But there are some more issues with your find 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 and srcFlder/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:

find srcFlder -type f -name '*.txt' -prune -o \
  -type f -perm /a+x -exec cp -f {} destinationFolder \;

If you have some more file extensions to exclude you can add them with:

find srcFlder -type f \( -name '*.txt' -o -name '*.log' \) -prune -o \
  -type f -perm /a+x -exec cp -f {} destinationFolder \;

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 you mkdir -p destinationFolder/bar before copying srcFlder/bar/foo to destinationFolder/bar/foo. Example with a small bash script as exec command:

find srcFlder -type f -name '*.txt' -prune -o \
  -type f -perm /a+x -exec bash -c 'f="${1/#srcFlder/destinationFolder}"; \
  mkdir -p $(dirname "$f"); cp "$1" "$f"' _ {} \;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文