1

How can I modify the pattern in the second instruction, so as to exclude nested directories? (such that ls returns only foo.mp4, not the content of bar:).

$ ls *
foo.mp4 foo.ogg

bar:
bar.ogg
$ shopt -s extglob
$ ls !(*.ogg)
foo.mp4

bar:
bar.ogg

PS: I use OS X.

3
  • 1
    ls does not automatically descend into subdirectories. Have you created an alias or function? What is the output of type -a ls? Commented Sep 11, 2016 at 15:21
  • $ type -a ls ls is /bin/ls
    – Erwann
    Commented Sep 12, 2016 at 0:19
  • Oh right. ls * will descend because you're explicitly naming the directory. My bad. Plain ls with no arguments will not Commented Sep 12, 2016 at 0:21

2 Answers 2

3

Use find rather than ls. With find you can exclude one or more glob-expressions. For your example (and OSX), you could do

find * -depth 0 -type f ! -name '*.ogg'  -ls

which produces something like an ls -l (on just files, given the -type f):

66294163        0 -rw-r--r--    1 tom              wheel                   0 Sep  9 20:12 foo.mp4

With OSX, the first column is the inode value, and then the link-count. find does not provide as many options for listing as ls:

$ ls -l
total 0
drwx------  3 thomas  wheel  102 Sep  9 15:33 com.apple.launchd.OFStJ79qtq
drwx------  3 thomas  wheel  102 Sep  9 15:33 com.apple.launchd.VQsV1ae6bI
drwx------  3 thomas  wheel  102 Sep  9 15:33 com.apple.launchd.e6HBMt2vnS
drwx------  3 thomas  wheel  102 Sep  9 15:33 com.apple.launchd.he9U4OAIMI
-rw-r--r--  1 tom     wheel    0 Sep  9 20:12 foo.mp4
-rw-r--r--  1 tom     wheel    0 Sep  9 20:12 foo.ogg

but can be useful.

1
  • Came here to recommend find with the ! bang operator for logical NOT. Have an upvote.
    – Matt Obert
    Commented Sep 10, 2016 at 1:55
0

What was I thinking?!

$ ls *.!(ogg)
foo.mp4

You must log in to answer this question.

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