0

I am trying remove files by names according this answer How to delete files listed in a text file

But get following error:

rm: cannot remove 'var/Resources/Images/fd29d33d.jpg'$'\r': No such file or directory

I understand that every line in txt file misses an / at the start and something wrong with end of lines: \r in the end (except last file). How can I change this command xargs rm < file to work properly with my file? File contains about 8kk lines

It is not a homework, I am not an admin and need this to be done.

Mateech
  • 131
  • 5
  • 1
    Did you create your text file on Windows? it looks like it has DOS-style (CRLF) line endings – steeldriver May 24 '22 at 15:04
  • @steeldriver no, it has been generated by program, microsoft .net btw – Mateech May 24 '22 at 15:05
  • 1
    So ... not Windows but Microsoft ... regardless, either fix the program to use the appropriate EOL, or fix the file by removing the carriage return characters. See for example [How to change Windows line-ending to Unix version](https://askubuntu.com/questions/803162/how-to-change-windows-line-ending-to-unix-version) – steeldriver May 24 '22 at 15:16
  • @steeldriver thanks, will try. How can I add `/` in front of every line also? – Mateech May 24 '22 at 15:19
  • 1
    That's probably not necessary provided that all the paths are rooted at the same directory (e.g. `/`) - you'd just need to `cd` to that directory first so that they are all valid relative pathnames. – steeldriver May 24 '22 at 15:40

1 Answers1

1

In order to add a / at the beginning of each file, and to remove the trailing characters after the first ', you can try this command instead of xargs rm < file:

sed 's/^/\//' file | sed "s/'.*$//" | xargs ls

If the above command correctly lists the files, you must change ls to rm so that the files are actually deleted, this way:

sed 's/^/\//' file | sed "s/'.*$//" | xargs rm

I hope this helps.