給定目錄結構,如
parent
- child1
- file1
- file2
- file3
- child2
- file1
- file3
- child3
- file1
- file2
- file3
- file4
什么命令將洗掉所有子目錄parent包含全部或部分的file1,file2,file3但沒有其他人。即在示例中child1,child2應洗掉但child3應保留,因為它也包含file4.
請發布命令的試運行和實際版本,以首先檢查將洗掉哪些檔案夾。
uj5u.com熱心網友回復:
您可能需要一個洗掉子目錄的函式,僅當它不包含您要檢查的一組輸入檔案時。
#!/bin/bash
delete_dir() {
local subdir=$1
local string_of_files=$2
#convert list of files into array
IFS=','
read -ra files_to_keep <<< "$string_of_files"
local list_of_files=()
if [ -d "$subdir" ]; then
for i in $subdir/*; do list_of_files=("${list_of_files[@]}" $(basename $i)); done
local list_of_matched_files=()
for i in ${list_of_files[@]}; do
if [[ " ${files_to_keep[@]} " =~ " $i " ]]; then
list_of_matched_files=("${list_of_matched_files[@]}" "$i")
fi
done
if [ "${#list_of_matched_files[@]}" -eq 0 ]; then
echo "deleting $subdir"
#rm -r $subdir
else
echo "Not deleting $subdir, since it contains files you want to keep!!"
fi
else
echo "directory $subdir not found"
fi
}
# Example1: function call
delete_dir child1 file4
# Example2: For your case you can loop through subdirectories like,
for dir in $(ls -d parent/child*); do
delete_dir $dir file4
done
示例輸出:
$ ./test.sh
Not deleting child1/, since it contains files you want to keep!!
Not deleting child2/, since it contains files you want to keep!!
deleting child3/
如果您可以自由選擇,最好將 python 用于此類操作,因為您可以使其更簡單和模塊化。
uj5u.com熱心網友回復:
假設父目錄只包含子檔案夾,這個單行將列出只包含標記為洗掉串列中的檔案的檔案夾:
for child in *; do if [ "$(ls -1 $child | egrep -v '(file1|file2|file3)'|wc -l)" -eq "0" ]; then echo "would delete $child"; fi ; done
這一行命令將洗掉它們:
for child in *; do if [ "$(ls -1 $child | egrep -v '(file1|file2|file3)'|wc -l)" -eq "0" ]; then rm -rf $child; fi ; done
uj5u.com熱心網友回復:
我想這就是你想要的嗎?如果生成的檔案路徑不包含 file4,則回圈遍歷父目錄中的檔案并洗掉它們
這顯示將被洗掉的檔案
while IFS= read -r -d $'\0' f; do
if [[ "$f" =~ 'file4' ]]; then
continue;
else
echo "rm "$f""
fi
done < <(find parent -type f -print0)
這會洗掉它們
while IFS= read -r -d $'\0' f; do
if [[ "$f" =~ 'file4' ]]; then
continue;
else
rm "$f"
fi
done < <(find parent -type f -print0)
uj5u.com熱心網友回復:
您可以使用內置的 bashcompgen -G和帶有 null/dot/ext 功能的 globing 來測驗每個子目錄。
以下腳本將洗掉parent僅包含file1和/或file2和/或其中的任何子目錄file3:
#!/bin/bash
shopt -s nullglob extglob dotglob
for child in parent/*/
do
compgen -G "$child/*" > /dev/null && {
compgen -G "$child/!(file1|file2|file3)" > /dev/null ||
echo rm -rf "$child"
}
done
shopt -s nullglob extglob dotglob當沒有匹配項時,使 bash 將 glob 擴展為空,啟用擴展的 globing(因此我們可以使用否定),并使*匹配點檔案parent/*/匹配所有目錄parentcompgen -G "$child/*"$child不為空時為真compgen -G "$child/!(file1|file2|file3)"當存在除 file1、file2 或 file3 之外的任何其他檔案時為真$child
測驗用例:
parent/
parent/child1/
parent/child1/file1
parent/child1/file2
parent/child1/file3
parent/child2/
parent/child2/file1
parent/child2/file3
parent/child3/
parent/child3/file1
parent/child3/file2
parent/child3/file3
parent/child3/file4
parent/child4/
parent/child5/
parent/child5/.file5
parent/child5/file2
輸出:
rm -rf parent/child1/
rm -rf parent/child2/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/373438.html
上一篇:搜索兩個字符之間的字串
