Can you change permission to all files except one directory in Linux?

8

1

Is there any way to change all files/directories permission except one single directory in a single Linux command line command?

Rana

Posted 2012-09-17T18:00:25.783

Reputation: 193

grep -v "directory to exclude" – None – 2012-09-17T18:03:53.237

Answers

6

Assuming that you wish to set the permission bit 755 recursively for the contents of the folders in your current working directory, apart from the contents of the folder called "nameOfFolderToBeExcluded":

 chmod 755 -R $(ls | awk '{if($1 != "nameOfFolderToBeExcluded"){ print $1 }}')

p_strand

Posted 2012-09-17T18:00:25.783

Reputation: 679

4

You can use find to search for all the files that does not match the given filename and exec a command on all such files found as:

Assuming you need to exclude directory test and give file permissions 755 to all other files and directores. This would be excecuted from the top of the tree.

find ! -name test -exec chmod 755 {} \;

Tested

mtk@mtk4-laptop:$ touch a1.txt a2.txt a3.txt test
mtk@mtk4-laptop:$ ls -lrt
total 0
-rw-rw-r-- 1 mtk mtk 0 Sep 17 23:55 test
-rw-rw-r-- 1 mtk mtk 0 Sep 17 23:55 a3.txt
-rw-rw-r-- 1 mtk mtk 0 Sep 17 23:55 a2.txt
-rw-rw-r-- 1 mtk mtk 0 Sep 17 23:55 a1.txt
mtk@mtk4-laptop:$ find ! -name test -exec chmod 777 {} \;
mtk@mtk4-laptop:$ ls -lrt
total 0
-rw-rw-r-- 1 mtk mtk 0 Sep 17 23:55 test
-rwxrwxrwx 1 mtk mtk 0 Sep 17 23:55 a3.txt*
-rwxrwxrwx 1 mtk mtk 0 Sep 17 23:55 a2.txt*
-rwxrwxrwx 1 mtk mtk 0 Sep 17 23:55 a1.txt*
mtk@mtk4-laptop:$ 

The file permissions for file test remained unchanged. Same is applicable for directories.

mtk

Posted 2012-09-17T18:00:25.783

Reputation: 1 027

This only exclude files by that name -- not folders. Try mkdir test + touch a1.txt a2.txt a3.txt, and then execute the command. – Kraang Prime – 2017-01-08T05:58:17.323

2

What shell?

If you're running bash (likely if you're on Linux), you can check out extglob, which gives you more options for globbing, including the "negative glob" !()

shopt -s extglob
chmod 774 !(file-to-ignore)

Rich Homolka

Posted 2012-09-17T18:00:25.783

Reputation: 27 121

1

Using find more simply like this:

find <from_where_to_change> -not -path "*/<excluded_dir_name>*" [-and -not -path "*/<another_excluded_dir_if_you_want>*"] -exec chown <user>[:<group>] {} \;

In my case it was:

find /data/project -not -path "*/.svn*" -exec chown :www-data {} \;

This way I changed group on folder /data/project recursively, except all folders ".svn" recursively.

Zanshee

Posted 2012-09-17T18:00:25.783

Reputation: 11

0

Even easier way with basic commands:

chmod 755 -R $(ls -A|grep -v 'directory_name_to_exclude/*')

Vsevolod

Posted 2012-09-17T18:00:25.783

Reputation: 13

0

I am using below command, it will give files 644 and directory 755 to all files and directory in Present working directory but will exclude var directory to provide permission.

find . -not -path "*/var*" -type f -exec chmod -c 0644 {} \; && find . -not -path "*/var*" -type d -exec chmod -c 0775 {} \;

Akash Sharma

Posted 2012-09-17T18:00:25.783

Reputation: 1