Home > Software engineering >  chmod only files in current directory but not subdirectories and main directory
chmod only files in current directory but not subdirectories and main directory

Time:10-02

I am trying to write a small bash script where I will change file permissions in batch and I can't figure out how to change permissions only on files in current directory and leave directory it self intact and subdirectories with different permission.

A few examples I tried are not working and I either end up changing recursively or changing main directory also.

This is the structure:

/dir/main_folder/
/dir/main_folder/subfolder1
/dir/main_folder/subfolder2
/dir/main_folder/subfolder3
/dir/main_folder/file1.php
/dir/main_folder/file2.php
/dir/main_folder/file3.php

So I would like to chmod all files under main_folder but leave main_folder permissions intact and also any folders under main_folder and files under those subfolders intact. The subfolder1 and files inside that folder should be intact.

The find command like this: find /opt/lampp/htdocs -type f -exec chmod 644 {} \; will change all files in subfolders too

CodePudding user response:

You can use the -type and -maxdepth options to find, for example:

find * -maxdepth 0 -type f 

This will find all non-dotfile files in the current directory (it will exclude files that begin with . because they don't match the glob expression *). If you want all files, you could use:

find . -maxdepth 1 -type f

And if you don't want "the current directory", you can pass a directory path:

find /dir/main_folder/* -maxdepth 0 -type f
find /dir/main_folder -maxdepth 1 -type f

All of the above commands will print only files because of the -type f option.

  • Related