Exclusion to tasks that you run in Bash

Sometimes we need to execute a command, for example change the permissions of a folder and its content, however we want this change NOT to be applied to a specific file, or to a specific folder ... even when it is inside the folder above it. which we execute the command.

For better understanding, I have a folder (files) and within it 4 files (doc.txt, file.mp4, list.txt and thesis.doc), I want to change the permission of those files so that only the owner can access, do that except with lista.txt, that I want everyone to be able to see, that is, that they do not change their permissions.

Resumiendo:

  • doc.txt, file.mp4 and thesis.doc can ONLY be viewed by the owner
  • list.txt can be seen by everyone, that is, I don't want their permissions to vary.

To achieve this I can change the permissions of all the files and then also change the permissions of list.txt so that it has them as before. This would be two lines… but, as almost always in Linux, there is a way to optimize 

bash

Let's see how:

  1. To change the permissions that only the owner can access we will use: chmod 700
  2. To change the permissions on all the files in that folder (I remember it's called: files) we will use: records/
  3. To exclude the file list.txt we will use: /!(list.txt)

In other words, the final command would be:

chmod 700 archivos/!(lista.txt)

It's that simple, here is a screenshot of the permissions before the files in that folder, the execution of the command, and then how the permissions are:
exclude-chmod-bash

In case we want to exclude more than one file, for example lista.txt and further thesis.doc , we can separate those files with | … that is:

chmod 700 archivos/!(lista.txt|tesis.doc)

Isn't it simple? 

Well, nothing more to add, it is a simple thing but when working with large volumes of information, it can save the day 

i-love-bin-bash_by-kzkggaara