简体   繁体   English

linux如何将不同文件夹下的同名文件批量复制重命名到单独的文件夹,文件重命名为父文件夹?

[英]How do batch copy and rename files with same names under different folders to a separate folder with files renamed to their parent folder in linux?

Say I have folders named user/john/john1 , john2 and john3 and I have one file called dog.txt inside each of them.假设我有名为user/john/john1john2john3的文件夹,每个文件夹中都有一个名为dog.txt的文件。 I want to create a separate folder called johns_dogs and copy all the dog.txt from each folder into this new folder, but with the files renamed as john1_dog.txt , john2_dog.txt , john3_dog.txt .我想创建一个名为johns_dogs的单独文件夹,并将每个文件夹中的所有dog.txt复制到这个新文件夹中,但文件重命名为john1_dog.txtjohn2_dog.txtjohn3_dog.txt

How would I go about this?我怎么会go这个呢? I assume I can use a for loop for this, and I have been playing around with it..i just can't seem to get it right;我想我可以为此使用 for 循环,而且我一直在玩弄它..我似乎无法做到正确; specifically isolating the folder names to apply to the file name using the linux terminal.使用 linux 终端专门隔离文件夹名称以应用于文件名。 Using GUI is not an option.使用 GUI 不是一种选择。 Thank you:)谢谢:)

for fname in user/john/*/dog.txt;
do
  new_name=basename $(PWD)
  cp user/john/*/dog.txt > $new_name.txt;
done

This is what I've tried to do...but it doesn't work, and i don't know why?这就是我尝试做的……但它不起作用,我不知道为什么?

One option would be to use bash string substitution via parameter expansion to build the desired output path for each file:一种选择是通过参数扩展使用 bash 字符串替换来为每个文件构建所需的 output 路径:

for f in ./user/john/*/dog.txt ; do 
    new="${f//\//_}"
    new="${new/._user_john_/./johns_dogs/}"
    cp "$f" "$new"
done

or as one-liner:或作为单行:

for f in ./user/john/*/dog.txt ; do new="${f//\//_}" ; new="${new/._user_john_/./johns_dogs/}" ; cp "$f" "$new"; done

After running above command contents of .johns_dogs directory:运行上述命令后.johns_dogs目录的内容:

find ./johns_dogs/*
./johns_dogs/john1_dog.txt
./johns_dogs/john2_dog.txt
./johns_dogs/john3_dog.txt
./johns_dogs/john4_dog.txt
./johns_dogs/john5_dog.txt

String replacement details:字符串替换细节:

new="${f//\//_}"  # replace all '/' with '_' in original path to create new path
new="${new/._user_john_/./johns_dogs/}" # replace '._user_john_' with './johns_dogs' to complete new path

Instead of some utterly incomprehensible, works-by-voodoo-magic, write-test-debug-then-throw-away script, my approach in similar cases is as follows:我在类似情况下的方法如下:

  • Use the ls command to create a "long" listing of your files.使用ls命令创建文件的“长”列表。 (One full filename per line.) (每行一个完整的文件名。)
  • Pipe it into a text file. Pipe 将其转换为文本文件。
  • Open the text file in an editor which supports column editing.在支持列编辑的编辑器中打开文本文件。
  • Copy the column of filenames to the clipboard.将文件名列复制到剪贴板。
  • While keeping the copied column in the clipboard:将复制的列保留在剪贴板中:
    • Edit the column of filenames, so as to turn them into the destination filenames.编辑文件名列,将它们变成目标文件名。 (Use column-mode and/or search-and-replace for this.) (为此使用列模式和/或搜索和替换。)
  • Paste the column of original filenames from the clipboard into the text file, right before the column of altered filenames.将剪贴板中的原始文件名列粘贴到文本文件中,就在更改后的文件名列之前。
  • Prepend cp at the start of each line.在每行的开头加上cp
  • Save the text file as a shell script.将文本文件另存为 shell 脚本。 (Prepend #!bash , do the necessary chmod ) #!bash前面加上必要的chmod
  • Run the shell script.运行 shell 脚本。

It might seem like a lot of work, but actually you can do it very quickly (as long as you do not have to manually modify each line) and the great benefit of it is that you can just examine it with your eyes and have a fairly high degree of confidence that once run, it will do the right thing, whereas the script might destroy your filesystem and you will only know after the fact.这看起来工作量很大,但实际上你可以很快完成(只要你不必手动修改每一行)而且它的最大好处是你可以用眼睛检查它并有一个相当高的信心,一旦运行,它就会做正确的事情,而脚本可能会破坏你的文件系统,你只会在事后才知道。

I mean, please do not get me wrong, I am a Software Engineer, I write code for a living, I solve problems by writing code, but there are some cases where it is just not worth writing code.我的意思是,请不要误会我的意思,我是一名软件工程师,我以编写代码为生,我通过编写代码来解决问题,但在某些情况下不值得编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM