How to change permissions for a folder and its subfolders/files in one step?

后端 未结 19 1765
情歌与酒
情歌与酒 2020-11-22 13:20

I would like to change permissions of a folder and all its sub folders and files in one step (command) in Linux.

I have already tried the below command but it works

相关标签:
19条回答
  • 2020-11-22 13:54

    I think Adam was asking how to change umask value for all processes that tying to operate on /opt/lampp/htdocs directory.

    The user file-creation mode mask (umask) is use to determine the file permission for newly created files. It can be used to control the default file permission for new files.

    so if you will use some kind of ftp program to upload files into /opt/lampp/htdocs you need to configure your ftp server to use umask you want.

    If files / directories be created for example by php, you need to modify php code

    <?php
    umask(0022);
    // other code
    ?>
    

    if you will create new files / folders from your bash session, you can set umask value in your shell profile ~/.bashrc Or you can set up umask in /etc/bashrc or /etc/profile file for all users. add the following to file: umask 022

    Sample umask Values and File Creation Permissions
    If umask value set to   User permission     Group permission     Others permission
    000                         all              all                   all
    007                         all              all                   none
    027                         all          read / execute            none
    

    And to change permissions for already created files you can use find. Hope this helps.

    0 讨论(0)
  • 2020-11-22 13:55

    It's very simple.

    In Terminal go to file manager. example: sudo nemo. Go /opt/ then click Properties → Permission. and then Other. Finally, change to create and delete and file acess to read and write and click on button apply... And work.

    0 讨论(0)
  • 2020-11-22 13:56

    Use:

    sudo chmod 755 -R /whatever/your/directory/is
    

    However, be careful with that. It can really hurt you if you change the permissions of the wrong files/folders.

    0 讨论(0)
  • 2020-11-22 13:56

    For already created files:

    find . \( -type f -exec chmod g=r,o=r {} \; \) , \( -type d -exec chmod g=rx,o=rx {} \; \)
    

    For future created files:

    sudo nano /etc/profile
    

    And set:

    umask 022
    

    Common modes are:

    • 077: u=rw,g=,o=
    • 007: u=rw,g=rw,o=
    • 022: u=rw,g=r,o=r
    • 002: u=rw,g=rw,o=r
    0 讨论(0)
  • 2020-11-22 13:57

    If you want to set permissions on all files to a+r, and all directories to a+x, and do that recursively through the complete subdirectory tree, use:

    chmod -R a+rX *
    

    The X (that is capital X, not small x!) is ignored for files (unless they are executable for someone already) but is used for directories.

    0 讨论(0)
  • 2020-11-22 14:01

    Here's another way to set directories to 775 and files to 664.

    find /opt/lampp/htdocs \
    \( -type f -exec chmod ug+rw,o+r {} \; \) , \
    \( -type d -exec chmod ug+rwxs,o+rx {} \; \)
    

    It may look long, but it's pretty cool for three reasons:

    1. Scans through the file system only once rather than twice.
    2. Provides better control over how files are handled vs. how directories are handled. This is useful when working with special modes such as the sticky bit, which you probably want to apply to directories but not files.
    3. Uses a technique straight out of the man pages (see below).

    Note that I have not confirmed the performance difference (if any) between this solution and that of simply using two find commands (as in Peter Mortensen's solution). However, seeing a similar example in the manual is encouraging.

    Example from man find page:

    find / \
    \( -perm -4000 -fprintf /root/suid.txt %#m %u %p\n \) , \
    \( -size +100M -fprintf /root/big.txt %-10s %p\n \)
    
    Traverse the filesystem just once, listing setuid files and  direc‐
    tories into /root/suid.txt and large files into /root/big.txt.
    

    Cheers

    0 讨论(0)
提交回复
热议问题