Boise, Idaho
Or Remote

find, xargs, and filenames with spaces

Although I don’t typically name a file with the space, such as “my cool file” it is nonetheless possible to do so in linux. However, this causes problems when using tools such as the find command and the xargs command.

The other day I rsync’d one of my folders to back up the documents to an external harddrive. Later, I copied the files back (not using rsync — but cp).

The permissions on the files changed, setting the executable bit on all the files. Now, when I open a file in gedit I am asked if I want to display the contents or run the file. Obviously it is text file, but because the executable bit is set linux treats it differently.

It is easy enough to change a few files:

$ chmod 644 *

To recursively change all of the files, I would use this set of commands:

$ find . -type f | xargs chmod 644

This works great…unless there are filenames which contain spaces in their names. In this case, the output from find will break up the filename into several pieces (separated by the space) and send each one over to xargs.

To prevent this we use the -print0 flag on find and the -0 flag on xargs:

$ find . -type f -print0 | xargs -0 chmod 644

Try it!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>