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

Solution 1:

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 }}')

Solution 2:

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.

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