linux - [Solved-15 Solutions] How to set chmod for a folder and all of its subfolders and files in Linux Ubuntu Terminal ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to set chmod for a folder and all of its subfolders and files in Linux Ubuntu Terminal ?

Linux - Solution 1:

chmod -R 755 will set this as permissions to all files and folders in the tree. You can use the find command. For example:

To change all the directories to 755 (drwxr-xr-x):

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;
click below button to copy the code. By - Linux tutorial - team

To change all the files to 644 (-rw-r--r--):

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

Check the -R option

chmod -R <permissionsettings> <dirname>
click below button to copy the code. By - Linux tutorial - team
man <command name>
click below button to copy the code. By - Linux tutorial - team

So in this case:

man chmod
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

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 *
click below button to copy the code. By - Linux tutorial - team

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.

Linux - Solution 4:

You can use -R with chmod for recursive traversal of all files and subfolders.

You might need sudo as it depends on LAMP being installed by the current user or another one:

sudo chmod 755 -R /opt/lampp/htdocs
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

To set to all subfolders (recursively) use -R

chmod 755 /folder -R
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 6:

chmod 755 -R /opt/lampp/htdocs will recursively set the permissions. There's no way to set the permissions for files automatically in only this directory that are created after you set the permissions, but you could change your system-wide default file permissions with by setting umask 022.

Linux - Solution 7:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 8:

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 {} \; \)
click below button to copy the code. By - Linux tutorial - team

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

  • Scans through the file system only once rather than twice.
  • 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.
  • Uses a technique straight out of the man pages (see below).
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.
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 9:

Use this:

sudo chmod 755 -R /whatever/your/directory/is
click below button to copy the code. By - Linux tutorial - team

It can really hurt you if you change the permissions of the wrong files/folders.

Linux - Solution 10:

The recursive command is:

sudo chmod 755 -R /opt/lampp/htdocs
click below button to copy the code. By - Linux tutorial - team
  • -R: change every sub folder including the current folder

Linux - Solution 11:

Ensure that appropriate files and directories are chmod-ed/permissions for those are appropriate. For all directories you want

find /opt/lampp/htdocs -type d -exec chmod 711 {} \;
click below button to copy the code. By - Linux tutorial - team

And for all the images, JavaScript, CSS, HTML...well, you shouldn't execute them. So use

chmod 644 img/* js/* html/*
click below button to copy the code. By - Linux tutorial - team

But for all the logic code (for instance PHP code), you should set permissions such that the user can't see that code:

chmod 600 file
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 12:

chmod 600 file
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 13:

For Mac OS X 10.7 (Lion), it is:

chmod -R 755 /directory
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 14:

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.


Related Searches to - linux - linux tutorial - How to set chmod for a folder and all of its subfolders and files in Linux Ubuntu Terminal ?