How do I create a new directory called duplicates if a file already exists when renaming in c shell
I have a script that renames files with.JPEG ext to .jpg but if it renames one and it already exists I need it to create a new directory called duplicat开发者_开发技巧es and move the file there instead of overwritting it.
I have not tested this:
foreach j (*.JPEG *.JPG)
set target = $j:r.jpg
if (-e $target) then
mkdir -p duplicates # create directory if it doesn't already exist
mv $j duplicates/$target
else
mv $j $target
endif
end
But consider whether csh is the best tool for this kind of thing:
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
This will allow you to test for the existance of a file:
test -e img.jpg
You can test the existance of a file and if it does exist then move it to the duplicates folder. To create a folder called duplicates do the following:
mkdir duplicates
Hope that helps.
精彩评论