问题
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
回答1:
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' ' '
回答2:
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.
回答3:
You can use the GNU ls
option --quoting-style
to easily get what you are after. From the manual page:
--quoting-style=WORD
use quoting style
WORD
for entry names:literal
,locale
,shell
,shell-always
,shell-escape
,shell-escape-always
,c
,escape
For example, using the command ls --quoting-style=shell-escape-always
, your output becomes:
'filename1' 'filename2' 'file name with spaces' 'foldername' 'folder name with spaces'
Using --quoting-style=c
, you can reproduce your desired example exactly. However, if the output is going to be used by a shell script, you should use one of the forms that correctly escapes special characters, such as shell-escape-always
.
回答4:
Try this.
find . -exec echo -n '"{}" ' \;
回答5:
for f in *; do printf "'%s' " "$f"; done; echo
Or, thanks to Gordon Davisson:
printf "'%s' " *; echo
The trailing echo
is simply to add a newline to the output.
回答6:
EDIT:
The following answer generate a new-line separated LIST instead of a single line.
- I'm second guessing that the OP uses the result for invoking other command
- converting the output LIST to single line is easy (
| tr '\n' ' '
)
A less mentioned method is to use -d
(--delimiter
) option of xargs
:
find . | xargs -I@ -d"\n" echo \"@\"
-I@
captures eachfind
result as@
and then we echo-ed it with quotes
With this you can invoke any commands just as you added quotes to the arguments.
$ find . | xargs -d"\n" testcli.js
[ "filename1",
"filename2",
"file name with spaces",
"foldername",
"folder name with spaces" ]
See https://stackoverflow.com/a/33528111/665507
回答7:
find . | sed "s|.*|\"&\"|"
Brief description:
We take result of .* pattern and put it into quotes.
Good source is sed.
Detail description:
Pattern: s/one/ONE/
- s Substitute command.
- / Delimiter.
In my expression "|" is used instead of "/" to prevent "mountains" like in pattern s/.*/\"&\"/. - one Regular expression pattern search pattern.
- ONE Replacement string.
- .* Means any symbol that repeats unlimited number of times.
- \" Means " itself.
- & Special character that corresponds to the pattern found.
回答8:
try
ls | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
来源:https://stackoverflow.com/questions/6041596/how-to-output-file-names-surrounded-with-quotes-in-single-line