Saturday 18 July 2009

Dealing with filenames with spaces in bash scripts

My war against spaces in filenames is lost. Even if I would never dare type one, my hardrive keeps receiving them from computer illiterate colleagues.

Admittedly, they are not a big pain until I want to make backups. My backup script contained a 

for file in `find . -type f`
do
...
done

kind of script. But whenever the found filename contained an space, my variable $file contained part of the filename and a huge mess unleashed.

Here is, however,  a neat solution to my troubles:

find . -type f -name '*' | while read file
do
...   
done

No comments:

Post a Comment