Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, I want to apply chmod 777 to every file in some directory which name matches some regular expression. Sure I can do stuff like chmod 777 f*, but when it comes to something more complicated, I don't know how to do it. For example, I can't run chmod 777 *a{3,5}.

How can it be done?

share|improve this question
up vote 5 down vote accepted

You could use find's -regex to find files matching a regular expression and use xargs to chmod the matched files.

$ ls -l
total 320
-rw-r--r-- 1 user user 0 2011-10-01 09:38 baaaaaaat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 baaat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 baat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 bat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 boo
$ find ./  -regextype posix-egrep -regex '.*a{3,5}.*' -print0 | xargs -0 chmod 0777
$ ls -l
total 320
-rwxrwxrwx 1 user user 0 2011-10-01 09:38 baaaaaaat
-rwxrwxrwx 1 user user 0 2011-10-01 09:38 baaat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 baat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 bat
-rw-r--r-- 1 user user 0 2011-10-01 09:38 boo
$ 
share|improve this answer

You must log in to answer this question.

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