Rename files inside directory on the fly

2

I've set up dropbox on my headless 12.04 server machine, it's synced properly and I have it setup so that I can email photos using sendtodropbox.com app. These all sync in to the dropbox folder locally on my host ~/Dropbox/Attachments/.

I'd like to do two things:

  1. I'd like to be able to email my dropbox photos, then have those files sync down to my server.

  2. Then copy those files over to the blog's photo repository folder.

The problems I'm running into are:

  • I can make a symlink to a folder outside the Dropbox folder (to my blog's photo repository folder), the only problem is, photo's are emailed in with one file name, photo.jpg (thanks Apple!).

This means, I need a bash script to rename files named photo.jpg to something unique when they are created.

This will allow me upload more than one photo, and be able to differentiate which pictures are which!

I can create a symlink easy enough, I'm just struggling to find a solution that can change the file's names in real time as they are synced through dropbox down to h

knishka

Posted 2012-08-22T23:09:10.773

Reputation: 23

You can email stuff to your DropBox account using SendToDropBox.

– martineau – 2012-08-23T00:54:48.853

Right, I mentioned that in the post :] – knishka – 2012-08-23T01:42:16.390

With SendToDopBox the attachments would not all have the same file name. – martineau – 2012-08-23T08:23:46.510

You're not able to change the file name with sendtodropbox, you can only attach one image at a time, and the only thing that can be modified is the folder they're copied in to. – knishka – 2012-08-23T10:02:09.533

Answers

0

The easiest way to do this would be to have a cron job renaming your files. Assuming you don't need instant access to your photos and can deal with waiting a minute (have a look here if not).

  1. Make a script that moves photos.jpg from your dropbox folder to your repository, saving it as year-month-day_hour_minutes_seconds.jpg. Create a text file called move_photos.sh in your favorite text editor and paste these lines into it:

    #!/bin/bash
    date=`date +%F"_"%H_%M_%S`;
    mv /Dropbox/Photos/photo.jpg /path/to/repo/$date.jpg
    

    Obviously, replace /path/to/repo with the actual path of your repository.

  2. Make the script executable:

    $ chmod +x move_photos.sh
    
  3. Create a crontab

    $ crontab -e
    
  4. Add this line to the crontab:

    * * * * * ~/move_photos.sh
    
  5. Save it and close the crontab editor window.

This will cause the move_photos.sh script to be run every minute. Since the filename includes seconds, there should never be two files with the same name.

terdon

Posted 2012-08-22T23:09:10.773

Reputation: 45 216

This is awesome, I figured there would be a way to do it with cron like this, check out my answer, I dug up a tool called incron which does this instantly. Although I'm still struggling with it to get it working >.< – knishka – 2012-08-23T03:46:55.640

0

I've found an interesting tool called incron, so I set it up by running sudo apt-get install incron. To put it simply, incron is:

This program is an "inotify cron" system. It consists of a daemon and a table manipulator. You can use it a similar way as the regular cron. The difference is that the inotify cron handles filesystem events rather than time periods.

Next I created a simple bash script:

#!/bin/bash
ls photo.JPG | while read a; do mv $a "$(echo "$a" | sed s/photo.JPG/"photo$(date +%a%b%d%H%M).jpg"/)"; done
mv photo* ~/Dropbox/Attachments /<photo repo>/

I place the script in ~/, then edit the /etc/incron.allow file to include my user name because these scripts and files are all in my home dir. Next I have to edit the incron config file by running incrontab -e.

In here I used ~/Dropbox/Attachments/ IN_MOVED_TO ~/script.sh (because the action of a Dropbox file entering the directory is a move)

This is working instantaneously when I email my Dropbox a picture from my phone now. I also tested incron with both root and my user name to make sure it worked, my first test as my user was: incrontab -e add the config: ~/ IN_CREATE touch ~/test, I then go to ~/, and run >foo, and test appears.

Otherwise, this should work just fine too!

knishka

Posted 2012-08-22T23:09:10.773

Reputation: 23