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

后端 未结 9 957
[愿得一人]
[愿得一人] 2021-02-03 19:47

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 wit         


        
相关标签:
9条回答
  • 2021-02-03 20:23
        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.
    0 讨论(0)
  • 2021-02-03 20:26
    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.

    0 讨论(0)
  • 2021-02-03 20:28

    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.

    0 讨论(0)
  • 2021-02-03 20:33

    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.

    0 讨论(0)
  • 2021-02-03 20:33

    try

    ls | sed -e 's/^/"/g' -e 's/$/"/g' | tr '\n' ' '
    
    0 讨论(0)
  • 2021-02-03 20:33

    To avoid hacks trying to manually add quotes, you can take advantage of printfs %q formatting option:

    ❯ ll .zshrc.d
    total 112K
    -rwxrwxrwx 1 root root  378 Jul  1 04:39  options.zsh*
    -rwxrwxrwx 1 root root   57 Jul  1 04:39  history.zsh*
    -rwxrwxrwx 1 root root  301 Jul  1 05:01  zinit.zsh*
    -rwxrwxrwx 1 root root  79K Jul  1 05:19  p10k.zsh*
    -rwxrwxrwx 1 root root  345 Jul  1 05:24  zplugins.zsh*
    -rwxrwxrwx 1 root root  490 Jul  4 23:40  aliases.zsh*
    -rw-r--r-- 1 root root 9.0K Jul 27 08:14  lscolors.zsh
    -rw-r--r-- 1 root root    0 Aug 30 05:56 'foo bar'
    -rw-r--r-- 1 root root    0 Aug 30 05:58 '"foo"'
    
    ❯ find .zshrc.d -exec printf '%q ' {} +
    .zshrc.d .zshrc.d/history.zsh .zshrc.d/aliases.zsh .zshrc.d/zplugins.zsh .zshrc.d/p10k.zsh .zshrc.d/zinit.zsh .zshrc.d/options.zsh '.zshrc.d/"foo"' '.zshrc.d/foo bar' .zshrc.d/lscolors.zsh #
    

    vs:

    find .zshrc.d -printf "\"%p\" "
    ".zshrc.d" ".zshrc.d/history.zsh" ".zshrc.d/aliases.zsh" ".zshrc.d/zplugins.zsh" ".zshrc.d/p10k.zsh" ".zshrc.d/zinit.zsh" ".zshrc.d/options.zsh" ".zshrc.d/"foo"" ".zshrc.d/foo bar" ".zshrc.d/lscolors.zsh" #
    

    Notice the file .zshrc.d/"foo" is incorrectly escaped.

    ❯ echo ".zshrc.d/"foo""
    .zshrc.d/foo
    
    ❯ echo '.zshrc.d/"foo"'
    .zshrc.d/"foo"
    
    0 讨论(0)
提交回复
热议问题