How to quote a string containing dollar sign $ and single quote '?
Wow, some complex solutions here! I think all you need to do is this though:
find ./\$my\'dir -type d -exec sh -c 'ls "$1"' sh {} \;
Instead of putting the arguments into the sh
command string, just use them as arguments to sh
. Note the second sh
is the value of $0
, everything after that is a positional argument.
As for your full problem of finding directories not containing pdfs, you are usually always better off using find
for this kind of thing rather than ls
. This should be close to what you are looking for:
find . -type d \
-exec sh -c '[ "$(find "$1" -maxdepth 1 -type f -iname "*.pdf")" = "" ]' sh {} \; \
-print
Ok... here is a convoluted way to do it. It'll fail if the filenames contain new line characters.
find . -type d -print0 \ # get the directory list,
\ # separated by nulls
| xargs -0 ls -1 -d -Q --quoting-style=shell \ # pick it with xargs, and use
\ # ls to apply shell quotes to each
\ # entry
| xargs -d '\n' -I '{}' \
sh -c "ls {} | grep -qi \.pdf$ || echo {}" # pick each quoted entry
# with xargs again
# (using -d, so it keeps the quotes)
# and insert it in the string
This relies on ls -Q --quoting-style=shell
quoting with single quotes... otherwise, use single quotes on the argument to sh -c
.
Ugh.