Renaming script after downloading

Come up with a useful post-processing script? Share it here!
Post Reply
svendaems
Newbie
Newbie
Posts: 3
Joined: February 11th, 2012, 1:12 pm

Renaming script after downloading

Post by svendaems »

Hello

I'm trying to rename the folders that I've downloaded by adding a date-time stamp.

I've following script

Code: Select all

#!/bin/sh
mv -f $1 "date +%Y-%m-%d %H:%M:%S $1"
When I download a test file, I get following error
Exit(1) mv: can’t rename ’/volume1/Downloads/test’: No such file or directory
Any idea?
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Renaming script after downloading

Post by shypike »

I suppose you are new to scripting?
First of all $1 is the full path of the folder, so /aaaa/bbb/ccc/name.
Next, you need to add quotes. The date command doen't work like you're using it.
What you're probably after is something like this:

Code: Select all

#!/bin/sh
# go to folder
cd "$1"
# go one down
cd ..
# Create date stamp, careful: there are single quotes and back-ticks!
MYDATE=`date '+%Y-%m-%d %H:%M:%S'`
# rename, where $3 is the plain name without the full path
mv "$3" "$MYDATE $3"
svendaems
Newbie
Newbie
Posts: 3
Joined: February 11th, 2012, 1:12 pm

Re: Renaming script after downloading

Post by svendaems »

Yes, I'm quite new to it ;-)

But I want to rename the complete folder where it was downloaded in, not only the file. I've added the quotes but still the same error

Code: Select all

#!/bin/sh
mv -f "$1" "date +%Y-%m-%d %H:%M:%S $1"
mv: can't rename '/volume1/Downloads/test': No such file or directory
User avatar
shypike
Administrator
Administrator
Posts: 19773
Joined: January 18th, 2008, 12:49 pm

Re: Renaming script after downloading

Post by shypike »

What you want is impossible, you cannot put the date ahead of /volume1.
The first place where you can start is after /volume1/Downloads.
Also you're still using the date command wrong.
svendaems
Newbie
Newbie
Posts: 3
Joined: February 11th, 2012, 1:12 pm

Re: Renaming script after downloading

Post by svendaems »

super, it works, thank you very much!
Post Reply