Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do i copy one file to multiple directories recursively when the directories have whitespace in the name?

Posted on 2009-02-19
26
Medium Priority
?
372 Views
Last Modified: 2013-12-26
I am using the following commands to copy one file into multiple directories and recurse through those directories and copy the file to any sub directories it finds... It would work but there is whitespace in almost all of the directory names and i cant find an easy or working command to remove the whitespace from the directory names... Also fyi, i am ssh'd into the server, which is for my webhosting...
for i in $(find directory-with-many-subdirectories -type d -print);do cp index.htm "$i";done

Open in new window

0
Comment
Question by:disASM
  • 14
  • 12
26 Comments
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687065
Add:

IFS=

before the loop, that is:

IFS=
for i in ........

0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687104
Scratch that...

Make it:

OLDIFS="$IFS"
IFS=`echo -en "\n\b"`
for i in ............................
IFS=$OLDIFS
0
 

Author Comment

by:disASM
ID: 23687108
ERROR:

-bash: syntax error near unexpected token `do'

0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687130
Split it into lines:

for i in $(......)
do
    .....
done
0
 

Author Comment

by:disASM
ID: 23687152
Scratch that...

Make it:

OLDIFS="$IFS"
IFS=`echo -en "\n\b"`
for i in ............................
IFS=$OLDIFS

"That did not go well.... i can NOT list the directories anymore.... from the webbased file explorer i get a data error when trying to read the directory and when i try to get to it from ssh i can see the the directories are royally f**ked...
0
 

Author Comment

by:disASM
ID: 23687165
Split it into lines:

for i in $(......)
do
    .....
done


Can i do this without creating a file and excuting it, I.E. from the command line. and are you saying this will fix my original problem or is it in conjuction with the other suggestions?
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687166
I ran the same script this side:

IFS=`echo -en "\n\b"`
for i in $(find . -type d -print)
do
  cp index.htm "$i"
done

And it works
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687194
Please define "royally f**ked"
0
 

Author Comment

by:disASM
ID: 23687224
after i ran it when i try to run the dir command i get the following errors:
/bin/ls: : No such file or directory
/bin/ls: : No such file or directory
/bin/ls: : No such file or directory
/bin/ls: : No such file or directory
/bin/ls: l: No such file or directory
/bin/ls: r=tty: No such file or directory
/bin/ls: F: No such file or directory
/bin/ls: a: No such file or directory
/bin/ls: : No such file or directory
/bin/ls: : No such file or directory
/bin/ls: T: No such file or directory
/bin/ls: 0: No such file or directory

i am fixing the directory...
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687245
What is the exact command that you entered into the commandline. Can you press the up arrow until you get to it, and paste it here please.
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687335
One line works here as well:

# echo "test file" > index.htm
# mkdir /root/test
# mkdir "/root/test/aaa bbb"
# mkdir "/root/test/ccc ddd eee fff"
# mkdir "/root/test/aaa bbb/ccc ddd eee"
# IFS=`echo -en "\n\b"`;for i in $(find /root/test -type d -print);do cp index.htm "$i";done
# find /root/test
/root/test
/root/test/index.htm
/root/test/aaa bbb
/root/test/aaa bbb/index.htm
/root/test/aaa bbb/ccc ddd eee
/root/test/aaa bbb/ccc ddd eee/index.htm
/root/test/ccc ddd eee fff
/root/test/ccc ddd eee fff/index.htm
0
 

Author Comment

by:disASM
ID: 23687344
unfortunately no i cannot post the command i used, i was in a ssh session and i exited the program to see if it would fix the error and then logged back in... I am fairly certain that it was my fault for not putting it on seperate lines... i had to delete the directory and recopy it from its original source. once that finishes i will try that again, on seperate lines, and let you know the results. thanks for your persistent help...
0
 

Author Comment

by:disASM
ID: 23687445
OK. here is what i ran, exactly:

IFS=`echo -en "\n\b"`
for i in $(find . -type d -print)
do
  cp index.htm "$i"
done

the directory structure is like this:

index.htm
directory-full-of-subdirectories\
directory-full-of-subdirectories\ sub dir a\
directory-full-of-subdirectories\ sub dir b\
directory-full-of-subdirectories\ sub dir....


after running the command from the directory containing index.htm got this error MANY TIMES...

cp: cannot create regular file `INSERTSOMESUBDIRECTORYNAME': No such file or directory

and the directory containing the original index.htm is filled with non-sensical files that are with names that seem to be substrings of the sub directories..
0
 

Author Comment

by:disASM
ID: 23687464
I dont want to assume that i have made everything clear, but my goal is to have it look like this after running the commands...

directory-full-of-subdirectories\index.htm
directory-full-of-subdirectories\ sub dir a\index.htm
directory-full-of-subdirectories\ sub dir b\sub sub dir 1\index.htm
directory-full-of-subdirectories\ sub dir b\index.htm
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687533
That is how I understood it. And it baffles me that it works here, as you can see from the previous session that I copied directly from my ssh client. What does

bash --version

say?
0
 

Author Comment

by:disASM
ID: 23687566
GNU bash, version 3.2.48(1)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
0
 

Author Comment

by:disASM
ID: 23687581
is i possible that its an issue with the first line:
IFS=`echo -en "\n\b"`

because i am seeing alot of errors like this:

cp: cannot create regular file `WIRELESS\n./MOTOROLA': No such file or directory
0
 

Author Comment

by:disASM
ID: 23687584
where MOTOROLA is part of the directory name that is in a directory ending in WIRELESS
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687600
Lets take another aproach:

Say index.htm is in the /tmp directory:

find dir-with-lots-of-subdirs -d -print -exec cp /tmp/index.htm {} ';'

0
 

Author Comment

by:disASM
ID: 23687638
# find attachments -d -print -exec cp index.htm {}';'
find: invalid predicate `-d'
0
 
LVL 7

Accepted Solution

by:
Morne Lategan earned 2000 total points
ID: 23687650
sorry, its late:

find attachments -type d -print -exec cp index.htm {}';'

BTW: It just dawned on me that you echo command might be aliased. Thats why the previous doesnt work on your machine.
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687660
And there's a space before ';'
0
 

Author Comment

by:disASM
ID: 23687680
Thats it.... Thank you, thank you, thank you... you just saved me a couple days of work... there is about 1300 subdirectories here...
0
 

Author Closing Comment

by:disASM
ID: 31549027
worked perfectly...
0
 
LVL 7

Expert Comment

by:Morne Lategan
ID: 23687685
You're welcome. Sorry it took so looooong to get to a solution :)
0
 

Author Comment

by:disASM
ID: 23687696
Thanks for hanging in there and helping me out... i was very close to downloading the entire directory structure and writing a python script to do it and reupload...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question