89884

Moving multiple files in directory that have duplicate file names [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/20237133/moving-multiple-files-in-directory-that-might-have-duplicate-file-names" dir="ltr" rel="nofollow">Moving multiple files in directory that might have duplicate file names</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

can anyone help me with this?

I am trying to copy images from my USB to an archive on my computer, I have decided to make a BASH script to make this job easier. I want to copy files(ie IMG_0101.JPG) and if there is already a file with that name in the archive (Which there will be as I wipe my camera everytime I use it) the file should be named IMG_0101.JPG.JPG so that I don't lose the file.

#!/bin/bash if [ $# -eq 0 ] then echo "Usage: $0 image_path archive_path" exit 999 fi if [ -d "$1" ] #Checks if archive directory exists then echo Image Source directory FOUND else echo ERROR: Image Source directory has NOT BEEN FOUND fi if [ -d "$2" ] then echo Photo Archive FOUND else echo Creating directory mkdir "$2" fi if [ find $1 -name "IMG_[0-9][0-9][0-9][0-9].JPG" ] #added this in to be more specific 1/4 then #2/4 for file in "$1"/* do dupefile= "$2"/"$file" while [ -e "$newfile" ]; do newfile=$newfile.JPG done mv "$file" "$newfile" done else #3/4 #do nothing fi #4/4 took all the /4 out, but it's saying theres no such file or directory, even though I've tested it and it says there is.

unexpected token fi is the error I'm getting but the if statement needs to be in there so the specific files i need, are getting moved.

Answer1:

You cannot have an empty else. Take out the else keyword if you don't have an else part.

Also, you should get rid of the superfluous find. Just loop over the files you actually want instead.

for file in "$1"/IMG_[0-9][0-9][0-9][0-9].JPG do newfile= "$2"/"$file" while [ -e "$newfile" ]; do newfile=$newfile.JPG done mv "$file" "$newfile" done done

(This also addresses the dupefile vs newfile error.)

Recommend

  • Automatically assign profile pics to pages in phpfox
  • I just “move” image, and its metadata changes…
  • How to Delete Images from Public/Images Folder in laravel 5 (URL Data)
  • Issues compiling C (Not C++) OpenCV code
  • Fastest way to find item in array/object
  • Mysql Invalid datetime format: 1292 Incorrect datetime value when creating some dummy data for testi
  • Why are some debug symbols missing and how to track them?
  • Precompiling uploaded assets
  • laravel-5.4 - error :Creating default object from empty value
  • Google Closure and Chrome packaged apps: compatibility?
  • How to disable sparse checkout after enabled?
  • How to rotate a PDF file using Ruby
  • Resource for understanding view, projection, 'virtual camera' in OpenGL or graphics in gen
  • C#, Updating a Progress Bar Using Background Worker From a Different Class
  • How to reduce the time delay to reach run method of Runnable class using ExecutorService Java
  • Unable to scan Code 128
  • Exception handling as per java coding standards
  • How can I get a specific version of a dataset row?
  • FragmentActivity with a Fragment Containing a MapView
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • Persistent stty Settings?
  • PHP file_exists() anomaly
  • Always require certain dependencies in RequireJS
  • Phonegap GA plugin not working
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • MVVM: Image Bind Source from FileOpenPicker
  • Galaxy S4 Android Custom Camera Issue
  • Python pandas melting data to multiple columns and coulmn names in another column
  • What does “t” refer to in this SQL?
  • How do I get the list of bad records that didn't load in Bigquery?
  • CERN ROOT exporting data to plain text
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • Why does access(2) check for real and not effective UID?
  • Syntax for setting draggablecursor property in google maps api
  • Q promise. Difference between .when and .then
  • Resize panoramic image to fixed size
  • what is the difference between the asp.net mvc application and asp.net web application