如何在单行中输出用引号括起来的文件名?

人气:620 发布:2022-10-16 标签: unix bash find xargs

问题描述

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

解决方案

this should work

find $PWD | sed 's/^/"/g' | sed 's/$/"/g' | tr '
' ' '

EDIT:

This should be more efficient than the previous one.

find $PWD | sed -e 's/^/"/g' -e 's/$/"/g' | tr '
' ' '

@Timofey's solution would work with a tr in the end, and should be the most efficient.

find $PWD -exec echo -n '"{}" ' ; | tr '
' ' '

538