Mass-generating random file names

After setting up Bluetooth on my phone and laptop, I was faced with another problem: the phone saves images using filenames of the form ImageXXX.jpg — which is OK the first time, but tends to conflict with older files later on as the "XXX" counter restarts from 0. One may think that "mktemp" is a solution, but unfortunately that command won't let us choose the extenion of the created file. Instead, on Ubuntu and Debian, use "tempfile":

cd /mnt/Memory\ card/Images/
for f in *; do mv $f `tempfile -d $DEST_DIR --suffix=.jpg`; done

tempfile will create unique file names in the destination directory and avoid race conditions at the same time (not really an issue in this case, but good to know.)

Update: here's a different method that will generate more meaningful (though not random) names

t=`date -u '+%F_%H-%M'`
for f in *; do cp $f $DEST_DIR/${t}_$f; done

The files will be prefixed with a timestamp, which is probably more useful.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.