Copying multiple files in a range
Hi, Here's one for you... If I have a range of files named dcp_1100.jpg - dcp_1189.jpg and I only want to copy 1156-1183, how do I do it. cp dcp_11[56-83].jpg <dest> does not work, but cp dcp_115[6-9].jpg <dest> does. What am I missing? There's got to be an easier way than typing it out serveral times for the range needed? Thanks Chris -- Chris ---------------------------------- E Mail: chris@glovercc.plus.com SIP: 84411389@voiptalk.org IAXTEL: 17003366726
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Chris Glover <chris@glovercc.plus.com> wrote:
Hi,
Here's one for you...
If I have a range of files named dcp_1100.jpg - dcp_1189.jpg and I only want to copy 1156-1183, how do I do it.
cp dcp_11[56-83].jpg <dest> does not work, but cp dcp_115[6-9].jpg <dest> does.
OK, how about this... cp dcp_11{5[6-9],[67][0-9],8[0-3]}.jpg dest Yeah, it's not pretty, but it should work. The alternative is something like... (in bash): for number in $(seq 1156 1183); do cp dcp_11$number.jpg dest done Cheers, - -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCmjSwEh8oWxevnjQRApZWAKC6uQIlV+IzBtbOsvBt2k33OptqfACgqWHX RWiR0liGny+ZraYIYwhSSdw= =UxkA -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
Chris Glover <chris@glovercc.plus.com> wrote:
Hi,
Here's one for you...
If I have a range of files named dcp_1100.jpg - dcp_1189.jpg and I only want to copy 1156-1183, how do I do it.
cp dcp_11[56-83].jpg <dest> does not work, but cp dcp_115[6-9].jpg <dest> does.
OK, how about this...
cp dcp_11{5[6-9],[67][0-9],8[0-3]}.jpg dest
Yeah, it's not pretty, but it should work. The alternative is something like... (in bash):
for number in $(seq 1156 1183); do cp dcp_11$number.jpg dest ^^ this was a deliberate error, honest... obviously this *will not* work unless you strip that 11, either from the seq or here. done
Cheers, -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
- -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCmjd4Eh8oWxevnjQRAjA4AJ92mZIyIiJwoFY6M5/F8RTXyRKhFwCfXfEZ H6bCwsbt62QDocm6km3ARSM= =Ay9l -----END PGP SIGNATURE-----
On Sunday 29 May 2005 10:43 pm, Brett Parker wrote:
for number in $(seq 1156 1183); do cp dcp_11$number.jpg dest
^^ this was a deliberate error, honest... obviously this *will not* work unless you strip that 11, either from the seq or here.
Ok that is a whole lot neater than my option (even with the mistake) Wasn't it Babbage that used to put deliberate errors in his plans to foil those who tried to copy him ? Hardly in the sprit of the ALUG, Brett :-)
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Wayne Stallwood <ALUGlist@digimatic.plus.com> wrote:
On Sunday 29 May 2005 10:43 pm, Brett Parker wrote:
for number in $(seq 1156 1183); do cp dcp_11$number.jpg dest
^^ this was a deliberate error, honest... obviously this *will not* work unless you strip that 11, either from the seq or here.
Ok that is a whole lot neater than my option (even with the mistake)
Wasn't it Babbage that used to put deliberate errors in his plans to foil those who tried to copy him ?
IIRC from random television, that Da Vinci character also deliberately built in faults in many of the designs for his inventions to make sure that the people that wanted to use them would actually pay him for his inventions. So, the designs they got to decide wether or not it was a good idea where deliberately flawed in order to stop the goits just stealing it ;) And, of course, at least I quickly pointed out the mistake (and added another variant, JIC ;) Cheers, - -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCmj26Eh8oWxevnjQRAv41AJ9T5Q2klmfZ/AtATKKi/PQcf4jOTgCfd0Ln Q/w1twE1JZrlPPSlNHnAkh8= =zJgO -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
Chris Glover <chris@glovercc.plus.com> wrote:
Hi,
Here's one for you...
If I have a range of files named dcp_1100.jpg - dcp_1189.jpg and I only want to copy 1156-1183, how do I do it.
cp dcp_11[56-83].jpg <dest> does not work, but cp dcp_115[6-9].jpg <dest> does.
OK, how about this...
cp dcp_11{5[6-9],[67][0-9],8[0-3]}.jpg dest
Yeah, it's not pretty, but it should work. The alternative is something like... (in bash):
for number in $(seq 1156 1183); do cp dcp_11$number.jpg dest done
And then, just because we can, and we like using as few processes as possible... for number in $(seq 1156 1183); do echo dcp_$number.jpg done | xargs cp --target-directory=dest Right - I wonder if anyone has anything more clean than that ;) (NOTE TO DAVE: No, you can not do: cp dcp_`seq 1156 1183`.jpg dest And here's why... the arguments to cp will expand to: dcp_1156 1157 ... 1183.jpg ) Cheers, - -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCmjjEEh8oWxevnjQRAjfrAJsGuaGpdxcPP+PxWSiX6RneihwYIwCfdq/t WZAWEc8hxbRMR01aApTNPzI= =5lHT -----END PGP SIGNATURE-----
Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
No, you can not do: cp dcp_`seq 1156 1183`.jpg dest And here's why... the arguments to cp will expand to: dcp_1156 1157 ... 1183.jpg
Actually, if you use the rc shell, you can do exactly that: cp dcp_^`{seq 1156 1183}^.jpg dest The `{} expands to a list, which then has the endings added associatively to produce dcp_1156.jpg dcp_1157.jpg and so on. Unfortunately, bash just turns the output of backticks into a string and pastes it into the command. It then reads the next command in from that, which causes problems in this and other situations, instead of doing what you usually want. -- MJ Ray (slef), K. Lynn, England, email see http://mjr.towers.org.uk/
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 MJ Ray <mjr@phonecoop.coop> wrote:
Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
No, you can not do: cp dcp_`seq 1156 1183`.jpg dest And here's why... the arguments to cp will expand to: dcp_1156 1157 ... 1183.jpg
Actually, if you use the rc shell, you can do exactly that: cp dcp_^`{seq 1156 1183}^.jpg dest The `{} expands to a list, which then has the endings added associatively to produce dcp_1156.jpg dcp_1157.jpg and so on.
Hmm, useful. Sortof. Though, other than you, dear Mark, who else is running the rc shell? I'd make an assumption that most people are using bash, or some derivitive of sh. If they're using tcsh on a modern *nix operating system, they probably want shooting... I haven't tested in zsh yet, but I'm fairly sure my solutions would work in that too ;)
Unfortunately, bash just turns the output of backticks into a string and pastes it into the command. It then reads the next command in from that, which causes problems in this and other situations, instead of doing what you usually want.
Well, no, it wouldn't because I'm very very used to how bash expands things ;) Cheers, - -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCnDqUEh8oWxevnjQRAtl6AJ9+gA0l+dVYAjvyjwRzO7yGCLcMKwCeK2dL xL3H02aa3Xn/i2XZUnEHYBg= =/fMK -----END PGP SIGNATURE-----
Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
Hmm, useful. Sortof. Though, other than you, dear Mark, who else is running the rc shell? I'd make an assumption that most people are using bash, or some derivitive of sh. [...]
In bash, you could do "cp $(seq -fdcp_11%g.jpg 58 83) dest" for the same effect, but it's not as general.
[...] which causes problems in this and other situations, instead of doing what you usually want. Well, no, it wouldn't because I'm very very used to how bash expands things ;)
So why did you use for and xargs to bring that into the problem? ;-) Anyway, the point was that Dave was expecting the obvious thing and bash doesn't do the obvious thing: a bug in bash not Dave. -- MJ Ray (slef), K. Lynn, England, email see http://mjr.towers.org.uk/
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 MJ Ray <mjr@phonecoop.coop> wrote:
Brett Parker <iDunno@sommitrealweird.co.uk> wrote:
Hmm, useful. Sortof. Though, other than you, dear Mark, who else is running the rc shell? I'd make an assumption that most people are using bash, or some derivitive of sh. [...]
In bash, you could do "cp $(seq -fdcp_11%g.jpg 58 83) dest" for the same effect, but it's not as general.
[...] which causes problems in this and other situations, instead of doing what you usually want. Well, no, it wouldn't because I'm very very used to how bash expands things ;)
So why did you use for and xargs to bring that into the problem? ;-)
For and xargs was the second example, and not as neat as the first, the first using pure shell globbing foo, the second being an easy ish way to do it if you don't know globbing. Oh, and of course, I used xargs foo to reduce the number of processes required on a large data set :P The for and xargs foo maybe required on stupidly large data sets.
Anyway, the point was that Dave was expecting the obvious thing and bash doesn't do the obvious thing: a bug in bash not Dave.
I'd say it's non-obvious to a shell that you want that bit in the middle of these things to be {pre,app}ended to the data. Maybe I'm not really human though :P Cheers, - -- Brett Parker web: http://www.sommitrealweird.co.uk/ email: iDunno@sommitrealweird.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFCnETMEh8oWxevnjQRAnUwAJ9aaYFrQn8VjRoSltIZrJ34Jw6BfgCgg4lq OWEVo85D1dD0R8P39QGT9DE= =PsLK -----END PGP SIGNATURE-----
I am sure there is an easier way to do it but... index=1155 while [ $index -le 1182 ]; do let index=$index+1 cp 'dcp_'$index'.jpg' <dest> done Will work On Sunday 29 May 2005 10:18 pm, Chris Glover wrote:
Hi,
Here's one for you...
If I have a range of files named dcp_1100.jpg - dcp_1189.jpg and I only want to copy 1156-1183, how do I do it.
cp dcp_11[56-83].jpg <dest> does not work, but cp dcp_115[6-9].jpg <dest> does.
What am I missing? There's got to be an easier way than typing it out serveral times for the range needed?
Thanks
Chris
participants (5)
-
Brett Parker -
Chris Glover -
MJ Ray -
runlevelten@gmail.com -
Wayne Stallwood