如何使用终端从多个文件夹复制和重命名文件?

发布于 2024-12-21 16:13:05 字数 246 浏览 2 评论 0原文

我有一个名为“week1”的文件夹,该文件夹中还有大约十个其他文件夹,其中都包含多个文件,其中一个名为“submit.pdf”。我希望能够将所有“submit.pdf”文件复制到一个文件夹中,最好使用终端来加快这一过程。我尝试过 cp week1/*/submit.pdf week1/ 以及 cp week1/*/*.pdf week1/,但最终只是复制一个文件。我刚刚意识到它每次都会写入每个文件,这就是为什么我坚持使用一个文件......无论如何我可以防止这种情况发生吗?

I have a folder called "week1", and in that folder there are about ten other folders that all contain multiple files, including one called "submit.pdf". I would like to be able to copy all of the "submit.pdf" files into one folder, ideally using Terminal to expedite the process. I've tried cp week1/*/submit.pdf week1/ as well as cp week1/*/*.pdf week1/, but it had only been ending up copying one file. I just realized that it has been writing over each file every time which is why I'm stuck with one...is there anyway I can prevent that from happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

奶气 2024-12-28 16:13:05

您没有指明您的操作系统,但如果您使用的是 Gnu cp,则可以使用 cp week1/*/submit.pdf --backup=t week/ 来让它(任意)数量已经存在的文件;但是,这不会给你任何真正的方法来识别哪个是哪个。

也许你可以这样做:

 for file in week1/*/submit.pdf; do cp "$file" "${file//\//-}"; done

...这将生成名为“week1-subdir-submit.pdf”的文件

对于它的价值,“${var/s/r}”表示法的意思是取var,但在插入其值之前,搜索s\/,意思是/,由于其他特殊 / 中的转义该表达式),并将其替换为 r (-),以生成唯一的文件名。

编辑:实际上还有一个 / ,使其匹配多次,语法如下:

             "${ var           /        /                 \/  /      -    }"
                 take "var"    replace  every instance of /   with   -

You don't indicate your OS, but if you're using Gnu cp, you can use cp week1/*/submit.pdf --backup=t week/ to have it (arbitrarily) number files that already exist; but, that won't give you any real way to identify which-is-which.

You could, perhaps, do something like this:

 for file in week1/*/submit.pdf; do cp "$file" "${file//\//-}"; done

… which will produce files named something like "week1-subdir-submit.pdf"

For what it's worth, the "${var/s/r}" notation means to take var, but before inserting its value, search for s (\/, meaning /, escaped because of the other special / in that expression), and replace it with r (-), to make the unique filenames.

Edit: There's actually one more / in there, to make it match multiple times, making the syntax:

             "${ var           /        /                 \/  /      -    }"
                 take "var"    replace  every instance of /   with   -
汐鸠 2024-12-28 16:13:05

find 来救援!经验法则:如果您可以使用 find 列出所需的文件,则可以复制它们。所以首先尝试这个:

$ cd your_folder
$ find . -type f -iname 'submit.pdf'

一些注意事项:

  • find . 表示“从当前目录开始查找”
  • -type -f 表示“仅查找常规文件”(即,不是目录)
  • -iname 'submit.pdf' “...具有不区分大小写的名称 'submit.dpf'”。您不需要使用'quotation',但如果您想使用通配符进行搜索,则需要。例如:

     ~ foo$ find /usr/lib -iname '*.So*'
     /usr/lib/pam/pam_deny.so.2
     /usr/lib/pam/pam_env.so.2
     /usr/lib/pam/pam_group.so.2
     ...
    

如果你想搜索区分大小写,只需使用-name而不是-iname

当此方法有效时,您可以使用 -exec 命令复制每个文件。 exec 的工作原理是让您指定在命中时使用的命令。它将针对 find 找到的每个文件运行该命令,并将文件名放入 {} 中。您可以通过指定 \; 来结束命令序列。

因此,要echo所有文件,请执行以下操作:

$ find . -type f -iname submit.pdf -exec echo Found file {} \;

将它们一一复制:

$ find . -type f -iname submit.pdf -exec cp {} /destination/folder \;

希望这会有所帮助!

find to the rescue! Rule of thumb: If you can list the files you want with find, you can copy them. So try first this:

$ cd your_folder
$ find . -type f -iname 'submit.pdf'

Some notes:

  • find . means "start finding from the current directory"
  • -type -f means "only find regular files" (i.e., not directories)
  • -iname 'submit.pdf' "... with case-insensitive name 'submit.dpf'". You don't need to use 'quotation', but if you want to search using wildcards, you need to. E.g.:

     ~ foo$ find /usr/lib -iname '*.So*'
     /usr/lib/pam/pam_deny.so.2
     /usr/lib/pam/pam_env.so.2
     /usr/lib/pam/pam_group.so.2
     ...
    

If you want to search case-sensitive, just use -name instead of -iname.

When this works, you can copy each file by using the -exec command. exec works by letting you specify a command to use on hits. It will run the command for each file find finds, and put the name of the file in {}. You end the sequence of commands by specifying \;.

So to echo all the files, do this:

$ find . -type f -iname submit.pdf -exec echo Found file {} \;

To copy them one by one:

$ find . -type f -iname submit.pdf -exec cp {} /destination/folder \;

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文