I am trying to feed Directory names into a for loop. My code is as follows:
td='/Test/TopDir/'
cd "$td"
for x in $(ls -d */ | cut -f1 -d'/'); do
echo x=$x
done
The top directory I run this on looks like this when running an ls command:
ls -l
drwxrwxrwx 4 Jason users 4096 May 6 06:36 2014-02-02 - Jebby (
drwxrwxrwx 3 Jason users 4096 May 6 06:09 2014-02-04 - Jebby (
drwxrwxrwx 2 root root 4096 May 6 06:09 @eaDir
-rw-r--r-- 1 Jason users 3956225 Jan 26 10:17 DSC01062.JPG
-rw-r--r-- 1 Jason users 3927603 Jan 26 10:18 DSC01063.JPG
The results of my for loop is as follows:
x=2014-02-02
x=-
x=Jebby
x=(
x=2014-02-04
x=-
x=Jebby
x=(
x=@eaDir
As you can see the for loop is breaking the directory names into sub-pieces after each space. In this example I only want the For Loop to execute three time with the three directories:
- 2014-02-02 - Jebby (
- 2014-02-04 - Jebby (
- @eaDir
What am I doing wrong?
ls -d */ | xargs -I{} echo 'x='{}
– Floris May 10 '14 at 19:43