Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux - Scripting, Please help

Status
Not open for further replies.

nrastogi

Programmer
Jul 19, 2002
52
US
Hi,

I need to move files from one directory to another. There are sub-directories under AA directory. These subdirectories are named as 2 digit number. I need to read all these 2 digit name directories for a certain date range and move those files to /home/arc directory (parent, no subdirectory). There will be other directories as well, but I like to read only the ones that have naming convention of 2 digits only.

Also, I would like to put any error handling as well, in case if a directory is empty or doesnot have files for a certain date, then skip and move rest of the set of files. Are these any other sort of error handling you would when writing such shell scripts, please let me know.

========================================
cd /home/AA/10

mv *080706* /home/arc
mv *080806* /home/arc

cd /home/AA/12
mv *080706* /home/arc
mv *080806* /home/arc

cd /home/AA/45
mv *080706* /home/arc
mv *080806* /home/arc

cd /home/AA/82
mv *080706* /home/arc
mv *080806* /home/arc

========================================


In addition to this, if this can be done using cron, let's say delete all files those are 7 days old.


Thanks for all help.
Nick
 
How about...
Code:
mv /home/AA/[0-9][0-9]/*080706* /home/arc
mv /home/AA/[0-9][0-9]/*080806* /home/arc
To move only files that are older than 7 days...
Code:
find /home/AA/[0-9][0-9]/* -type f -mtime +7 -exec mv {} /home/arc \;
Or something like that.
 
Hi Sam,

I like to automate the process so I don't have to manually type the directory name [0-9] [0-9].

There can be other sub-directories other that 2 digit directories. So, I like to disregard any other directory that is not named as 2 digit. I only like to move files from 2 digit directories under /home/AA/ directory.

Also, I like to be able to input a date range to delete files. FromDate and ToDate. Once that is input, go through the directories under /home/AA. When found a 2 digit numeric directory. Go into that directory and move files that contains date as provided in date range parameters, to /home/arc directory. When all the files are moved under that directory, then continue to look for next 2 digit named directory and keep going until moved all.

Hope this helps. Please let me know how this can be done.

Regards,
Nick
 
That will automatically look only in two digit named directories under [tt]/home/AA[/tt]. The pattern "[tt]/home/AA/[0-9][0-9]/*[/tt]" will only match files in two digit named directories under [tt]/home/AA[/tt].

And to make it select on a date range, you can still use "[tt]-mtime[/tt]", just have two of them, one with the least number of days (i.e. "[tt]-mtime +3[/tt]"), and one with the most number of days (i.e. "[tt]-mtime -10[/tt]").

To give it dates to select by, you'll have to write a shell script to do the testing.
 
Hi Sam,

It worked! I tested it using cp (i am sure mv will work too).

But, how do I preserve date and time of the files using this command. When I run it to copy to home directory, it creates (copies) with today's date.

I like to keep the old (original) date (and time).

Please suggest, THanks again for all help.

Nick


find /home/AA/[0-9][0-9]/* -type f -mtime -60 -exec cp{} /home/arc \;
 
A move ("[tt]mv[/tt]") will keep the file's modification date and time, but will remove it from the source directory.

A copy ("[tt]cp[/tt]") will create a copy with the current date and time as it's modification, last access, and status change date and times. The original file it was copied from will not be changed and keep it's original modification date and time.

Check your man page for "[tt]cp[/tt]" to see if you have the "[tt]-p[/tt]" option. That would preserve the attributes (including modification time) on the copy.
 

Tested using copy (instead move) =


find /wolf/day/month/AA/[0-9][0-9]/* -type f -mtime -7 -exec cp {} /home/jdoe/AA \;


When I run this in development, it works.

When running in Production, I does not copy files into home directory.

I am getting this message -

>>>>>> bash: /usr/bin/find: Argument list too long <<<<<<<

Please suggest, what to do to run this into Production.

There are about 30 directories to copy the files from.

I have tried using root, it doesn't work either.

Thanks, Nick
 
find /wolf/day/month/AA/[0-9][0-9] -type f ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top