6

I have a Ubuntu 16.04 64 bit desktop in which I had installed wine in the past and later removed it. Now when I try to open txt files in nautilus, in many cases I get a dailouge box saying the file is executable and whether should I run or display it. I am annoyed by this behaviour and want to revoke execute permissions from these files. If anyone can show me how to do this it will be helpful.

Thanks

2
  • do you mean files with a .txt extension in your home?
    – Zanna
    Sep 23, 2016 at 5:57
  • @Zanna Yes, all *.txt files across all my partitions. However if a general method is shown to change permissions on a partition, I think I can do it for other partitions
    – nlern
    Sep 23, 2016 at 6:03

2 Answers 2

6

To remove the execute bit from all .txt files in your home directory and all its subdirectories, try:

find "$HOME" -type f -executable -name '*.txt' -execdir chmod a-x {} +

How it works:

  • find "$HOME"

    This starts find looking for files starting with your home directory and recursively including all subdirectories

  • -type f

    This tells find to look only for regular files, not directories.

    It is important to restrict the search to regular files because, if we remove the execute bit from a directory, that directory become inaccessible.

  • -executable

    This tells find to look only for files which have the executable bit set.

  • -name '*.txt'

    This tells find to look only for files whose names match the glob *.txt.

  • -execdir chmod a-x {} +

    This tells find to execute chmod a-x on any such files found.

    We use -execdir, as opposed to -exec, because it is more reliable should the names of directories change while this command is running.

    The + at the end of the form -execdir chmod a-x {} + tells find to run fewer instances of chmod by putting many files on each chmod command line if possible.

6
  • To be clear, I should first issue cd / then your command, right?
    – nlern
    Sep 23, 2016 at 6:21
  • 1
    Did you want to do this to all files starting from the root directory, /? Or, just your own files start files with your home directory. If the former, then use find / -type f -executable -name '*.txt' -execdir chmod a-x {} +.
    – John1024
    Sep 23, 2016 at 6:30
  • 1
    By the way, find searches for files starting with the directory specified on its command line. If an absolute directory is specified, like / or $HOME, the the value of the current working directory is ignored and cd has no effect. cd would only matter if a relative directory was specified. A common example would be find . -type f .... which starts its search from the current working directory.
    – John1024
    Sep 23, 2016 at 6:53
  • I replaced $HOME with the mounted partition paths where I have some txt files and used your command.
    – nlern
    Sep 23, 2016 at 7:25
  • 1
    this is not good. -executable is both non portable and "user centric", ie it will miss .txt files that are executable by other users than the one running the find but not executable by the user running the find. Just don't add this option (so every .txt file will have their "x" dropped, even if they already don't have any...), or replace it with : -perm +111 (non-gnu, or old gnu find) or -perm /111 (recent gnu find) to find files with ANY of the x set (u, g or other). Sep 23, 2016 at 12:32
0
find . -name "*.txt" -exec chmod a-x {} \;

find command is self explanatory. It finds the files according to given arguments. Usage : find location comparison-criteria search term

find . -name "*.txt" -exec chmod a-x {} \; Here . means root directory, -name "*.txt" means find all files which include ".txt" in their names. -exec chmod a-x {} \; means execute the chmod command on all matching files.

3
  • 1
    It would be nice if you add an explanation of your command does.
    – edwinksl
    Sep 23, 2016 at 6:31
  • if using GNU find : replace \; by + so that it will not fork one shell chmod command for EACH file, but only when the chmod has it's parameter list filled (so it is thousands of times less called, and therefore a lot more efficient). so "-exec chmod a-x {} \; means execute the chmod command on all matching files" should be "-exec chmod a-x {} \; means execute the chmod command on EACH matching file" Sep 23, 2016 at 12:36
  • Would you be so kind to explain what is an 'a' in 'a-x' and how to remove results of its execution? My folder is shown in 'Administrative Root' Aug 4, 2022 at 12:40

You must log in to answer this question.

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