Change permission on a file recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change permission on a file recursively
# 1  
Old 02-26-2014
Question [Solved] Change permission on a file recursively

Hi,

this is the structure of the directory

/local/home/app/cases

under cases directory, below are the sub directories and each directory has files.

/local/home/app/cases/1

/local/home/app/cases/2

/local/home/app/cases/3

/local/home/app/cases/4

File types are .txt .sh and so on. recursively, in each directory, I only like to change the .sh files permission to 744. can someone pls help me on this. thanksSmilie
# 2  
Old 02-26-2014
Code:
find /local/home/app/cases -iname '*\.sh' -type -exec chmod -c 744 {} \;

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 02-27-2014
You missed the type:
Code:
find /local/home/app/cases -iname '*\.sh' -type f -exec chmod -c 744 {} \;

Escaping the period is not really necessary, and if there is a lot of files, using xargs is more efficient -- it will run only one chmod call on many files. Using -0 will allow for filenames with white characters:
Code:
find /local/home/app/cases -iname "*.sh" -type f -print0 | xargs -0 chmod -c 744

This User Gave Thanks to mirni For This Post:
# 4  
Old 02-27-2014
Quote:
Originally Posted by mirni
You missed the type:
Code:
find /local/home/app/cases -iname '*\.sh' -type f -exec chmod -c 744 {} \;

Escaping the period is not really necessary, and if there is a lot of files, using xargs is more efficient -- it will run only one chmod call on many files. Using -0 will allow for filenames with white characters:
Code:
find /local/home/app/cases -iname "*.sh" -type f -print0 | xargs -0 chmod -c 744

It is even more efficient to leave xargs out of the mix and let find invoke chmod with multiple operands by using + instead of \; to terminate the list of -exec arguments:
Code:
find /local/home/app/cases -iname '*.sh' -type f -exec chmod -c 744 {} +

Note that the find -iname primary and the chmod -c option are extensions to the standards that are not supplied by all implementations of find and chmod. If your system doesn't have these features, as long as you don't have files that end with .sH, Sh, or .SH and don't care about seeing a status message for every file whose permissions were changed, the following command should work on any system that supports the basic requirements of the standards for chmod and find:
Code:
find /local/home/app/cases -name '*.sh' -type f -exec chmod 744 {} +

If you do have mixed case filename extensions and your system doesn't support -iname, let us know and we can help you modify the find to look for all four combinations of capitalization using multiple -name primaries.

Just out of curiosity, why do you want these files to have 744 rather than 755 permissions?
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 02-27-2014
man chmod

Quote:
-R, --recursive
change files and directories recursively
Code:
~/case$ tree
.
├── 1
│   ├── file.sh
│   └── file.txt
├── 2
│   ├── file.sh
│   └── file.txt
└── 3
    ├── file.sh
    └── file.txt

Code:
~/case$ chmod -c -R 744 {1..3}/*.txt
mode of `1/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)
mode of `2/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)
mode of `3/file.txt' changed from 0755 (rwxr-xr-x) to 0744 (rwxr--r--)

Quote:
chmod (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.
This User Gave Thanks to ni2 For This Post:
# 6  
Old 02-27-2014
Thanks everyone for their inputs.

the below commands done the job. Smilie
Code:
find /local/home/app/cases -iname '*\.sh' -type f -exec chmod -c 744 {} \;


Last edited by Franklin52; 02-27-2014 at 10:51 AM.. Reason: Please use code tags, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a script to create file permission change restriction

Hello, I am looking for a UNIX shell script which can help me for access restriction. 1) /home/ram, there are number file with .txt extension, which should be only owned "ram" user. like as below ls -lrt *.txt -rwx------ 1 ram dba 11 Jan 4 2015 PASS1.txt -rwx------ 1 ram dba 10 Jan 4... (8 Replies)
Discussion started by: mr.trilok
8 Replies

2. Shell Programming and Scripting

Change file permission of mounted drive Linux

I got a problem with the permission of mounted 2TB drive in my Linux/Mint system. All the files in any folder are with 777, which is not what I want. my fstab line for this disk is: UUID=90803E0C803DF974 /media/grape/Workspace1_ntfs ntfs auto,users,permissions 0 0 and blkid gave me: $> blkid ... (4 Replies)
Discussion started by: yifangt
4 Replies

3. Shell Programming and Scripting

Change filenames recursively

Hello, I made a mistake in a script and now need to go back and change allot of filenames. I need to change "v4" in filenames to "v3". I was thinking of something like this. #!/bin/bash FILELIST=$(ls -f -R *) for FILE in $FILELIST do # create new filename ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

4. UNIX for Dummies Questions & Answers

Change unix permission when I don't own the file

Hi, A file is transferred from a Windows server(say username : user1) to Unix server via ftp. In unix, the permission of the file for a user, say user2 will be "-rw-r-----". Since the user1 is the owner of the file, user2 is not able to change the file permission using chmod. Is there... (5 Replies)
Discussion started by: merin
5 Replies

5. Shell Programming and Scripting

Change the file permission

Guys, I need help. I need to change the .txt file permission after I have reset the file content to 0. The code that reset the file content to 0 is as follows: #!/bin/sh for i in /root/script/*.txt do echo "0" > $i done However, the file is generated by the apache application,... (3 Replies)
Discussion started by: jasperux
3 Replies

6. UNIX for Advanced & Expert Users

script to recursively change permissions on file and dirs differently?

Hi there, I need to change all files/dirs 1. all files with 744 2. all dirs with 755 is there a script for that ? thanks, thegunman (13 Replies)
Discussion started by: TheGunMan
13 Replies

7. Shell Programming and Scripting

Changing file permission recursively

I have a directory named DIR. The contents of the directory is something like: a.sh b.sh cghsk.sh assjsjkd gdshddll DFG/ ... ... Where only DFG/ is a folder. I want to grant execute permission to all(a+x), for all the files directly under the DIR directory except the files that... (4 Replies)
Discussion started by: proactiveaditya
4 Replies

8. AIX

Change file permission by anothere user !

Guy's we are in AIX 5.3 We have created two users user1 and user2 and they are under same group Staff Group user1 will create file under /tmp/ and this is the permission of this file -rw-r--r-- 1 user1 staff 1 Jun 13 09:47 file user2 is under same group and when he... (14 Replies)
Discussion started by: ITHelper
14 Replies

9. UNIX for Dummies Questions & Answers

How to change the default permission of a file

I am creating a file using the UTL_FILE command of oracle. This creates a file with the oracle user id. The file does not have permission for being read by any other user id. Is there a way that I can change this default permission. I tried using umask in the .login. Setting the umask to 022 works... (2 Replies)
Discussion started by: reachsamir
2 Replies

10. UNIX for Advanced & Expert Users

Timestamp of File permission change

Hi!! Experts, Is there any way to find the timestamp when the permission of a file was modified?? I mean no change to file contents.. Just the chnage of permissions. :) (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question