How do I deal with directories that have spaces in their names from `find`?
Solution 1
Something like the following works ...
find . -type d | while read dir; do echo $dir; done
.
./my dir
Depending on what you're doing, you might be better using find
's -print0
option and xargs -0
.
The code you've got takes the unquoted output from find
and uses it as a list of words (split on whitespace) for for
to iterate over.
Solution 2
Do not use for
loop, use while
instead:
find . -type d -print0 | while read -d '' -r dir; do echo "$dir"; done
Option print0
prints NULL character at the end of file/directory name (instead of newline) and read -d ''
interprets it properly.
Solution 3
You can just use "$file" in your example.
Solution 4
If you're really just looking to echo
the results of find
, you can use the parameter -print
(or just no additional parameter at all) to have find
print a list of its results.
If you want to delete the results, there's -delete
(which can be combined with -print
to get a list of the deleted files).
If you want to do something else with/to the results, you can use the parameter -exec
to pass the results as parameters to another command, e.g.
find . -type d -exec tar cf {}.tar {} \;
to compress all directories into individual tarballs (one tar per directory, since\;
makesfind
run the-exec
command with one result at a time)find . -type d -exec tar cf all-directories.tar {} +
to compress all directories into a single tarball (+
makesfind
run the-exec
command with as many results as possible at a time)
Solution 5
You have to set IFS variable in bash:
SAVEIFS=IFS
IFS=$(echo -en "\n\b")
...
YOURCODE
....
IFS=$SAVEIFS
But what does above code do? , it discard any space and meta char..
Related videos on Youtube
Almo
Programmer, game designer, master's in physics. Cognizer: my latest game on iOS, Android, Steam and the web version at Kongregate. Free download! Maniac Games, my video game endeavor: http://www.maniac-games.com/ Maniac Games forum: http://www.maniac-games.com/forum Linkage, my published boardgame: http://nestorgames.com/#linkage_detail
Updated on September 18, 2022Comments
-
Almo over 1 year
I have this:
#!/bin/bash for file in `find . -type d` do echo $file done
If I have just one directory called
My Directory
, the output isMy Directory
How do I fix this?
The
echo $file
is just temporary. There will be other code in there operating on the directories. -
Almo over 9 yearsThis doesn't actually work. The
for
breaks it up before it gets there. -
Almo over 9 yearsI'll be doing an xattr to remove resource forks on OSX with this. Question edited, thanks.
-
adamantly over 9 yearsWhat do you mean breaks it up? Using
echo "$file"
works for me. -
cuonglm over 9 yearsThis assumes that you use GNU find of BSD find.
-
Almo over 9 yearsEcho "$file" works on one variable. But in this loop, the for $file has already broken the output of the find command in to a list of strings that were split by spaces. So it's too late already by the time it gets to the echo command.
-
syntaxerror over 9 yearsMaybe it would be a good idea to issue a warning about
-delete
! Likedeltree
on DOS prompt in Windows, this seemingly inconspicuous option can cause huge file trees to get purged in no time!! So use-delete
with extreme caution. -
syntaxerror about 9 yearsJust to make one thing clear: for the OP's question, the
for
loop is indeed unnecessary BUT ... it is not necessarily redundant, especially not forbash
beginners. For example, if you use two loop variablesi
andj
(withj
being an alteredi
) or simply a couple different statements, thefor
loop approach (though frowned upon) is easier to comprehend than teaching them the pro solution of-exec sh -c ...
which is as if you attempted to teach calculus to a person that only ever learned about basic arithmetics.