Remove/delete not working in bash script

physicalgenius

New Member
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.
 


JasKinasis

Well-Known Member
Credits
1,193
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!
 

physicalgenius

New Member
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. :)
 


Staff online

Members online


Top