1

I need to delete some files in a directory using a list. For example I have this file list:

list.txt:

0NM.pdb
POR.pdb
0UA.pdb
AU7.pdb

In /home/my_directory/ I have this file

U6Y.pdb
0NM.pdb
POR.pdb
AR2.pdb
0UA.pdb
AU7.pdb

At the end in the directory i should have only this:

U6Y.pdb
AR2.pdb

I have show some other discussion for this problem and I try to use some different script but all gave me the same result:

rm: impossibile rimuovere "ONM.pdb\r": File o directory non esistente
rm: impossibile rimuovere "POR.pdb\r": File o directory non esistente
rm: impossibile rimuovere "OUA.pdb\r": File o directory non esistente
rm: impossibile rimuovere "AU7.pdb\r": File o directory non esistente

I try to use these different script:

-> printf "%s\n" $(<list.txt) | xargs -I@ rm @

-> while read name; do rm "$name"; done < /home/list.txt

-> while read -r filename; do rm "$filename"; done </home/list.txt

-> xargs -a /home/list.txt -d'\n' rm

Why all these give me the same error? Why does "\r" appear at the end of the file names to be deleted?

  • 1
    It seems like the list is created with windows? Can you may exchange the end of line characters? – mstruebing Nov 06 '20 at 14:53
  • How I can do that? I use the comand "chmod +x list.txt" but I have the same result – Tommaso Palomba Nov 06 '20 at 14:56
  • 2
    Try running [`dos2unix`](https://linux.die.net/man/1/dos2unix) on the `list.txt` before running the `rm` commands – 0stone0 Nov 06 '20 at 14:56
  • OK! Thanks, now thwe script works well! – Tommaso Palomba Nov 06 '20 at 15:00
  • 1
    yes, `dos2unix list.txt` should solve this problem. Voting to close as a typo. If you going to continue to use `bash`, please book mark https://stackoverflow.com/tags/bash/info and review often the section labeled *Before asking about problematic code*. Good luck. – shellter Nov 06 '20 at 15:02

1 Answers1

1

The newline character in Windows is \r\n, and in Linux is \n. Linux cannot recognize the \r identifier and will treat it as part of the text content.

You can use dos2unix to convert it permanently (dos2unix list.txt) or you could do it on the fly by setting IFS=$'\r':

Example:

while IFS=$'\r' read -r filename || [[ $filename ]]
do
    echo "do something with $filename"
done < list.txt
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Ming
  • 26
  • 1
  • 1
    Mention how to use the `dos2unix` or some alternative command to fix this issue. Possibly recreate one of the scripts OP tried to use with your fix included. – Lenna Nov 06 '20 at 15:03
  • 1
    If you use `IFS=$'\r'`, then you don't need the `dos2unix`; IFS characters on the end of your string get stripped by `read`. (I don't advise having spaces or tabs in IFS in this context: If a filename _really has_ trailing spaces, it's a bug to not be able to read it from your data file). – Charles Duffy Nov 06 '20 at 15:13
  • @TedLyngmo, what "new version"? You get that ("that" being compatibility with both text file formats) even with `IFS=$'\r' read -r filename; do ...; done – Charles Duffy Nov 06 '20 at 15:18
  • @TedLyngmo, ...and having `IFS=$'\n'` is completely pointless. `$'\n'` is the default argument to `read -d`, telling it what character to _stop reading at_ if `-d` isn't overridden. The thing it stops reading at can't possibly show up inside a record, so it'll never show up when the record is being broken down into fields. – Charles Duffy Nov 06 '20 at 15:18
  • 1
    Actually, let me revise that. `while IFS=$'\r' read -r filename || [[ $filename ]]; do ...; done – Charles Duffy Nov 06 '20 at 15:20
  • @CharlesDuffy I just noticed that when creating a dos file with notepad :) Did I get it right this time? – Ted Lyngmo Nov 06 '20 at 15:24
  • Yup, looks good to me. – Charles Duffy Nov 06 '20 at 15:42