long string using find and "-exec ls -ls" to find part-of filename

Polytropon freebsd at edvax.de
Mon Jun 30 22:30:16 UTC 2014


On Mon, 30 Jun 2014 14:55:34 -0700, Gary Kline wrote:
> 	how about whitespace?  ls -lsi appears as two cmds, so would
> 	it work as you  have with backtics?
> 
> 	find ... | xargs -n 1 `ls -lsi` 

Erm... this looks wrong. Either find or ls, but both...
except of course you want a ls -lsi output for each item
found by find... :-)

In case of whitespaces in filenames, you need to use at least
"..." (double quotes), that's why using a variable here is
probably the safer solution. Note that the whitespace has
a special meaning to the shell: it's the standard field
separator, separating commands and command line arguments.
The output separator of ls and find is a newline, so the
following should work for files with spaces:

#!/bin/sh
OLD_IFS=$IFS
IFS="
"
ls -lsi *.tar.gz *.tgz | while read F; do
	some_command "$F"
done
IFS=$OLD_IFS

First, IFS is set to newline. Then ls with the desired options
is run. Its results are separated by a newline. They are piped
to the while read construct which reads one line = one result
item at a time, executing the command. The double quotes make
sure that each result (which can contain spaces) is provided
as _one_ command line argument. Finally, IFS is restored.

But the articles I mentioned before do cover this quite compli-
cated topic much better.


-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list