Change permissions for files

Hi!

I have a dir in a server, that receives files with the wrong permissions, so I decide to put on a cron entry that changes its permitions, but because of the time gap, not all of them get changed.
What I did was the following:

0,3,5,7,9,11,13,15,17,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59  * * * * find /dir/sub_dir -type f ! -perm a+rw -exec chmod a+rw  {} +

Can you help me to provide me with a better idea?

If the user account is only for delivering files and it fits to your environment, you could set a umask of 111 for this user to have all files with the same permissions that are created.

If you tend to use the cronjob, maybe just use another * instead of all these comma separated minutes. Have the output redirected to /dev/null to not spam your logs, ie. something like this:

* * * * find /dir/sub_dir -type f ! -perm a+rw -exec chmod a+rw  {} \; > /dev/null 2>&1

It could be you will need absolute paths to find and chmod etc. or maybe just put it into a little script (I personally prefer that) and call this script via cron.

Depending on how the files are deliviered, maybe you can set a umask with the file transfer tool you are using.

Hi
The files are sent by a mediation device using

ftp

---------- Post updated at 02:39 PM ---------- Previous update was at 02:34 PM ----------

Hi

I had an error when trying to change to your suggestion:

* * * * find  /dir/sub_dir -type f ! -perm a+rw -exec chmod a+rw {} \; > /dev/null 2>&1
crontab: error on previous line; unexpected character found in line.
crontab: errors detected in input, no crontab file generated.

As said, better put it into a script and have the paths to find and as absolute paths.

You can try to add the ftp command

site umask 111

when sending the file to have it the correct permissions. You can also have the ftpd a default umask in /etc/inetd.conf, but that will be for all transfers then.

Also you might want to think using scp/sftp if possible, since ftp is not encrypted.

the

umask

command is to run on the server that is sending the files?

No, on the receiving side in the users environment/profile but this will be default for all the user does.

Forget about umask, forget about the cronjob.

Try following:
Add in the ftp-call on the sending side adding

site umask 111

before you put or mput the files.

That should do the trick.

1 Like

The owner or sender says that cannot change permission using

umask

because its not recommended to change the permission of a raw CDR file

I need to chmod all files arriving in a landing directory every moment and them moved them with the new permissions to another directory.
I came up with the following code, please help:

#!/usr/bin/sh
a=`find /temp/in_ccn -type f ! -perm a+rw -exec chmod a+rw {} \;`
for i in $a ; do
mv /temp/in_ccn/$i /final_destination/dir
done

find generally slow's you down when you are dealing with larger directories.

rather you could do the below

chmod a+rw /temp/in_ccn/*
mv /temp/in_ccn/* /final_destination/dir/

Hi

Thanks ... but how do I make sure that the files I am moving are already chmod, and also not picking up the same files (duplicates)?

in that case

for i in /temp/in_ccn/*
do
chmod a+rw /temp/in_ccn/${i}
mv /temp/in_ccn/${i} /final_destination/dir/
done

Is it possible to add a line to delete the files already moved?

If a file is already available, it will be replaced when you use mv command.

If you still want to check and delete if the file already exists at destination,

for i in /temp/in_ccn/*
do
if [[ ! -f /final_destination/dir/${i} ]]; then
 chmod a+rw /temp/in_ccn/${i}
 mv /temp/in_ccn/${i} /final_destination/dir/
else
 rm /temp/in_ccn/${i}
fi
done
1 Like

Hi

I am having an error when executing the script:

 #!/usr/bin/sh
  

  for i in /var/tmp/temp/in_ccn/*
  do
  /usr/bin/chmod a+rw /var/tmp/temp/in_ccn/${i}
  /usr/bin/mv /var/tmp/temp/in_ccn/${i} /final_destinat/in_ccn/
  done

the error is:

sh change_file_perm.sh
chmod: WARNING: can't access /var/tmp/temp/in_ccn//var/tmp/temp/in_ccn/CCN1-CCNCDR44-04-Blk65536Blk-4237-20140226124610-2170
mv: cannot access /var/tmp/temp/in_ccn//var/tmp/temp/in_ccn/CCN1-CCNCDR44-04-Blk65536Blk-4237-20140226124610-2170
chmod: WARNING: can't access /var/tmp/temp/in_ccn//var/tmp/temp/in_ccn/CCN2-CCNCDR44-01-Blk65536Blk-4210-20140226124738-1675
mv: cannot access /var/tmp/temp/in_ccn//var/tmp/temp/in_ccn/CCN2-CCNCDR44-01-Blk65536Blk-4210-20140226124738-1675

Pls

That was my mistake.Below is the corrected code. Let me know if you get error again

#!/usr/bin/sh
  

  for i in /var/tmp/temp/in_ccn/*
  do
  /usr/bin/chmod a+rw ${i}
  /usr/bin/mv ${i} /final_destinat/in_ccn/
  done