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

42,396

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.

Share:
42,396
Ana
Author by

Ana

Updated on July 09, 2022

Comments

  • Ana
    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
    Ana almost 13 years
    This 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
    Gordon Davisson almost 13 years
    This can be simplified even further: printf "'%s' " *.
  • xastor
    xastor over 11 years
    remove -n to get the result with newlines included.
  • Charles Duffy
    Charles Duffy over 8 years
    Please, 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
    Charles Duffy over 8 years
    @Ana, if on a system with GNU tools (find -print0 and sort -z), it's far better to use find ... -print0 | sort -n -z | xargs -0 myCommand
  • Charles Duffy
    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
    AnrDaemon almost 8 years
    You should really use "ls" to list directory contents. It DO have the needed options.
  • Sridhar Sarnobat
    Sridhar Sarnobat over 7 years
    This should be marked the correct answer. The regex manipulation is too low-level.
  • jcarballo
    jcarballo over 7 years
    I had to add a new line using cygwin on Windows for the output to be the same: -printf "\"%p\" \n"
  • splaisan
    splaisan over 6 years
    related: 'ls -Q' ;# adds double quotes
  • Nimrod
    Nimrod over 6 years
    -printf Not available on OSX.
  • Admin
    Admin almost 6 years
    @Nimrod With Homebrew: brew install findutils --with-default-names
  • David Balažic
    David Balažic over 5 years
    ...which fails spectacularly if the filename contains quotes.
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 5 years
    But how to give this output with quoted to XARGS input for another command execution ?
  • Sagar Chauhan
    Sagar Chauhan about 5 years
    Please explain your answer.
  • will
    will over 4 years
    I suggest: ls -1 --quoting-style=c --> "file one" "file two" etc.. Thanks.
  • SharpC
    SharpC about 4 years
    Doesn't work on Solaris 11, just prints -n "{}" lots of times!
  • Chris Redford
    Chris Redford almost 4 years
    Why 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
    Ian Colwell about 2 years
    If you want to remove the leading ./, try this: find * -exec echo -n '"{}" ' \;
  • darkdragon
    darkdragon about 2 years
    While it works great in Alpine, it doesn't work in Debian/Ubuntu unfortunately...