简体   繁体   English

bash脚本,根据目录名称的模式在目录上递归运行命令

[英]bash script to recursively run commands on directories depending on the pattern of the directory name

I am trying to write a small bash script to help sort some recovered directories/files back to their original file structure prior to the recovery. 我正在尝试编写一个小的bash脚本,以帮助在恢复之前将一些恢复的目录/文件排序回其原始文件结构。 In a nutshell I have recovered a pool of disks that were pooled using aufs union file system, after the recovery of deleted directories/files I noticed that in a lot of the directories there are directories named ".wh..wh.original_dir_name.4_random_characters" for example ".wh..wh.Images.1E7W". 简而言之,我恢复了使用aufs union文件系统池化的磁盘池,恢复已删除的目录/文件后,我注意到许多目录中都有名为“ .wh..wh.original_dir_name.4_random_characters的目录” ”,例如“ .wh..wh.Images.1E7W”。 This directory would be located in the same directory where the actual "Images" directory is located. 该目录将位于实际“图像”目录所在的目录中。 These .wh directories are referred to as white space and are created by the aufs file system usually during delete operations but I noticed there are a lot of recoverable files/directories located in these but I would like to integrate them back into the normal structure. 这些.wh目录称为空白,通常由aufs文件系统在删除操作期间创建,但是我注意到其中有很多可恢复的文件/目录,但是我想将它们重新集成到正常结构中。

To paraphrase, this is what I want to do but do not know how to piece it together in a bash script. 简而言之,这是我想要做的,但不知道如何在bash脚本中将其拼凑在一起。

I need recursively traverse the structure, when I find a directory named something like .wh..wh. 当我找到一个名为.wh..wh之类的目录时,我需要递归遍历该结构。 then I need to parse the real name of the directory and if this directory already exists in the same directory then execute cp -r --backup=numbered .wh..wh./* real_directory_name/ and then delete the .wh..wh directory or if the directory does not exist then simply rename the directory using the mv command. 那么我需要解析目录的真实名称,如果该目录已经存在于同一目录中,则执行cp -r --backup=numbered .wh..wh./* real_directory_name/ ,然后删除.wh..wh目录,或者如果目录不存在,则只需使用mv命令重命名目录。 I assume that I would use the find command to traverse the structure but again not sure how to piece it altogether. 我假设我将使用find命令遍历该结构,但再次不确定如何将其完全拼合。 Any help would be awesome. 任何帮助都是极好的。

Here is an example file structure to clarify things: 这是一个示例文件结构,用于说明问题:

    /home/
    /home/.wh..wh.Images.67TY/
    /home/.wh..wh.Images.67TY/Camping/
    /home/Images/
    /home/Images/Camping/

In the above example I would like to use the cp command to copy the contents of /home/.wh..wh.Images.67TY to /home/Images and then delete /home/.wh..wh.Images.67TY 在上面的示例中,我想使用cp命令将/home/.wh..wh.Images.67TY的内容复制到/ home / Images,然后删除/home/.wh..wh.Images.67TY

Here is a solution using only bash parameter expansion: 这是仅使用bash参数扩展的解决方案:

#!/bin/bash

for BACKUPDIR in $(find . -type d -name ".wh..wh.*")
do
    BASE=$(dirname "${BACKUPDIR}")

    LASTPART=$(basename "${BACKUPDIR}")
    D1=${LASTPART#.wh..wh.}
    D2=${D1%.*}

    if [ -d "${BASE}/${D2}" ]
    then
         cp -a "${BACKUPDIR}/"* "${BASE}/${D2}"
    else
         mv "${FILEPATH}" "${BASE}/${D2}"
    fi

done

Haven't tested the copy/move part; 还没有测试复制/移动部分; you should first see with echo, if this gives you, what you want. 您应该首先看到回声,如果这给您,您想要什么。

#!/bin/bash
# handles directory names with spaces
IFS='
'
for BACKUPDIR in $(find . -depth -type d -name ".wh..wh.*")
do
    BASE=$(dirname "${BACKUPDIR}")
    LASTPART=$(basename "${BACKUPDIR}")
    D1=${LASTPART#.wh..wh.}
    D2=${D1%.*}

    if [ -d "${BASE}/${D2}" ]
    then
       echo copying from "${BACKUPDIR}" to "${BASE}/${D2}"
       # prevents deleting if the cp command fails
       if cp -a --backup=numbered "${BACKUPDIR}/"* "${BASE}/${D2}"
       then
           echo deleting "${BACKUPDIR}"
           rm -rf "${BACKUPDIR}"
       fi
    else
        echo moving "${BACKUPDIR}" to "${BASE}/${D2}"
        mv "${BACKUPDIR}" "${BASE}/${D2}"
    fi
done

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

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