遍历文件夹内的特定子文件夹并删除包含特定名称的文件 - AIX
loop through specific sub folders inside a folder and delete files contains specific name - AIX
我的文件夹结构如下:
有 100 个文件夹,每个文件夹有 3 个子文件夹(假设 A、B 和 C)。我的任务是从所有 100 个文件夹中删除仅在文件夹 C 中具有特定文本的文件。
我对 Unix 命令知之甚少,我的任务是在 AIX 服务器上执行
我尝试 google 并找到了下面的脚本(它实际上并没有删除)。
当我尝试执行以下脚本时,出现以下错误
"test.sh[5]: accepted: not found."
但实际上有 "accepted" 文件夹 existed.not 确定为什么会发生错误。
for dir in $(ls)
do
for dir2 in $(ls)
do
cd accepted
echo $(ls)
done
done
任何人都可以帮我更新脚本以执行 "loop through sub folders inside a folder and delete files contains specific text inside specific sub folder only" 吗?
虽然可以实现使用树搜索来识别文件的逻辑,但在本例中这不是必需的。考虑以下因素。
请注意,如果任何文件名包含特殊字符(空格、引号、换行符),解决方案将不起作用。它适用于 "normal" 文件名(字符 a-zA-Z0-9_-.
等)..
file_list=$(grep -l SomeText */C/*)
rm $file_list
可以合并为一行 - 小心使用
rm $(grep -l SomeText */C/*)
如果需要,您也可以使用 rm -i
而不是 rm
来提示删除每个文件。
以目录树为例:
X
├── A
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ ├── bar.txt
│ └── foo.txt
├── B
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ ├── bar.txt
│ └── foo.txt
└── C
├── D
│ ├── bar.txt
│ └── foo.txt
├── E
│ ├── bar.txt
│ └── foo.txt
└── F
├── bar.txt
└── foo.txt
假设您只想从目录 F
中删除以 foo
开头的文件。
那么请尝试以下操作:
find X/*/F -type f -name "foo*" -delete
现在目录树将如下所示:
X
├── A
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ └── bar.txt
├── B
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ └── bar.txt
└── C
├── D
│ ├── bar.txt
│ └── foo.txt
├── E
│ ├── bar.txt
│ └── foo.txt
└── F
└── bar.txt
希望这能满足您的要求。
我的文件夹结构如下: 有 100 个文件夹,每个文件夹有 3 个子文件夹(假设 A、B 和 C)。我的任务是从所有 100 个文件夹中删除仅在文件夹 C 中具有特定文本的文件。
我对 Unix 命令知之甚少,我的任务是在 AIX 服务器上执行 我尝试 google 并找到了下面的脚本(它实际上并没有删除)。 当我尝试执行以下脚本时,出现以下错误 "test.sh[5]: accepted: not found." 但实际上有 "accepted" 文件夹 existed.not 确定为什么会发生错误。
for dir in $(ls)
do
for dir2 in $(ls)
do
cd accepted
echo $(ls)
done
done
任何人都可以帮我更新脚本以执行 "loop through sub folders inside a folder and delete files contains specific text inside specific sub folder only" 吗?
虽然可以实现使用树搜索来识别文件的逻辑,但在本例中这不是必需的。考虑以下因素。
请注意,如果任何文件名包含特殊字符(空格、引号、换行符),解决方案将不起作用。它适用于 "normal" 文件名(字符 a-zA-Z0-9_-.
等)..
file_list=$(grep -l SomeText */C/*)
rm $file_list
可以合并为一行 - 小心使用
rm $(grep -l SomeText */C/*)
如果需要,您也可以使用 rm -i
而不是 rm
来提示删除每个文件。
以目录树为例:
X
├── A
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ ├── bar.txt
│ └── foo.txt
├── B
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ ├── bar.txt
│ └── foo.txt
└── C
├── D
│ ├── bar.txt
│ └── foo.txt
├── E
│ ├── bar.txt
│ └── foo.txt
└── F
├── bar.txt
└── foo.txt
假设您只想从目录 F
中删除以 foo
开头的文件。
那么请尝试以下操作:
find X/*/F -type f -name "foo*" -delete
现在目录树将如下所示:
X
├── A
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ └── bar.txt
├── B
│ ├── D
│ │ ├── bar.txt
│ │ └── foo.txt
│ ├── E
│ │ ├── bar.txt
│ │ └── foo.txt
│ └── F
│ └── bar.txt
└── C
├── D
│ ├── bar.txt
│ └── foo.txt
├── E
│ ├── bar.txt
│ └── foo.txt
└── F
└── bar.txt
希望这能满足您的要求。