LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-14-2007, 08:33 AM   #1
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Rep: Reputation: 18
Advice? Best way to move files daily to a daily "date" named directory


Hi, I was wondering if I can get some suggestions (and maybe some help!) on this issue I'm going to describe:

I have webcam that uploads pictures, which are captured whenever there's movement, to my server (no, not a girl's lockerroom, but the driveway in a countryhouse).

I generate about 30 pictures a day, and I'd like to keep them for about 3 months, grouped by date [daily] (i.e., was that bill for snowplowing really just a scam?). Currently, the files from the webcam all upload to the root directory. I'm not sure about the best way to do this, so I was hoping to get suggestions.

Ultimately, I thought I'd put the various commands into a script, like

mkdir temp_date
mv *jpg temp_date
mv temp_date `date +%Y%m%d`

and then I'd run that as a cron job, every day, at like 12.10am.

However, I'm not sure how to write the script: I tried to do like above, and then name the script picbackup.sh, but that doesn't work. I know I'm doing this wrong, so any suggestions would be greatly appreciated. Thanks very much!!!

Last edited by ziphem; 04-14-2007 at 08:36 AM.
 
Old 04-14-2007, 09:16 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
Assuming it ran in the directory where your *jpg files existed (root you say) the script would work. The reason it doesn't work is likely because of assumptions like that and that it knows where to get the commands (mv, mkdir, date).

When you are logged in you have an "environment" that includes things such as CWD, PATH, TERM and various other things. It is PATH that lets you know where to find commands. CWD is current working directory. Typically on login your CWD is your HOME directory. This environment is created by your /etc/bashrc, /etc/profile, $HOME/.profile, $HOME/.bash_profile, $HOME/.bashrc and/or other files.

However when you run a script in cron it does not get that "environment" but rather a much more rudimentary environment because it isn't "logging in" so doesn't execute all the above files.

Accordingly your script should contain most of that information:

e.g.
Code:
/bin/mkdir /temp_date
/bin/mv /*jpg /temp_date
/bin/mv /temp_date /`/bin/date +%Y%m%d`
So you see it is told exactly where to find the 3 commands it is using (/bin - not all commands are there just happens all 3 of these are). It is also told to use "/" (root) directory as part of path for the directories you're creating.

Another assumption to address is that it is using the shell you are. In fact it uses a basic shell so when writing scripts it is a good idea to include an intepreter line to tell it what shell or interpreter you want it to run as. So to run it as bash:

Code:
#!/bin/bash
/bin/mkdir /temp_date
/bin/mv /*jpg /temp_date
/bin/mv /temp_date /`/bin/date +%Y%m%d`
Note that the "#!" is a special syntax - normally "#" means comment (lines that don't get executed) but "#!" at beginning of first line means to use what follows on the line to execute the remainder of the script.

You don't need to do the two moves however. Your script could have one less step:

Code:
#!/bin/bash
/bin/mkdir /temp_date
/bin/mv /*jpg /`/bin/date +%Y%m%d`
You might also not want to use / as your directory for storing pictures. This is the root directory and cluttering it up with other files can make diagnosing system issues later a little difficult.

e.g. mkdir /jpegs then use that for your daily captures then change the script to:
Code:
#!/bin/bash
/bin/mkdir /jpegs/temp_date
/bin/mv /jpegs/*jpg /jpegs/`/bin/date +%Y%m%d`
 
Old 04-15-2007, 09:03 AM   #3
ziphem
Member
 
Registered: Feb 2004
Location: US / EU
Distribution: Fedora 20
Posts: 129

Original Poster
Rep: Reputation: 18
Hi, this advice was wonderful, I got it running perfectly. Just to quickly note, I did incorrectly say I was running it at root, I really meant to say at the base of one of the domain's directories, not the actual server's root. Again, thanks a million for the help!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE deleted my files! A "copy to" tale of woe ChemBot Linux - General 1 08-27-2005 02:05 AM
daily mail "Security-Report" Fle>< Linux - Security 8 01-06-2005 10:16 AM
rm files named as "-xxxxxxx" kaon Linux - General 2 09-12-2004 02:43 AM
empty files named "1" owned by root ??? kkempter Linux - General 3 07-09-2003 03:07 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 10:29 PM


All times are GMT -5. The time now is 08:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration