bash 在同一个文件夹终端脚本中复制多个同名文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14879174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying multiple files with same name in the same folder terminal script
提问by mannaroth
I have a lot of files named the same, with a directory structure (simplified) like this:
我有很多同名的文件,目录结构(简化)如下:
../foo1/bar1/dir/file_1.ps
../foo1/bar2/dir/file_1.ps
../foo2/bar1/dir/file_1.ps
.... and many more
As it is extremely inefficient to view all of those ps files by going to the respective directory, I'd like to copy all of them into another directory, but include the name of the first two directories (which are those relevant to my purpose) in the file name.
由于通过转到相应目录来查看所有这些 ps 文件效率极低,我想将它们全部复制到另一个目录中,但包括前两个目录的名称(与我的目的相关的目录)在文件名中。
I have previously tried like this, but I cannot get which file is from where, as they are all named consecutively:
我以前试过这样,但我无法得到哪个文件来自哪里,因为它们都是连续命名的:
#!/bin/bash -xv
cp -v --backup=numbered {} */*/dir/file* ../plots/;
Where ../plotsis the folder where I copy them. However, they are now of the form file.ps.~x~ (x is a number) so I get rid of the ".ps.~*~" and leave only the ps extension with:
哪里../plots是我复制它们的文件夹。但是,它们现在的格式为 file.ps.~x~(x 是一个数字),所以我去掉了“.ps.~*~”,只留下 ps 扩展名:
rename 's/\.ps.~*~//g' *;
rename 's/\~/.ps/g' *;
Then, as the ps files have hundreds of points sometimes and take a long time to open, I just transform them into jpg.
然后,由于ps文件有时有数百个点并且需要很长时间才能打开,我只是将它们转换为jpg。
for file in * ; do convert -density 150 -quality 70 "$file" "${file/.ps/}".jpg; done;
This is not really a working bash script as I have to change the directory manually.
这不是一个真正有效的 bash 脚本,因为我必须手动更改目录。
I guess the best way to do it is to copy the files form the beginning with the names of the first two directories incorporated in the copied filename.
我想最好的方法是从开头复制文件,并将前两个目录的名称包含在复制的文件名中。
How can I do this last thing?
我怎么能做这最后一件事?
回答by that other guy
If you just have two levels of directories, you can use
如果您只有两级目录,则可以使用
for file in */*/*.ps
do
ln "$file" "${file//\//_}"
done
This goes over each ps file, and hard links them to the current directory with the /s replaced by _. Use cpinstead of lnif you intend to edit the files but don't want to update the originals.
这会遍历每个 ps 文件,并将它们硬链接到当前目录,将/s 替换为_. 如果您打算编辑文件但不想更新原始文件,请使用cp代替ln。
For arbitrary directory levels, you can use the bash specific
对于任意目录级别,您可以使用特定于 bash 的
shopt -s globstar
for file in **/*.ps
do
ln "$file" "${file//\//_}"
done
But are you sure you need to copy them all to one directory? You might be able to open them all with yourreader */*/*.ps, which depending on your reader may let browse through them one by one while still seeing the full path.
但是您确定需要将它们全部复制到一个目录中吗?您也许可以使用 将它们全部打开yourreader */*/*.ps,这取决于您的读者可以让它们一一浏览,同时仍能看到完整路径。
回答by mtk
You should run a find command and print the names first like
您应该运行 find 命令并首先打印名称,例如
find . -name "file_1.ps" -print
Then iterate over each of them and do a string replacement of /to '-' or any other character like
然后遍历它们中的每一个并进行字符串替换/为 '-' 或任何其他字符,如
${filename/\//-}
The general syntax is ${string/substring/replacement}. Then you can copy it to the required directory. The complete script can be written as follows. Haven't tested it (not on linux at the moment), so you might need to tweak the code if you get any syntax error ;)
一般语法是${string/substring/replacement}. 然后就可以复制到需要的目录了。完整的脚本可以写成如下。尚未对其进行测试(目前不在 linux 上),因此如果您遇到任何语法错误,您可能需要调整代码;)
for filename in `find . -name "file_1.ps" -print`
do
newFileName=${filename/\//-}
cp $filename YourNewDirectory/$newFileName
done
You will need to place the script in the same root directory or change the find command to look for the particular directory if you are placing the above script in some other directory.
如果将上述脚本放在其他目录中,则需要将脚本放在同一根目录中,或者更改 find 命令以查找特定目录。
References
参考

