Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

shell - How to remove semi-duplicate directories in Bash?

How to delete folders that might-be semi duplicated?

E.g., there can be two versions A and B of each folderName. Either A, B or both A and B:

B_folderName1
A_folderName2
A_folderName3
B_folderName3 

I want to choose preferred version A and delete B, or just keep B if A is not available. Following example above the expected output is:

B_folderName1
A_folderName2
A_folderName3

Thanks!

question from:https://stackoverflow.com/questions/65931320/how-to-remove-semi-duplicate-directories-in-bash

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Loop through B_*, and delete each of those for which a corresponding A_* exists.

for d in B_*/; do
  if [[ -e A_${d#B_} ]]; then
    echo rm -r "$d"
  fi
done

Drop echo if the output looks good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...