2017-05-29 102 views
-1

我有12,000多个需要组织的文件。所有的文件夹都包含在内,但是文件现在处于展开的文件结构中。Bash Mac终端组织文件结构

我的文件夹和文件都被命名为它们应该在的路径。例如,在一个目录下我有一个名为\textures文件夹并命名为\textures\actors\bear但没有\textures\actors文件夹中的另一个文件夹。我正在努力开发一个宏,将采取这些文件夹,并把他们在每个文件夹和文件名建议它应该在正确的位置。我想能够自动排序这些textures和内部将是actors和里面那将是bear。但是,有超过12,000个文件,因此我正在寻找一个可以确定所有这一切的自动化流程,只要可能就做到这一点。

是否有脚本会查看每个文件或文件夹名称,并检测文件或文件夹应位于目录中的哪个文件夹,并自动将其移动到那里以及创建任何不存在于给定路径中的文件夹需要的时候?

感谢

+0

德文,如果下面的解决方案有效,你可以让这个知道。也许通过投票回答。 –

+0

德文,做了这个解决方案的工作还是你需要帮助来实现你的目标? –

+0

为什么在目录名称中有反斜杠? –

回答

0

给定的目录结构是这样的:

$ ls /tmp/stacktest 
    \textures 
    \textures\actors\bear 
     fur.png 
    \textures\actors\bear\fur2.png 

下面的Python脚本会变成这样:

$ ls /tmp/stackdest 
    textures/actors/bear 
     fur.png 
     fur2.png 

Python脚本:

from os import walk 
import os 

# TODO - Change these to correct locations 
dir_path = "/tmp/stacktest" 
dest_path = "/tmp/stackdest" 

for (dirpath, dirnames, filenames) in walk(dir_path): 
    # Called for all files, recu`enter code here`rsively 
    for f in filenames: 
     # Get the full path to the original file in the file system 
    file_path = os.path.join(dirpath, f) 

     # Get the relative path, starting at the root dir 
     relative_path = os.path.relpath(file_path, dir_path) 

     # Replace \ with/to make a real file system path 
     new_rel_path = relative_path.replace("\\", "/") 

     # Remove a starting "/" if it exists, as it messes with os.path.join 
     if new_rel_path[0] == "/": 
      new_rel_path = new_rel_path[1:] 
     # Prepend the dest path 
     final_path = os.path.join(dest_path, new_rel_path) 

     # Make the parent directory 
     parent_dir = os.path.dirname(final_path) 
     mkdir_cmd = "mkdir -p '" + parent_dir + "'" 
     print("Executing: ", mkdir_cmd) 
     os.system(mkdir_cmd) 

     # Copy the file to the final path 
     cp_cmd = "cp '" + file_path + "' '" + final_path + "'" 
     print("Executing: ", cp_cmd) 
     os.system(cp_cmd) 

该脚本读取dir_path中的所有文件和文件夹,并在dest_path下创建新的目录结构。确保你不要把dest_path放在dir_path之内。