Remove/delete not working in bash script

physicalgenius

New Member
Joined
Apr 20, 2020
Messages
3
Reaction score
1
Credits
0
I have a text file(.txt) containing a list of files (one per line) which have to be deleted from a folder on my server. The files to be deleted from the server have to match names of the files in the text file. I wrote a small bash script to do this. Did some research online. When I run the script, it says:
"rm: removing »/var/www/pathToFolder/2011.zip\r“ is not posible: file or folder not found"
But when I execute the rm command and give in the path, it does delete the file.

Bash script:
for file in `xargs < list_of_files_to_be_deleted_1.txt`;
do rm /var/www/projects/path_To_Folder_Having_Both_Text_And_Files_To_Be_Deleted/"$file";
done

then I run "bash delete_files.sh" --> the bash file in found in the same folder

Thanks for any help.
 


Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,400
Reaction score
1,141
Credits
10,108
"rm: removing »/var/www/pathToFolder/2011.zip\r“ is not posible: file or folder not found"
A typo? I don't know but that "\r" after 2011.zip looks out of place to me.
 
OP
P

physicalgenius

New Member
Joined
Apr 20, 2020
Messages
3
Reaction score
1
Credits
0
A typo? I don't know but that "\r" after 2011.zip looks out of place to me.
That is just part of the error message I receive. Any idea why the "rm" command in the bash script does not work. Any better way of doing this?
Thanks
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,464
Reaction score
2,082
Credits
10,249
The \r at the end of each file name means that your text file has MSDOS style cr/lf endings.
Run the command:
Code:
dos2unix /path/to/yourlist.txt

That will convert the line endings in the file to unix style line endings. Then your script should work properly!
 
OP
P

physicalgenius

New Member
Joined
Apr 20, 2020
Messages
3
Reaction score
1
Credits
0
The \r at the end of each file name means that your text file has MSDOS style cr/lf endings.
Run the command:
Code:
dos2unix /path/to/yourlist.txt

That will convert the line endings in the file to unix style line endings. Then your script should work properly!
Hi JasKinasis, you are the man. That was the trick. Thanks a million. Everything now works like a charm. :)
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top