Results 1 to 4 of 4

Thread: assigning pwd to a variable

  1. #1
    Join Date
    Aug 2008
    Beans
    30

    assigning pwd to a variable

    well since I am a beginner today I learnt how to change permission recursively of files and folder in a directory

    using 'find foldername -exec chmod 788 {} \;'

    what i want to know how can i set a variable to pwd so that i got a directry and using a defined alias it changes the permissions of the directry contents?

  2. #2
    Join Date
    Apr 2012
    Beans
    5,920

    Re: assigning pwd to a variable

    1. If you want to execute 'find' relative to the current directory you can just use '.'

    Code:
    find . -name 'whatever' -type f
    2. To assign a variable would be something like

    Code:
    d="/path/to/dir"; find "$d" -name 'whatever' -type f
    3. To assign and use the pwd would be

    Code:
    d=$(pwd); find "$d" -name 'whatever' -type f
    but see (1)

    Be careful with recursive chmods... also 788 is not a valid octal mode

    PS chmod has a recursive mode built in - see man chmod - although the value of using 'find' is that you can apply it selectively (based on name, file type etc.)
    Last edited by steeldriver; December 29th, 2012 at 05:07 PM.

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,453
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: assigning pwd to a variable

    also bash provides you with the $PWD variable that doesn't require runnning pwd

    be careful with recursive chmods... also 788 is not a valid octal mode
    true that
    most likely you want separate chmods for files and dirs which requires find -type d/-type f
    if you want full access for dirs and read/write for files you need 7/+rwx eg 755 or 700, etc for dirs and 6/+rw for files, eg 644 or 600 etc.
    Personally i like to use +/-rwx notation, which allows to add remove a single permission without setting 2 others like in case of numeric notation.
    Last edited by Vaphell; December 29th, 2012 at 05:20 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Aug 2008
    Beans
    30

    Re: assigning pwd to a variable

    i have made an alias in .bashrc chmods with directory as
    d=$(pwd);
    alias chmods='find "$d" -exec chmod 755 {} \;'

    but if it try to run from terminal with sudo i get error saying
    sudo: chmods: command not found

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •