Sending file name via |

Discussion in 'Command Line' started by Lukasz, Mar 30, 2014.

  1. Lukasz

    Lukasz New Member

    Messages:
    2
    Likes Received:
    2
    Trophy Points:
    3
    Hi,


    I have a problem with a chmod command.

    I want to find a file in my home directory that contains "o" as second character with find command and then change its permission for 222.

    I've tried using:
    But I've received error that chmod didn't get file name.

    How can I forward file name from find to chmod?

    Lukasz
  2. boatguy

    boatguy New Member

    Messages:
    4
    Likes Received:
    2
    Trophy Points:
    3
    Try
    Code:
    find /home/lukasz -type f -name "?o*"| xargs /bin/chmod 222
    Lukasz and ryanvade like this.
  3. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,260
    Likes Received:
    428
    Trophy Points:
    83
    The CHMOD command works with the file being listed last by default..
    Code:
    chmod [OPTION]... MODE[,MODE]... FILE...
    Lukasz likes this.
  4. MikeyD

    MikeyD Active Member

    Messages:
    232
    Likes Received:
    112
    Trophy Points:
    43
    You can do it all with one find command:

    Code:
    find /home/dir -name "?o*" -exec chmod 222 '{}' \;
    This however will apply chmod to ALL files the find command finds so beware. Often by default find will look recursively within all folders in the base directory.
    Lukasz and ryanvade like this.
  5. Lukasz

    Lukasz New Member

    Messages:
    2
    Likes Received:
    2
    Trophy Points:
    3
    Thank you guys. Both commands worked as I've wanted. I completely forgot about -exec option. :) It was an exercise that our teacher gave us before test and I also think this exercise is senseless because it changes all found files, just as MikeyD wrote, but who understands teachers? :)
    ZZs and ryanvade like this.

Share This Page