Issue
I have a file having the list of directory name I want to keep. Say file1 and its contents are names of directories like
My directory (actual directories) on the other hand has directories like
- dir1
- dir2
- dir3
- dir4
- dirs
What I want to do is delete dir4, dirs and other directories of which their name doesn't exist on file1 from My directory. file1 has a directory name per line. There might be sub directories or files under dir4 and dirs which needs a recursive deletion.
I can use xargs to delete the files in the list within My directory
xargs -a file1 rm -r
But instead of removing, I want to keep them and remove the others which are not on file1. Can do
xargs -a file1 mv -t /home/user1/store/
And delete the remaining directories in my directory but I am wandering if there is a better way?
Thanks.
Solution
find . -maxdepth 1 -type d -path "./*" -exec sh -c \
'for f; do f=${f#./}; grep -qw "$f" file1 || rm -rf "$f"; done' sh {} +
Answered By - Eugeniu Rosca Answer Checked By - Gilberto Lyons (WPSolving Admin)