Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

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.