Operating System - Linux
1752857 Members
3776 Online
108790 Solutions
New Discussion юеВ

Help regarding a shell script

 
Atul Gautam
Valued Contributor

Help regarding a shell script

Hi Xperts,

I have got a dir structure in my home dir for FTP users which is ---

/home
|
/ftp
|
-------------USERS---------------
| | | | | |
A B C D E F |
|
|-/2006 - /Month - /Date - /1 & /2
| | |
| /a & /b /a & /b
|
|
|-/2007 - /Month - /Date - /1 & /2
| |
/a & /b /a & /b



The dir structure present under A is replicated under every user's home directory.

What I want is ---
FILES MODIFIED 7 OR MORE DAYS BACK SHOULD GET COPIED FROM "/home" TO "/archive" DIRECTORY USING THE SCRIPT.

The simple script that I could write is given below but it is not doing as to what I want
__________________________________________
DURATION=7
BACKUP=/tmp/ftp
HOME_DIR=/home/ftp

cp -rf -p `find $HOME_DIR -mtime +$DURATION | sed 's/\.\///' | sed '/.*\/.*/d'` $BACKUP
__________________________________________

It gets executed but gives me an error message at the end which is ---

./copy_file.sh: line 5: /bin/cp: Argument list too long


I also counted the no. of lines that it parses and it is 78916.

Kindly help me in resolving this problem.


Thanks in advance!!

ATUL
10 REPLIES 10
Atul Gautam
Valued Contributor

Re: Help regarding a shell script

Hi Xperts,

I'm attaching a .doc file depicting the directory structure for your convenience.

Kindly help me.....


Thanks....



Atul
g33k
Valued Contributor

Re: Help regarding a shell script

As far as I can read someoneelse script this will give you the list of file not modified for last 7 days.
Well it seems to be OK, but how many files could be used as source for cp command? I mean how long can be the list of coping files? I think it have limited count...

So try to use FOR cycle and coping one file from list after another.

I hope you know and use this but: http://www.tldp.org/LDP/abs/html/

(nice "book" about scripting)
Atul Gautam
Valued Contributor

Re: Help regarding a shell script

Hi...

As I know it will be....

List of file not modified in last 7 days but modified before that time.





--
Atul
g33k
Valued Contributor

Re: Help regarding a shell script

As I allready said the argument list is too long cp is not able too handle such many parametres as sources. so solution is:

for File in `find $HOME_DIR -mtime +$DURATION | sed 's/\.\///' | sed '/.*\/.*/d'`; do cp $File $BACKUP; done
Atul Gautam
Valued Contributor

Re: Help regarding a shell script

Thanks....

As you suggested...I also did the same thing but removed the "sed" portion from it....

Mine looks like dis.....

DURATION=7
BACKUP=/tmp/ftp
HOME_DIR=/home/ftp

for file in [ `find $HOME_DIR -mtime +$DURATION` ]
do
cp -rf -p $file $BACKUP
done



Thank u vereeeee much for your valuable help....



--
Atul
g33k
Valued Contributor

Re: Help regarding a shell script

you are wellcome it was quite easy, when I was on the begining of my scripting way I've done a lot of mistakes... it wants just time and practice...
Manuel Wolfshant
Trusted Contributor

Re: Help regarding a shell script

you could also use two alternative methods

find $HOME_DIR -mtime +$DURATION` -exec cp -rf -p {} $BACKUP \;

or

find $HOME_DIR -mtime +$DURATION` | xargs cp -rf -p $BACKUP

Atul Gautam
Valued Contributor

Re: Help regarding a shell script

Hi Manuel,

The command that you mentioned is not working as I want.

What I want is that "the files or directories that are being searched for should get copied in the same way as they are present.
In other other words I should say that the directory hierarchy should be maintained at the destination as well.

I appreciate your help. Your command did work, but it doesn't copy the files as per their hierarchy.




Thanks!!


ATUL
Atul Gautam
Valued Contributor

Re: Help regarding a shell script

Hey Manuel,

I've attached a file with the output of few of my file names that I want to copy from the home directory location and wanna paste them inside /archive directory in the same hierarchy as they are present.




Atul