nunos
nunos

Reputation: 21389

Moving Generated Files in Shell Script

I have a shell script that makes calls to some other scripts that generate some files. At the end of my shell script I want to move all files generated (they have the same preffix) into a folder.

However, for some reason, if I have the mv call at the end of the script I am getting this error, like the filesystem still didn't know about the files I think.

mv: cannot stat `foo_*': No such file or directory

My first attempt to "solve" this was to introduce a sleep call of some seconds to help me avoid this issue, but, even though the value was very high and I was sure the files were already created, I was getting the same error.

I then tried running my script like this:

sh myscript.sh ; mv foo_* bar

and it workded with no problem.

Any idea why this happens and how I can avoid having to use two scripts or a double line command to achieve the desired effect?

Thanks.

EDIT: I am writing this for someone that might get here from Google and have a similar problem.

Basically, I was generating the mv command like this:

mv $preffix"_*" $destination

For some reason, that wasn't working properly. Just created a temp variable like this:

MV_CMD=$preffix"_*" $destination

and then just called it like this:

mv $MV_CMD.

If anyone knows a cleaner way, please feel free to answer with that. Thanks all for your answers.

Upvotes: 0

Views: 127

Answers (2)

Nitin4873
Nitin4873

Reputation: 17562

Its a common practice to not leave behind garbage, that your prog creates

check the other shells for rm command ....... they might have instructions to remove or delete the intermediate files created .

you should also check for specific directory changes , the files might have been created in some other locations or moved somewhere.

but in either case you need to just browse through the scripts looking for a rm , mv or a cd command.

that ought to do it.

Upvotes: 0

Alex
Alex

Reputation: 11090

For debugging purposes, instead of appending sleep followed by mv to the end of the script, add:

pwd
ls -la .

This will print the current directory and all it's contents to the console.

Additionally, execute your script with debugging enabled: sh -x ./myscript.sh to view all the commands executed to give you a better idea of what the cause could be, as well as what the files being created are, and in which directory.

Upvotes: 1

Related Questions