how can I recursively run chmod -x?

6,930

There is no (good) reason ever for pictures to be executable.

Just run this command to fix them:

find /space/music -name "*.jpg" -exec chmod -x {} +

Bonus:

find /space/music -type d -exec chmod o-w {} +

Explanation:

Most of the command line is self-explanatory. The only part that need some insight is the ending. {} is replaced by the name of the files (resp directory) found that match the filter. Usually, the find command is ended with a protected semicolon \;.

However, this isn't efficient as the command is run once for each file/directory.

There is a well known workaround that pipes find output to xargs which concatenates the file names up to the maximum command line allowed but then there is an issue when the path contains spaces, which is more and more common even in the Unix/Linux world.

Again a workaround based on GNU specific extensions allows to properly handle these case. I choose a simpler approach using a POSIX (read portable) way to have find building itself the long command lines, i.e. {} is replaced by a suite of file names up to the maximum number allowed.

Share:
6,930

Related videos on Youtube

Luis Flores III
Author by

Luis Flores III

Updated on September 18, 2022

Comments

  • Luis Flores III
    Luis Flores III over 1 year

    So I have my music collection living on my 12.04.2 server. This Collection has traveled with me over the years from a Windows machine to a MacBook Pro, and then finally to where it is today on the server.

    I have noticed that for some reason all of my 'folder.jpg' (cover photos) are highlighted green on the terminal (signaling that they are executable). if I run chmod -x folder.jpg the terminal will then show them in a light purple (signaling some form of media file). Now i'm not worried about the color, per se, but I have two questions. a) Why are my .jpgs being given the executable bit? (Probably from Windows screwing with them, i'm sure), and b) how can I recursively run chmod -x *.jpg from the top of my music hierarchy?

    When I try chmod -R -x *.jpg from the top level, I get:

    mediausr@MediaSRV:/space/music$ chmod -R -x *.jpg
    chmod: cannot access `*.jpg': No such file or directory
    

    Bonus Question: All of the folders are colored for 'other writable', while new folders I create are colored correctly for 'directory'. I'm not super worried about this at all, as it does not involve things being executable, but you know, if someone could enlighten me on a recursive way of making the system see them as straight up directories, that would be awesome. :0)

    Thanks for all of your input.

  • Luis Flores III
    Luis Flores III almost 11 years
    Sadly the Bonus answer is not working...
  • Luis Flores III
    Luis Flores III almost 11 years
    edit: I ran find /space/music -type d -exec chmod 755 {} +(copying the permissions from a newly created folder, and everything looks great now.) Thank you so much. :0)
  • jlliagre
    jlliagre almost 11 years
    Answer updated to add explanations. Glad you fix the directory issue too. Reply fixed, I confused group for other.