10

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

1
  • grep -v "directory to exclude"
    – user1655481
    Sep 17, 2012 at 18:03

8 Answers 8

8

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 }}')
1
  • 1
    Why not just use -I (--ignore) option of ls?
    – Ashark
    May 16, 2020 at 6:18
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.

1
  • This only exclude files by that name -- not folders. Try mkdir test + touch a1.txt a2.txt a3.txt, and then execute the command. Jan 8, 2017 at 5:58
3

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

1

Just a simple command will do :-

chmod 755 /x/y/!(abc)

Where x/y/z = path.

abc = name for the folder/file to be excluded

0

Even easier way with basic commands:

chmod 755 -R $(ls -A|grep -v 'directory_name_to_exclude/*')
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 {} \;
0

And more easily with basic commands:

chmod -R 755 $(ls -I folder_excluded)
1
  • 1
    Please edit your answer to explain it. Otherwise it may get deleted. Feb 23, 2023 at 5:33

You must log in to answer this question.

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