Hello,
I am new to shell scripting and am having some trouble writing a script that will work the way I want it to. I am good at the one liners, but when I try to string them together I get errors.

I have a ftp server that will trigger shell scripts for uploads. What I have accomplished is basic copys from one server to another file server. What I am trying to do is to create a new directory (on the remote server) named with the current date and time, and copy the new uploaded file to it.

If I run the mkdir part by itself it works fine. I just cant seem to get the file to copy into this newly created directory.

This is what I have tried:
(The words in brackets are the tokens for dir path and filename.)

mkdir /Volumes/Jobs-2008/Incoming_JOBS/`date +%Y-%m-%d-%H-%M`
cp <FILE> /Volumes/Jobs-2008/Incoming_JOBS/`date +%Y-%m-%d`/<FILENAME>

Any pointers would be appreciated
thanks.

Hello,

If I run the mkdir part by itself it works fine. I just cant seem to get the file to copy into this newly created directory.

This is what I have tried:
(The words in brackets are the tokens for dir path and filename.)

mkdir /Volumes/Jobs-2008/Incoming_JOBS/`date +%Y-%m-%d-%H-%M`
cp <FILE> /Volumes/Jobs-2008/Incoming_JOBS/`date +%Y-%m-%d`/<FILENAME>

Any pointers would be appreciated
thanks.

First of all, the names of a newly created directory and the copy target directory are different (the former does have hours ind minutes, and the latter does not).
Next, you should not invoke 'date' twice - there's always a risk that the second invocation returns different result.
Therefore:

DATE=`date ... ... ...`
PATH="/Volumes/.../.../.../"${DATE}
mkdir ${PATH}
cp <FILE> ${PATH}/<FILENAME>

First of all, the names of a newly created directory and the copy target directory are different (the former does have hours ind minutes, and the latter does not).
Next, you should not invoke 'date' twice - there's always a risk that the second invocation returns different result.
Therefore:

DATE=`date ... ... ...`
PATH="/Volumes/.../.../.../"${DATE}
mkdir ${PATH}
cp <FILE> ${PATH}/<FILENAME>

Thank you for leading me in the right direction. After some testing I was able to get the results that I want. Now I have run into another problem. he FTP software executes each line of the script as a seperate script. So now I am looking to have the full script triggered by the FTP program and run using input from the program, i.e. the paths, and filenames.

The developer suggested that I use something like this...
have the program have the program pass along the file path and file name:

/path/to/script <FILE> <FILENAME>

then the script would be
MYDATE=`date +%d_%m_%y-%H-%M`
DIREC="/Volumes/Jobs-2008/Incoming_JOBS/"$MYDATE
mkdir $DIREC
cp $1 $DIREC "/" $2


He was not too sure about it, so I guess I am asking if that is the proper format the have the script read in the variables for the file path and file name.

MYDATE=`date +%d_%m_%y-%H-%M`
DIREC="/Volumes/Jobs-2008/Incoming_JOBS/"$MYDATE
mkdir $DIREC
cp $1 $DIREC "/" $2

Path and name are passed as the command line arguments, and the script accesses them as positional parameters 1 and 2. $1 and $2 in the code above are their respective values.

PS: Make sure there's no spaces around "/" at line 4.

Edited 6 Years Ago by nezachem: n/a

Hello Friends.
I read your entire post it is too good well your answer is that here so read carefully
Path and name are passed as the command line arguments, and the script accesses them as positional parameters 1 and 2.
$1 and $2 in the code above are their respective values.

Here is the script I have refined to do what I had set out to do. This copies the files to a new directory, and timestamps them. With the $1-filepath, $2-customer name and $3-filename being supplied by the ftp program.

MYDATE=`date +%d.%m.%y`
MYTIME=`date +%H.%M`
DIREC="/Volumes/Jobs-2008/Incoming_JOBS/"$MYDATE

mkdir $DIREC

cp $1 $DIREC"/"$2"."$3"."$MYTIME

What I would like to try now is to only create a directory for the day if it does not exist. I thought I could use the && command but then realized I might need to use an if then statement. Im having trouble with the format of this.

What do i compare the folder name to so that it will only create one folder per day?

If
folder (date) does not exist
then mkdir....
fi
cp files to folder

What I would like to try now is to only create a directory for the day if it does not exist. I thought I could use the && command but then realized I might need to use an if then statement.

I wouldn't recommend it. There's no harm in creating an existing directory other than an error message, which is easily suppressed with -p option. Testing complicates the script and in pathological cases may lead to a race condition.

Oh great! I did not realize that I could do it that way. I guess I was over-thinking it. Thanks a lot.

This article has been dead for over six months. Start a new discussion instead.