How to move files based on filetype and time created?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to move files based on filetype and time created?
# 1  
Old 06-13-2011
How to move files based on filetype and time created?

Hi,

I'm trying to improve my Unix skills and I'm wondering what is the best way to move some files based on filetype and attributes like time created?

For instance, lets suppose I have a directory with many different files in it and I'd like to move all the jpgs that were created between May 30th and Jun 3rd to another folder? What is the best way to achieve this and why?

Thanks!
# 2  
Old 06-13-2011
Top of my head solution, relies on epoch date to position the creation time, (usual caveat about inode change versus actual creation time)

Code:
for i in $(file * |grep JPEG| cut -d\: -f1);do 
   export i;
   ctime=$(perl -e ' @details = stat $ENV{i};print $details[10]'; 
   if [ $ctime -gt 1306710000  -a $ctime -lt 1307055600 ] 
      mv $i newFolder;
   fi
done

I chose this method because epoch time seemed the easiest route to creating a time window and Perl's stat utility returns the ctime in epoch seconds (I'm sure there are other utilities which could do the same, however when the only tool you have is a hammer every problem looks like a thumb Smilie )

Last edited by Skrynesaver; 06-13-2011 at 11:59 AM..
# 3  
Old 06-13-2011
Skrynesaver, thanks for the response.

Do you mind helping me decipher this code for a moment:

Code:
for i in $(file * |grep JPEG| cut -d\: -f1);do 
export i; ctime=$(perl -e ' @details = stat $ENV{i};print $details[10]'; if [ $ctime -gt 1306710000 -a $ctime <1307055600 ]
mv $i newFolder;
fi
done

I added indents to make it more readable.

Code:
for i in $(file * |grep JPEG| cut -d\: -f1);do

For each filename i in ( grep JPEG ) // What does "cut -d\: -f1" do?

Code:
export i;

Load the filename into variable i?

Code:
ctime=$(perl -e ' @details = stat $ENV{i};print $details[10]';

Extract the file attributes, parsing for creation time and load into variable ctime?

Code:
if [ $ctime -gt 1306710000 -a $ctime <1307055600 ]

If that creation time is > May 30 && < June 2nd

Code:
mv $i newFolder;

Move the file

Is that right?
# 4  
Old 06-13-2011
Code:
for i in $(file * |grep JPEG| cut -d\: -f1);do

Run the file command on each file and if the result contains the string "JPEG" use cut to extract just the file name.

try running file on a JPEG file and look at the output, using the cut command with the delimiter set to ":" and taking only the first field effectively extracts the filename from the result, however we needed the result to determine if the file was a JPEG or not.

Code:
export i;

Make the variable i available in sub-shells (ie. the Perl shell we are about to launch)

Code:
ctime=$(perl -e ' @details = stat $ENV{i};print $details[10]';

Extract the file attributes, parsing for creation time and load into variable ctime? Bingo, using Perl's stat command we get the creation time in epoch seconds (perldoc -f stat for more info on the details of the stat command)

Code:
if [ $ctime -gt 1306710000 -a $ctime <1307055600 ]

If that creation time is > May 30 && < June 2nd

Code:
mv $i newFolder;

Move the file

Is that right? yup, you got it.

Last edited by Skrynesaver; 06-13-2011 at 10:53 AM..
This User Gave Thanks to Skrynesaver For This Post:
# 5  
Old 06-13-2011
Final Code

For anyone interested, here's the final working code for the shell script. I've included comments to explain whats going on.

Code:
#! /bin/sh

####################
# Since filenames have spaces, first need to run these two statements to prevent delimiter errors:

# Back up default IFS to be restored later
SAVEIFS=$IFS

# Set IFS variable to account for spaces in filenames
IFS=$(echo -en "\n\b")

####################
# Main Loop - For each jpg, use perl stat() to extract the file's create-time attribute and if within bounds, move to new folder

# For each element f in "*.jpg"
for f in *.jpg
do
	
	# Make $f available in perl shell ( interface between bash <-> perl )
	export f;

	# Use the perl stat() function to obtain file created-time attribute:
	  # 1. Use wrapper $ENV{f} so $f it can be resolved in Perl, then use it in the stat() function:  stat ( $ENV{f} );
	  # 2. Stat() returns an array of file attribute values which is stored in @temp:  @temp = stat (...);
	  # 3. Output the 10th element of array temp:  print @temp[10];
	  # 4. Output from perl script is saved as variable ctime:  ctime=$(...)

	ctime=$( perl -e '@temp = stat ( $ENV{f} ); print @temp[10];' );

	# If $ctime is > May 30th and < Jun 2nd
	if [ $ctime -gt 1306713600 ] && [ $ctime -lt 1306972800 ]
	then
	
		# Move the file named $f to folder
		mv "$f" newfolder
	fi
done

# Restore IFS variable back to default
IFS=$SAVEIFS

# 6  
Old 06-14-2011
Only query I'd have is what you do about jpeg files named .jpeg, .JPG etc...
Using the "file" command allows you check the contents of the file (or at least the magic number at the start of the file) to verify that the file is a jpeg
# 7  
Old 06-14-2011
Yes, I tried that first with grep but I was also getting some strange other file types that had JPEG in the meta data. Thats why I switched to *.jpg to get only files with the jpg extension.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move files with a certain suffix based on how many files are in another folder

Hello, First time poster. I am looking for a way to script or program the process of moving files from one folder to another, automatically, based on the count of files in the destination folder. I was thinking a shell script would work, but am open to the suggestions of the experts... (6 Replies)
Discussion started by: comtech
6 Replies

2. Shell Programming and Scripting

Segregate files based on the time they are created

Hi All, I have scenario where I need to zip huge number of DB audit log files newer than 90 days and delete anything older than that. If the files are too huge in number,zipping will take long time and causing CPU spikes. To avoid this I wanted to segregate files based on how old they are and... (2 Replies)
Discussion started by: veeresh_15
2 Replies

3. Shell Programming and Scripting

Files created on specific time

Hello Gurus, I am facing one issue to get a file for a specific time. There are about 300 files created between 6.30 pm to 7.15 pm everyday. Now I wanted only the file which is created on 6.45pm. No other files required. I have used "find" command to get the files, but not getting the expected... (3 Replies)
Discussion started by: pokhraj_d
3 Replies

4. Shell Programming and Scripting

Copy files based on last created date

Hi, I have a requirement to copy files from a windows network drive to a Linux server using shell script based on the last created date. Ex: FileName CreatedDate/Time F1 05-01-2012 3:00 PM F2 05-01-2012 3:15 PM F3 05-01-2012 2:00 PM When i run the shell script... (1 Reply)
Discussion started by: Lee_10
1 Replies

5. Shell Programming and Scripting

Display files created on particular date & time

Hi , I have BASH system & i am trying to display the files created on a particular date and time, and after displaying those files I also want to delete all those files.Can anyone of you help me out for this............. Thanx Original post contents restored... Please do not erase the question... (3 Replies)
Discussion started by: rakeshtomar82
3 Replies

6. Shell Programming and Scripting

Removal of files/folders created during a period of time

Hi, I have some folders (containing files) which gets created daily and names start with 'TT_' . I would like to remove these folders every month end. Could you please provide me the commands and also the syntax to schedule through crontab. Thanks, Archana (3 Replies)
Discussion started by: archana.n
3 Replies

7. Fedora

Move file based time stamp

Hi all, I've already tired to try to solved this problem. Also search in Internet didn't find anything solution I have a directory like this : # pwd /opt/projects/juventini # ls -al | more total 3627460 drwxr-xr-x 2 app apps 12472320 Sep 24 14:59 . drwxr-xr-x 11 app apps 4096 Jun... (8 Replies)
Discussion started by: sunardo
8 Replies

8. UNIX for Dummies Questions & Answers

Help with command to find all newly created files in a given time period

Hi all! Can someone please help me create a command to accomplish the following task. I have a parent directory called ex. /var/www/parent and it has a bunch of sub-directories called /var/www/parent/1, var/www/parent/1/xyz/ and etc. What I would like to do is to count the number of files... (2 Replies)
Discussion started by: bbzor
2 Replies

9. Shell Programming and Scripting

Copying files created after a specified date/time

I need to write a script that copies files from one directory to another that were created after "today 6:30". This script will be NOT be ran at the same time each day. any help is appreciated. (2 Replies)
Discussion started by: jm6601
2 Replies

10. Shell Programming and Scripting

List files created between specific date and time

Hi I need to write a script to list files in a directory created within specific date and time for eg list files created between Apr 25 2007 11:00 to Apr 26 2007 18:00. and then i have to count them Any suggestions pls ? (3 Replies)
Discussion started by: jazjit
3 Replies
Login or Register to Ask a Question