How to start shell expansion at a specific point?

peterfarge

New Member
Joined
Apr 11, 2019
Messages
6
Reaction score
0
Credits
0
Hello Forum,

often I have the problem that I want to process some files with a shell command. Then an error occurs with a file. I fix it and then I want to proceed after the file. Example:

cp /mydir/* /otherdir

The error occurs with file '/mydir/fffx'. What would be a fine way with shell expansion to skip all files before '/mydir/fffx'?


Thanks a lot

Peter
 


G'day Peter and welcome to linux.org :)

I won't be the one to answer your question, but I am moving this to Command Line, where it will attract a better audience for you and hopefully some help.

Good luck.

Chris Turner
wizardfromoz
 
The quickest and easiest way to do this would be to completely avoid trying to write a shell expansion and use rsync instead of cp:
Code:
rsync -a /path/to/mydir/* /path/to/otherdir/
That will pretty much do what your cp command was doing. But it will only copy files from /path/to/mydir that do not already exist in /path/to/otherdir, or that have a newer time-stamp.

So if rsync fails with an error on a particular file, you can fix the problem and then run rsync again. On the second run, the files that were copied over the first time will be ignored because their time-stamps will match.

That would be my recommendation. That and I can't think of a sane expansion to do what you want ATM!
 
  • Like
Reactions: Rob
Thanks for the advice :)
I could also use cp -n. My problem is more general, because this happens also with other commands.
For example if the last processed file was Lnxxxxxx and the next file will be Loxxxxxxxx, I write this expansion:
cp L[o-zA-Z]* [M-Z]*

But then I always have to check manually if really all files are processed, because of the lexicographic order of some special signs and cases I missed. But if there is no easy way... :(
 
Thanks for the advice :)
I could also use cp -n. My problem is more general, because this happens also with other commands.
For example if the last processed file was Lnxxxxxx and the next file will be Loxxxxxxxx, I write this expansion:
cp L[o-zA-Z]* [M-Z]*

But then I always have to check manually if really all files are processed, because of the lexicographic order of some special signs and cases I missed. But if there is no easy way... :(

Exactly. You could try to write a regex each time to continue where you left off, but it is easy to get things slightly wrong with a regex and end up accidentally missing files, or getting a different result than expected.

Also re: cp -n
That would only work if the destination directory was empty the first time you tried to copy the files.
Repeating the copy using the -n (aka --no-clobber) flag to cp would cause ANY existing files to be ignored and would copy from the failed file onwards. So that could work really well.

But if you'd used the destination directory to back up the entire source directory previously - using -n would not help because any files in the source directory that are already in the destination directory will NOT be copied over to the destination. So if the destination directory was being used to continuously back-up files from the source - the -n flag would be of little use, because the destination would never end up getting the newer versions of the files.

So bearing all of those different factors in mind, I recommended using rsync. Using rsync in place of cp should solve the problem nicely without having to use a regex/shell expansion.
 

Members online


Top