I would like to output the list of items in a folder in the folowing way:
"filename1" "filename2" "file name with spaces" "foldername" "folder name with spaces"
In other words, item names must be in a single line, surrounded with quotes (single or double) and divided by spaces.
I know that
find . | xargs echo
prints output in a single line, but I do not know how to add quotes around each item name.
This code is part of a bsh script. The solution can therefore be a set of commands and use temporary files for storing intermediate output.
Thank you very much for any suggestion.
Cheers, Ana
Quotation marks, unless they are an explicit element of a command or label, should be avoided—though they may be helpful, used consistently, to delimit file names (which may or may not have telltale extensions and even if they do may be impossible to distinguish from the run of text, as in the WPD example above).
' End first quotation which uses single quotes. " Start second quotation, using double-quotes. ' Quoted character. " End second quotation, using double-quotes.
A single quote is not used where there is already a quoted string. So you can overcome this issue by using a backslash following the single quote. Here the backslash and a quote are used in the “don't” word. The whole string is accompanied by the '$' sign at the start of the declaration of the variable.
You could also simply use find "-printf", as in :
find . -printf "\"%p\" " | xargs your_command
where:
%p = file-path
This will surround every found file-path with quotes and separate each item with a space. This avoids the use of multiple commands.
this should work
find $PWD | sed 's/^/"/g' | sed 's/$/"/g' | tr '\n' ' '
EDIT:
This should be more efficient than the previous one.
find $PWD | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
@Timofey's solution would work with a tr in the end, and should be the most efficient.
find $PWD -exec echo -n '"{}" ' \; | tr '\n' ' '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With