how to output file names surrounded with quotes in SINGLE line?
Solution 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' ' '
Solution 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.
Solution 3
Try this.
find . -exec echo -n '"{}" ' \;
Solution 4
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
.
Solution 5
After 10 years, no one suggested the Bash "|while read" method?
find * -type d -depth 0|while read f; do echo \"$f\"; done
It's a simple Bash shell pipeline instead of launching another program like sed or xarg. If you really want to do something with each file/folder:
find * -type d -depth 0|while read f; do du -sh "$f"; done
By the way, find *
uses another Bash feature that excludes .xyz files/folders and will not output the ./
prefix find .
does.
Ana
Updated on July 09, 2022Comments
-
Ana almost 2 years
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
-
Ana almost 13 yearsThis works great! Thank you very much. My final command is now: "find . ! -type l ( -name . -o -prune ) | sort -n | sed 's/^/"/g' | sed 's/$/"/g' | tr '\n' ' ' | xargs myCommand". This single line was needed as input parameter for myCommand.
-
Gordon Davisson almost 13 yearsThis can be simplified even further:
printf "'%s' " *
. -
xastor over 11 yearsremove -n to get the result with newlines included.
-
Charles Duffy over 8 yearsPlease, please no. First,
find $PWD
will behave badly if your$PWD
contains spaces or literal glob characters that expand to anything. Second, substituting double quotes around a name to try to escape it is a weak and easily circumvented mechanism -- valid filenames can themselves contain"
or'
characters, thus throwing off parsing for the entire rest of the stream. -
Charles Duffy over 8 years@Ana, if on a system with GNU tools (
find -print0
andsort -z
), it's far better to usefind ... -print0 | sort -n -z | xargs -0 myCommand
-
Charles Duffy over 8 years@Ana, ...even if you don't want to use NUL delimiters, you can still tell
xargs
to use only newline delimiters and be safe against filenames with spaces (albeit not filenames with newlines, but the answer you've already accepted is also already unsafe against filenames containing newline literals):find ... | sort -n | xargs -d $'\n' myCommand
-
AnrDaemon almost 8 yearsYou should really use "ls" to list directory contents. It DO have the needed options.
-
Sridhar Sarnobat over 7 yearsThis should be marked the correct answer. The regex manipulation is too low-level.
-
jcarballo over 7 yearsI had to add a new line using cygwin on Windows for the output to be the same:
-printf "\"%p\" \n"
-
splaisan over 6 yearsrelated: 'ls -Q' ;# adds double quotes
-
Nimrod over 6 years-printf Not available on OSX.
-
Admin almost 6 years@Nimrod With Homebrew:
brew install findutils --with-default-names
-
David Balažic over 5 years...which fails spectacularly if the filename contains quotes.
-
Kanagavelu Sugumar over 5 yearsBut how to give this output with quoted to XARGS input for another command execution ?
-
Sagar Chauhan about 5 yearsPlease explain your answer.
-
will over 4 yearsI suggest:
ls -1 --quoting-style=c
-->"file one" "file two" etc.
. Thanks. -
SharpC about 4 yearsDoesn't work on Solaris 11, just prints
-n "{}"
lots of times! -
Chris Redford almost 4 yearsWhy have you included
your_command
? I thought the question had the single use case of listing the files in a directory with quotes around their names. -
Ian Colwell about 2 yearsIf you want to remove the leading
./
, try this:find * -exec echo -n '"{}" ' \;
-
darkdragon about 2 yearsWhile it works great in Alpine, it doesn't work in Debian/Ubuntu unfortunately...