LinuxQuestions.org
Register a domain and help support LQ
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 12-19-2005, 10:08 AM   #1
timothee
LQ Newbie
 
Registered: Jan 2005
Location: Singapore
Distribution: Gentoo / Mepis
Posts: 10

Rep: Reputation: 0
Shell question: moving files into subdirectories by dates


Hi there,

I want to build myself a shell script to transfer all files under directory "A", to directories "B/date_of_the_file", where "date_of_the_file" is the date, in format YYYY_MM_DD, of the creation date of the file. I'm sure it can't be very complicated but I do not know where to begin. Any pointer would be much appreciated.

Thanks in advance,
Tim
 
Old 12-19-2005, 10:59 AM   #2
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 30
Quote:
Originally Posted by timothee
...

I want to build myself a shell script to transfer all files under directory "A", to directories "B/date_of_the_file", where "date_of_the_file" is the date, in format YYYY_MM_DD, of the creation date of the file.
You can't get the actual creation time of a file but
but
Code:
stat -c %y filename
returns the last modified time like this: 2005-11-11 18:52:57.000000000 -0600

This is fairly easy so I'll let you work it out.

Hint: use a for loop; cut and command substitution to process the stat output.

Also use 'echo' instead of mv or cp to see exactly what happens before you destroy something important

Post if you get stuck.
[Slight edit to clarify sentence.]

Last edited by vls; 12-19-2005 at 11:26 AM.
 
Old 12-20-2005, 07:46 AM   #3
timothee
LQ Newbie
 
Registered: Jan 2005
Location: Singapore
Distribution: Gentoo / Mepis
Posts: 10

Original Poster
Rep: Reputation: 0
Hi Vls,

Thanx, that helped. I've figured it out and here is my very first shell script . Any comments would be most welcome.

Code:
#!/bin/sh
#
if [ ! $# -eq 2 ]
then
	echo Usage is \"move_around.sh origin_folder destination_folder\"
	exit
fi

# origin folder must be a readable directory
if [ ! -d $1 ]
then
	echo $1 is not a directory
	exit
fi

if [ ! -r $1 ]
then
	echo $1 is not readable
	exit
fi

# destination folder must be a writable directory
if [ ! -d $2 ]
then
	echo $2 is not a directory
	exit
fi

if [ ! -w $2 ]
then
	echo $2 is not writable
	exit
fi

# move files now
for of in `find $1 -iname *.jpg` ; do
	d=`stat -c %y $of`
	d=`echo $d | cut -c1-10 | tr "-" "_"`
	mkdir -p "$2/$d"
	mv "$of" "$2/$d/"
done
I wanted to use the destination file name explicitely but I couldn't figure out how to extract the filename from the full path in my variable $of, can it be done?
 
Old 12-20-2005, 08:29 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Have a look at the
"basename"
command.
 
Old 12-20-2005, 08:34 AM   #5
timothee
LQ Newbie
 
Registered: Jan 2005
Location: Singapore
Distribution: Gentoo / Mepis
Posts: 10

Original Poster
Rep: Reputation: 0
Ha yes, exactly what I wanted. Thanx
 
Old 12-20-2005, 10:14 AM   #6
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 30
Thumbs up

Not too shabby. Now you get to tinker with it for the rest of your life.
 
  


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
Simple console question. Moving files. The_Messiah Linux - Newbie 3 08-17-2005 11:51 PM
Simple console question. Moving files. The_Messiah Linux - General 1 08-17-2005 08:51 PM
Unzipping all .gz files in all subdirectories darin3200 Linux - General 10 04-24-2004 09:58 PM
delete files / keep subdirectories? lhorstman Linux - Newbie 2 01-10-2003 03:01 PM
Script For Moving Files In Subdirectories darkmage Linux - General 3 12-17-2002 06:34 PM


All times are GMT -5. The time now is 12:10 AM.

Main Menu
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