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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

list directory with spaces shell script

or file in `ls`; do echo "cp -R $dir  ./dir2/";  done
 
This works great except directories with spaces do not work as expected.  For example directory name  sales and returns

echoes
cp -R /dir1/sales  /dir2/
cp -R/dir1/and  /dir2/
cp -R /dir1/returns  /dir2/

I should get
cp -R /dir1/sales and returns  /dir2/

How do I stop directory names with spaces from being split?

I'll close the question after this (-: thanks all.
0
dplinnane
Asked:
dplinnane
3 Solutions
 
vvkCommented:
this should be able to help
ls|while read dir; do echo "cp -R \"$dir\"  ./dir2/";  done
0
 
TintinCommented:
Simply do

cp -R * ../dir2

Open in new window

0
 
AnacreoCommented:
I always use this:


or file in `ls | sed -e 's/.*/"&"/g'`; do echo "cp -R $dir  ./dir2/";  done


But basically this will wrap each line output from ls in double quotes...

.* - match whole line
"&" - & is replaced by the search result and the double quotes are literals inside of the single quoted arg...

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now