如何使用终端从多个文件夹复制和重命名文件?
我有一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有指明您的操作系统,但如果您使用的是 Gnu
cp
,则可以使用cp week1/*/submit.pdf --backup=t week/
来让它(任意)数量已经存在的文件;但是,这不会给你任何真正的方法来识别哪个是哪个。也许你可以这样做:
...这将生成名为“week1-subdir-submit.pdf”的文件
对于它的价值,
“${var/s/r}”
表示法的意思是取var,但在插入其值之前,搜索s(\/
,意思是/
,由于其他特殊/
中的转义该表达式),并将其替换为 r (-
),以生成唯一的文件名。编辑:实际上还有一个
/
,使其匹配多次,语法如下:You don't indicate your OS, but if you're using Gnu
cp
, you can usecp 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:
… 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:find
来救援!经验法则:如果您可以使用find
列出所需的文件,则可以复制它们。所以首先尝试这个:一些注意事项:
find .
表示“从当前目录开始查找”-type -f
表示“仅查找常规文件”(即,不是目录)-iname 'submit.pdf'
“...具有不区分大小写的名称 'submit.dpf'”。您不需要使用'quotation'
,但如果您想使用通配符进行搜索,则需要。例如:如果你想搜索区分大小写,只需使用
-name
而不是-iname
。当此方法有效时,您可以使用
-exec
命令复制每个文件。exec
的工作原理是让您指定在命中时使用的命令。它将针对find
找到的每个文件运行该命令,并将文件名放入{}
中。您可以通过指定\;
来结束命令序列。因此,要
echo
所有文件,请执行以下操作:将它们一一复制:
希望这会有所帮助!
find
to the rescue! Rule of thumb: If you can list the files you want withfind
, you can copy them. So try first this: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.: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 filefind
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:To copy them one by one:
Hope this helps!