Ubuntu – Invisible space when trying to remove file (but exclude all files in a list)

bashcommand linermscripts

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?

Best Answer

It seems that your a.txt file contains hidden characters due to possibly being created or edited on other OS's like Windows.

You need to sanitize the file first in order to use it without syntax errors.

The easiest way is to use a tool called dos2unix. Please follow these steps to do so:


Firstly: Install dos2unix by opening a terminal and running the following command:

sudo apt install dos2unix

Secondly: Back up your a.txt file.

Thirdly: Use dos2unix on your file by running the following command in the terminal from within the directory containing your a.txt file:

dos2unix a.txt

Finally: Test your file. It should be fine now.