I'm trying to accomplish a simple task using the excellent post here: I would like to remove all files from a folder except those listed in a .txt file contained in the folder. The syntax works fine, but for some reason all files are removed - basically the script is saying that there are no correspondences between the filenames in the folder and those in the .txt file - but I can see that there are!
I thought maybe there might be some invisible white space after my file names that I cannot see (in the folder or the .txt file). But I don't think so...Is there another solution that I am not thinking of?
ls -p | grep -v / | sed 's/\<exclude.txt\>//g' | sort | comm -3 - <(sort exclude.txt) | xargs
this will only output without removing. To remove runls -p | grep -v / | sed 's/\<exclude.txt\>//g' | sort | comm -3 - <(sort exclude.txt) | xargs rm
– Raffa Oct 25 '19 at 23:41ls -p | grep -v / | sed 's/\<a.txt\>//g' | sort | comm -3 - <(sort a.txt) | xargs
No it is just a one line command that is easier and will do the job. I replaced exclude.txt with a.txt assuming this is the name of your list. If you run this command, it will only show you the output. If that is what you want, appendrm
after adding one space at the end to remove the files. – Raffa Oct 25 '19 at 23:55\n
is okay and should be there if you runod -c a.txt
but\r
is not and shouldn't be there. – Raffa Oct 26 '19 at 00:14nano
editor. Copy and paste the lines one by one and press Enter once after each line except the last line no Enter after it. This should work. Good Luck – Raffa Oct 26 '19 at 00:20