Search code examples
macosterminalprintfls

Command "ls" can't find existing file


I am attempting to sanitize my filenames without actually renaming them in preparation for "ls -l" in Mac OS terminal.

I am using printf %q like this:

ls -l $(printf %q "filenamewithspaceinit")

However, if a space is present in the file name (which normally elicits an error that I was trying to correct with this code), ls has the gall to repeat the exact file name with backslash and space that it would recognize if I simply autocompleted the file name with tab in shell and claim that there is "No such file or directory."

I am very exasperated with the command right now. I thought that printf was supposed to produce navigable and proper file names.


Solution

  • Leave out the %q so you don't quote it twice?

    15:23:54-qsario@MacBook-Air:~/test$ ls
    foo bar
    
    15:24:01-qsario@MacBook-Air:~/test$ ls -l "$(printf %q 'foo bar')"
    ls: foo\ bar: No such file or directory
    
    15:24:04-qsario@MacBook-Air:~/test$ ls -l "$(printf 'foo bar')"
    -rw-r--r--  1 qsario  staff  0 Aug  5 15:19 foo bar