13

I want to list (or delete, or do some other operation) on certain files in a directory, like this:

$ ls /opt/somedir/
aa  bb  cc  aa.txt  bb.txt  cc.txt
$ ls /opt/somedir/(aa|bb|cc)  ## pseudo-bash :p
aa  bb  cc

How can I achieve this (without cd-ing to the directory first)?

4 Answers 4

22

Use curly braces for it:

ls /opt/somedir/{aa,bb,cc}

For more information read about brace expansion.

0
3

Under bash, run shopt -s extglob (or put it in your ~/.bashrc), and you can use additional patterns that provide regular expressions with an unusual syntax (inherited from ksh). You can use these patterns in ksh too, of course, and also in zsh after setopt ksh_glob.

$ shopt -s extglob
$ ls /opt/somedir/@(aa|bb|cc|doesnotexist)
/opt/somedir/aa   /opt/somedir/bb  /opt/somedir/cc

In zsh, you can directly use (foo|bar) as a pattern.

% ls /opt/somedir/(aa|bb|cc|doesnotexist)
/opt/somedir/aa   /opt/somedir/bb  /opt/somedir/cc

Note that the command is called with the full path. If you want to call the command with a short path, you'll need to change the directory somehow, and (cd /opt/somedir && somecommand aa bb cc) is by far the easiest way.

1
  • If you look closely, you'll see that I specify "without cd-ing to the directory first" in my question ;) Commented Dec 30, 2011 at 10:10
1

You can filter using egrep:

ls | egrep '(aa|bb|cc)'

To find all text files:

ls | egrep '(aa|bb|cc).txt'
4
  • 1
    dot will match every character, and this approach make it difficult to further operate on these files Commented Dec 29, 2011 at 12:22
  • 1
    I got the feeling that ls was just an example command, too. Commented Dec 29, 2011 at 14:23
  • Yup, you're right, I specified that I want to do perform an operation: listing files was just an example. Commented Dec 30, 2011 at 10:15
  • Good comments dear friends, thanks. Good luck neu242 :D Commented Dec 31, 2011 at 16:52
1

First, I direct you to: never parse ls, ever. The proper, and canonical way to do what you want is with find.

For example:

find /opt/somedir -regex '.*[aa|bb|cc].*' -exec mv '{}' ~/backup \;

I usually use -name instead of -regex as it is simpler. But a regex fits your use case. You should run the command without -exec the first time, to make sure they actually are the files you want moved/deleted.

1
  • I never said I'd parse ls, and Rush had a far better answer. Thanks for trying, though :) Commented Dec 29, 2011 at 22:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.