开发者

How do I create a new directory called duplicates if a file already exists when renaming in c shell

开发者 https://www.devze.com 2023-04-11 01:40 出处:网络
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 instea

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号