1

I have this code

for i in $(find pwd)
do
    echo $i
done

the problem is if the file name contains spaces, it prints on a separate line

how can I list all of the files in some directory including files that contains spaces

Lukap
  • 30,736
  • 61
  • 153
  • 243

3 Answers3

1

I would use while read for this.

find . | while read i; do echo $i; done;

Edit:
Alternatively, you could just do ls -a1

Tom
  • 2,144
  • 2
  • 17
  • 28
1

This would have the intended effect of your example:

find /path/to/somewhere

That is, no need to wrap a for loop around it.

But I'm guessing you want something more than just echoing. Perhaps call a script for each file? You can do that with:

find /path/to/somewhere -exec path/to/script.sh {} \;

where {} will be replaced for each filename found.

janos
  • 115,756
  • 24
  • 210
  • 226
-1

here is the solution

IFS=$'\n'
    for i in $(pwd)
    do
        echo $i
    done
Lukap
  • 30,736
  • 61
  • 153
  • 243
  • Why limited to `$7`? It will fail if the path contain more spaces, right? – anishsane Jun 03 '13 at 16:13
  • 1
    The `awk` solution is a non-solution, as you are only processing the directory name, not the names of the files contained under the directory. While the `IFS`-based solution works, it only works for file names that don't contain new lines (which are rare, but legal). – chepner Jun 03 '13 at 16:15