Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to output file names surrounded with quotes in SINGLE line?

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

like image 241
Ana Avatar asked May 18 '11 08:05

Ana


People also ask

Can filenames have quotes?

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).

How do you escape a single quote inside a single quote?

' End first quotation which uses single quotes. " Start second quotation, using double-quotes. ' Quoted character. " End second quotation, using double-quotes.

How do you handle a single quote in a string?

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.


2 Answers

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.

like image 151
Benjamin A. Avatar answered Oct 05 '22 02:10

Benjamin A.


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' ' ' 
like image 39
freethinker Avatar answered Oct 05 '22 02:10

freethinker