You are not logged in.

#1 2009-05-06 18:21:41

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved]simple bash command / me to stupid to escape spaces

Hi!


I'm trying to...
cp $(find | grep odt) ../all
(get all odt's from subfolders and copy to ../all)

But obviously that one only copies the odt that don't have spaces in their path.

1) How do I do that one the right way? (Can't somehow just escape \" \" around the find-stuff, so I guess I gotta find the right thing . Also I need some "putting-stuff-together-thing".+."to get the \" to the expression thing without it getting into the \\command executed\\ dammit. Sometimes I think, maybe this bash-thing isn't meant to be used/learned solely by trial & error roll)?
2) Does someone know a good "general bash syntax" chart with condensed information? Something very compact without "useless words" in between ?


thx!

Last edited by whoops (2009-05-07 10:44:40)

Offline

#2 2009-05-06 18:47:12

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved]simple bash command / me to stupid to escape spaces

cp "$(find | grep)" ... would take care of the spaces, but a better way would be this:

find ./ -name '*odt*' | while read file; do
   cp "$file" ../all
done

adjust the -name 'whatever' as needed

there are probably other ways.

Last edited by brisbin33 (2009-05-06 18:47:45)

Offline

#3 2009-05-06 18:57:55

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved]simple bash command / me to stupid to escape spaces

brisbin33 wrote:

cp "$(find | grep)"

[i]I tried that one - the whole find-output is inside the quotes which results in a three site "filename" that doesn't exist. So if I want to try the wrong way... maybe I can find a way to replace all \n by "".

$(find | grep odt | [regexp program I don't know the name of] ("\n", "\"\""))

(edit:
The other one seems to do the same, although I first thought it worked as it was doing something with the hd a whole lot longer than the first one.)
edit2:
Oh, it does work, just something strange after that, I will see...

Last edited by whoops (2009-05-06 19:00:55)

Offline

#4 2009-05-06 19:00:38

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: [solved]simple bash command / me to stupid to escape spaces

the good old "find and xargs combo":

find . -iname *\.odt  -type f -print0 | xargs -0 -I cp {} ../all

i think this should work.

Offline

#5 2009-05-06 19:04:48

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: [solved]simple bash command / me to stupid to escape spaces

brisbin33 wrote:

cp "$(find | grep)" ... would take care of the spaces, but a better way would be this:

find ./ -name '*odt*' | while read file; do
   cp "$file" ../all
done

adjust the -name 'whatever' as needed

there are probably other ways.

for this you'll need smth like

while read file; do
   cp "$file" ../all
done < <(find ./ -name '*odt*')

Offline

#6 2009-05-06 19:21:21

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [solved]simple bash command / me to stupid to escape spaces

"xargs -I" will still do 1 file at a time so the command is run a lot of times

You can use -printf to surround the files in single quotes and interpret it with eval.
eval cp $(find -maxdepth 1 -type f -printf "'%f' ") backup/

Offline

#7 2009-05-06 19:33:01

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved]simple bash command / me to stupid to escape spaces

DonVla wrote:
brisbin33 wrote:

cp "$(find | grep)" ... would take care of the spaces, but a better way would be this:

find ./ -name '*odt*' | while read file; do
   cp "$file" ../all
done

adjust the -name 'whatever' as needed

there are probably other ways.

for this you'll need smth like

while read file; do
   cp "$file" ../all
done < <(find ./ -name '*odt*')

lolwut?

$ mkdir all
$ mkdir test
$ touch test/something{1,2,3,4}.odt
$ cd test/
$ touch something\ else{1,2,3}.odt
$ ls
something1.odt  something3.odt  something else1.odt  something else3.odt
something2.odt  something4.odt  something else2.odt
$ find ./ -name '*.odt' | while read file; do cp "$file" ../all/; done
$ ls ../all/
something1.odt  something3.odt  something else1.odt  something else3.odt
something2.odt  something4.odt  something else2.odt

edit: made the example better.

Last edited by brisbin33 (2009-05-06 19:48:25)

Offline

#8 2009-05-06 19:59:17

syntaxerrormmm
Member
From: Italy
Registered: 2008-10-22
Posts: 80
Website

Re: [solved]simple bash command / me to stupid to escape spaces

Really sorry guys, but please read man pages.

$ find test/ -iname \*.odt -exec cp {} all/ \;

HTH


syntaxerrormmm - Homepage

Offline

#9 2009-05-06 20:01:46

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [solved]simple bash command / me to stupid to escape spaces

syntaxerrormmm wrote:

Really sorry guys, but please read man pages.

$ find test/ -iname \*.odt -exec cp {} all/ \;

HTH

heyoooh, i always forget about that one.

/me goes to replace many a while loops...

Offline

#10 2009-05-07 10:43:34

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved]simple bash command / me to stupid to escape spaces

Thanks, got it, me learned something wink !


find test/ -iname \*.odt -exec cp {} all/ \;

Yes, I was familiar with that one, but didn't want to be limited to find (more stuff like that to do later).

while read file; do
   cp "$file" ../all
done < <(find ./ -name '*odt*')

I think I like that syntax most - for me it's easiest to remember / understand / alter.

xargs looks very useful - gotta take a closer look at that later.

Offline

#11 2009-05-07 11:10:54

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: [solved]simple bash command / me to stupid to escape spaces

best bash FAQ around:
http://wooledge.org:8000/BashFAQ
should answer most of your questions.

Offline

Board footer

Powered by FluxBB