3

Okay, this is weird, hopefully someone here can explain what is happening.

I am using OS X 10.9.4 and I want to list files in a directory (and filter them using *)

I want to look for zip files in the Downloads directories of my user directory and my son's directory.

This shows all files including ones ending in .zip: sudo ls /Users/Jamie/Downloads/
But when I try to filter it: sudo ls /Users/Jamie/Downloads/*zip
I get No such file or directory

But that same command works fine in my directory!? sudo ls /Users/Hamish/Downloads/*zip

In fact even this generates the No such file or directory error on his path but not mine: sudo ls /Users/Jamie/Downloads/*

3
  • And are there files in /Users/Jamie/Downloads (other than . files)? Jul 29, 2014 at 22:39
  • Yes, there are lots of files there including zip files (visible when I don't use the wildcard)
    – Hamish
    Jul 29, 2014 at 22:43
  • 4
    I'm guessing the expansion of the asterisk is happening before the sudo, so it expands to the files with your credentials. It doesn't make too much sense to me, might be a bug. I can't test it right now. You can always do this workaround sudo ls /Users/Jamie/Downloads/ | grep -E 'zip$'
    – user127350
    Jul 29, 2014 at 22:54

2 Answers 2

3

Pathname expansion occurs at the time you run the command, so be expanded before running sudo.

You can instance a new shell inside sudo with: sudo sh -c "ls /Users/Jamie/Downloads/*.mp3"

3

Or you can use grep to filter your results. So

$ sudo ls -l /Users/other/Library/Keychains/*keychain
ls: /Users/other/Library/Keychains/*keychain: No such file or directory

will fail but using grep will work

 sudo ls -l /Users/other/Library/Keychains|grep .keychain
 -rw-r--r--   1 other staff    48K Jan 31 18:27 login.keychain

Additionally, grep allows you to search for multiple items with the -e option.

 $ sudo ls -laRh /Users/other/Library/Logs|grep -e Finder -e Google
  -rw-r--r--   1 other  staff    18K Jan 31 18:27 GoogleSoftwareUpdateAgent.log
  -rw-------   1 other  staff    56K Jan 31 18:27 .Finder_2014-06-25-165714.crash.plist
  -rw-------@  1 other  staff    52K Jan 31 18:27 Finder_2014-06-25-165714.crash

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .