简体   繁体   English

根据目录名称移动文件(Linux / Debian /#!)

[英]Move files based on directory name (Linux / Debian / #!)

Question: 题:

How do I move files from many sub-directories with the same name to a single directory of that name. 如何将文件从多个具有相同名称的子目录移动到该名称的单个目录中。 All files in multiple directories named X should get moved together into one directory named X. 多个名为X的目录中的所有文件应一起移动到一个名为X的目录中。

Example file structure: 示例文件结构:

(The real structure is 200-300 directories at the level I've marked as 1, 2, 3, etc., with varying numbers of directories below that.) (真正的结构是200-300个目录,在我标记为1、2、3等的级别上,其下的目录数量不同。)

(Note all labels like Group _ are just that--labels. The actual names are irregular.) (请注意,所有类似Group _的标签都只是这些标签。实际名称是不规则的。)

  • Disk 磁碟

    • 1 1个

      • Library 图书馆
        • Music 音乐
          • Group A A组
            • Files 档案
            • Files 档案
            • Files 档案
          • Group B B组
            • Files 档案
            • Files 档案
            • Files 档案
    • 2 2

      • Library 图书馆
        • Music 音乐
          • Group B B组
            • Files 档案
            • Files 档案
            • Files 档案
          • Group C C组
            • Files 档案
            • Files 档案
            • Files 档案
    • 3 3
      • Library 图书馆
        • Music 音乐
          • Group C C组
            • Files 档案
            • Files 档案
            • Files 档案
          • Group D D组
            • Files 档案
            • Files 档案
            • Files 档案

The goal is to have files in various sub-directories with name "Group X" moved into a single directory with name "Group X", like so: 目标是将名称为“ Group X”的各个子目录中的文件移动到名称为“ Group X”的单个目录中,如下所示:

  • Disk 磁碟
    • 1 1个
      • Library 图书馆
        • Music 音乐
          • Group A A组
            • Files 档案
            • Files 档案
            • Files 档案
          • Group B B组
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
          • Group C C组
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
            • Files 档案
          • Group D D组
            • Files 档案
            • Files 档案
            • Files 档案

Thanks! 谢谢!

You would do something like: 您将执行以下操作:

dest="Group A"
find -type f | 
grep $dest | 
while read filename
do     
    echo mv $filename ${dest}/$(basename $filename)
done

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

相关问题 如何根据 linux 中的名称将文件移动到新目录? - How do I move files to a new directory based on their name in linux? 如何在Linux中根据名称在具有相应名称的文件夹中移动文件? - How to move files in Linux based on its name in a folder with a corresponding name? 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 使用Linux脚本根据文件名模式查找和移动文件 - find and move files based on file name pattern using linux script 在同一目录中移动多个文件,而无需每次都输入目录名(Linux) - Move multiple files in same directory without typing directory name each time (Linux) Linux循环将文件移动到其子目录 - Linux Move files to their child directory in a loop 需要根据Linux中文件名的最后两个字符将文件移动到特定目录 - Need to move files to specific directory based on last two characters of filename in Linux 将目录名称附加到文件末尾并将其移动 - Append directory name to the end of the files and move them Linux根据目录中的另一个文件重命名文件? - Linux rename files based on another file in the directory? Shell:如何将多个文件移动到一个目录并以不同的名称压缩该目录? - Shell: How to move multiple files to a directory and zip that directory with different name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM