Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)?

share|improve this question
add comment

4 Answers 4

up vote 21 down vote accepted

Use curly braces for it:

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

For more information read about brace expansion.

share|improve this answer
2  
Curly braces is the shit! Thanks! –  neu242 Dec 29 '11 at 22:46
add comment

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.

share|improve this answer
    
If you look closely, you'll see that I specify "without cd-ing to the directory first" in my question ;) –  neu242 Dec 30 '11 at 10:10
add comment

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.

share|improve this answer
    
I never said I'd parse ls, and Rush had a far better answer. Thanks for trying, though :) –  neu242 Dec 29 '11 at 22:48
add comment

You can filter using egrep:

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

To find all text files:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.