PDA

View Full Version : want all files in a particular folder to have permission 755



IAMTubby
June 4th, 2013, 10:36 AM
Hello,
Assume I have a folder called /home/IAMTubby/shellscrtipts .I want all files created in this folder to have permission 755.

Use case:
Otherwise, for every shell script I write, before running, it , I have to go and do a chmod +x myshellscript.sh or sh myshellscript.sh.

Thanks.

ofnuts
June 4th, 2013, 11:42 AM
I don't think it can be done (and that would be a nice security hole)... I circumvent the problem with a "newscript" script: creates a stub (shebangs for bash & python and I drop the one I don't need) in my ~/bin, sets it executable, and starts the editor on it.

Vaphell
June 4th, 2013, 01:00 PM
nautilus supports such a feature already. Whatever you put in your Templates dir will be accessible from rightclick/new document menu. Filename is used as the entry name and that barebone template file is simply copied to where you need the new instance. I have templates named 'Bash script.sh' and 'Python script.py' with hashbangs and permissions set, plus Libre office doc and spreadsheet.

IAMTubby
June 4th, 2013, 01:13 PM
I don't think it can be done (and that would be a nice security hole)... I circumvent the problem with a "newscript" script: creates a stub (shebangs for bash & python and I drop the one I don't need) in my ~/bin, sets it executable, and starts the editor on it.
ofnuts, thanks for the reply, but can't it be done by changing the folder properties or something? Sorry, don't know what's technically possible, just talking from an end-user point of view :). I couldn't understand the solution you gave, I'm sorry :(



nautilus supports such a feature already. Whatever you put in your Templates dir will be accessible from rightclick/new document menu. Filename is used as the entry name and that barebone template file is simply copied to where you need the new instance. I have templates for 'Bash script.sh' and 'Python script.py' with hashbangs and permissions set, plus Libre office doc and spreadsheet.
Vaphell, thanks, but I'm not running a nautilus sadly. Is there a solution by editing the folder properties ?

Morbius1
June 4th, 2013, 02:19 PM
What version of Ubuntu are you using? - 12.04, 12.10, 13.04, .....

There's a way to do this using bindfs but it different in 12.04 than in newer versions.

sanderj
June 4th, 2013, 05:51 PM
Hello,
Assume I have a folder called /home/IAMTubby/shellscrtipts .I want all files created in this folder to have permission 755.

Use case:
Otherwise, for every shell script I write, before running, it , I have to go and do a chmod +x myshellscript.sh or sh myshellscript.sh.

Thanks.

Put in a crontab entry * * * * *, and ... problem solved.

So: "crontab -e" and then


* * * * * chmod +x /home/IAMTubby/shellscrtipts/*.sh

HTH

IAMTubby
June 9th, 2013, 12:32 AM
What version of Ubuntu are you using? - 12.04, 12.10, 13.04, .....

There's a way to do this using bindfs but it different in 12.04 than in newer versions.
Morbius1, I'm very sorry for the late reply.
I'm running 11.04 but ideally I would like to have a solution which fits all versions, because I use other systems as well.

IAMTubby
June 9th, 2013, 12:37 AM
Put in a crontab entry * * * * *, and ... problem solved.

So: "crontab -e" and then


* * * * * chmod +x /home/IAMTubby/shellscrtipts/*.sh

HTH
sanderj, I'm sorry for the late reply, I added that line after doing a crontab -e. (I did make sure all my paths are correct.)


I then went and wrote a 1-line shell script in the same directory as above and tried executing once again as ./test.sh, but it still gave me the same Permission denied message.
Am I executing incorrectly ?

sanderj
June 9th, 2013, 07:54 PM
post the output of:


crontab -l
ls -al /home/IAMTubby/shellscrtipts/*.sh

Vaphell
June 9th, 2013, 08:52 PM
which DE/file browser do you use that it doesn't support the file templates?

IAMTubby
June 11th, 2013, 06:38 PM
post the output of:


crontab -l
ls -al /home/IAMTubby/shellscrtipts/*.sh

sanderj,
this is the output of my crontab -l command

I first did a export VISUAL=vi



droid@droid-Inspiron-1545:~/Desktop/shellscripts$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * chmod +x /home/droid/Desktop/shellscripts/*.sh

I then went and created a shell script called test.sh at this location, which has nothing but an echo "hello world";

On running the script, it gave me the following
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ ./test.sh
bash: ./test.sh: Permission denied

sanderj
June 11th, 2013, 07:09 PM
It works for me:

sander@hapee:~/shellscripts$ ll
total 12
drwxrwxr-x 2 sander sander 4096 jun 11 20:03 ./
drwxr-xr-x 41 sander sander 4096 jun 11 20:01 ../
-rw-rw-r-- 1 sander sander 12 jun 11 20:03 weg.sh
sander@hapee:~/shellscripts$ date
di jun 11 20:03:42 CEST 2013
sander@hapee:~/shellscripts$ date
di jun 11 20:03:58 CEST 2013
sander@hapee:~/shellscripts$ date
di jun 11 20:04:00 CEST 2013
sander@hapee:~/shellscripts$ date
di jun 11 20:04:01 CEST 2013
sander@hapee:~/shellscripts$ ll
total 12
drwxrwxr-x 2 sander sander 4096 jun 11 20:03 ./
drwxr-xr-x 41 sander sander 4096 jun 11 20:01 ../
-rwxrwxr-x 1 sander sander 12 jun 11 20:03 weg.sh*
sander@hapee:~/shellscripts$ ./weg.sh
Hallo
sander@hapee:~/shellscripts$


So, why doesn't it work for you?

What if you run


chmod +x /home/droid/Desktop/shellscripts/*.sh

from a terminal? Does it make the scripts executable?

JonnyPython
June 11th, 2013, 07:31 PM
Hi,
Can't you do something like chmod 755 *.* ?

Greets

IAMTubby
June 11th, 2013, 10:17 PM
It works for me:
So, why doesn't it work for you?
sanderj, thanks for the constant feedback.
But are the outputs which I posted giving expected values ?



What if you run
chmod +x /home/droid/Desktop/shellscripts/*.sh
from a terminal? Does it make the scripts executable?
Yes, that surely does, but weren't we trying to automate it in the first place?


Hi,
Can't you do something like chmod 755 *.* ?
Greets
Hello JonnyPython I tried doing as you said
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ vi new
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ ./new
bash: ./new: Permission denied

sanderj
June 11th, 2013, 10:32 PM
sanderj, thanks for the constant feedback.
But are the outputs which I posted giving expected values ?


Yes, that surely does, but weren't we trying to automate it in the first place?


Good, and: yes, absolutely, it should be automated. But if that does not work, first do it manual. And that works. Good. But I'm puzzled why the crontab does not work. Possibilities:
- crontab is not run at all
- crontab does run, but the entry does not work correctly
- you're mixing directory or file names. I'm still not sure you're sharing exactly the exact names and results ...

drmrgd
June 12th, 2013, 11:39 AM
I don't think it can be done (and that would be a nice security hole)... I circumvent the problem with a "newscript" script: creates a stub (shebangs for bash & python and I drop the one I don't need) in my ~/bin, sets it executable, and starts the editor on it.

I do the same, and think this is a much easier solution. With the crontab solution, you have to constantly update the directories that you process (assuming you script in more than 1 directory). Vaphell's solution is good too, but for me I usually am coding from the terminal. Here's an example of a quick and dirty shell script I use for creating new perl scripts from a stub template I have in my Dropbox dir so that I can access it from home or work:


template="/home/dave/Dropbox/scripts/templates/perlScriptTemplate.pl"
if [[ ! -f $template ]]; then
echo "ERROR: The Perl template file does not exist or has been moved"
exit 1
fi

if [ -z $1 ]; then
printf "No filename indicated\n"
printf "What filename would you like to give the new script: "
read filename
else
filename=$1
fi

cp "$template" ./"$filename" && chmod 755 ./"$filename"
vim ./"$filename"
exit


You can of course mess with the above and choose a different template and editor as you like. You could also get crazy and add commandline opts to choose which language you want to use as the template (perl, python, bash, C, etc) or whatever.

Hope that helps!

Meghnaad
June 13th, 2013, 07:27 AM
drmrgd's Solution is perfect.

I just want to add one more solution:

How about creating a Function & putting in your profile file (typically ~/.bashrc):



function CreateScript
{
if [ $# -ne 1 ] #Check if you've received exactly a single argument
then
echo "Pass One File name as argument...." >&2
echo "Usage: CeateScript filename" >&2
else
if [ -e $1 ] #Check if file with that name already exists if exists open it for editing if possible
then
if [ -f $1 ]
then
vim $1 #Open it for editing
else
echo "$1 is not a regular file can not edit it..." >&2
fi
else
touch $1 #Create an empty file with whatever name you want
chmod 755 $1 #Set the permissions you want
vim $1 #Start Writing Script
fi
fi
}


Now you can execute this function as follows:


CreateScript filename

No matter where you create the file with this function it will have 755 permissions
Also in function in place of touch you can make copy of a template file.

I have not thoroughly tested the script.

IAMTubby
June 18th, 2013, 03:40 AM
Good, and: yes, absolutely, it should be automated. But if that does not work, first do it manual. And that works. Good. But I'm puzzled why the crontab does not work. Possibilities:
- crontab is not run at all
- crontab does run, but the entry does not work correctly
- you're mixing directory or file names. I'm still not sure you're sharing exactly the exact names and results ...
sanderj,
I was out of town with no access to mail. These are the things I have done regarding crontab.
1.When I do crontab -l at the terminal, this is what I get

# Edit this file to introduce tasks to be run by cron.#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * chmod +x /home/droid/Desktop/shellscripts/*.sh





Possibilities:
- crontab does run, but the entry does not work correctly
- you're mixing directory or file names. I'm still not sure you're sharing exactly the exact names and results ...
2.The below 2 commands tell you about the path and contents of the folder on which I am trying to enforce the permission

droid@droid-Inspiron-1545:~/Desktop/shellscripts$ pwd
/home/droid/Desktop/shellscripts
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ ls
command-line concatenate exprOR hello increment minus-e multiline
droid@droid-Inspiron-1545:~/Desktop/shellscripts$


3.These are my commands to create a new script and execute it


droid@droid-Inspiron-1545:~/Desktop/shellscripts$ pwd
/home/droid/Desktop/shellscripts
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ vi test.sh
droid@droid-Inspiron-1545:~/Desktop/shellscripts$ ./test.sh
bash: ./test.sh: Permission denied


4.My script is as follows


#!/bin/bash
echo "this is a test to see if crontab is working

5.I'm getting some really weird scenarios
Sometimes it works when I open a terminal, write in the test.sh script, close the terminal, reopen the terminal to the required directory and then execute.
It's like it needs a terminal closing trigger to activate the permission or something.
But that's again only sometimes, some other times, the same steps do not work, and so I cannot rely on it.

6.
- crontab is not run at all
Is there something else to do, to run crontab, or would it just start running when the system boots up ?

Should I post any other output which may assist you ?
Thanks.

IAMTubby
June 18th, 2013, 03:43 AM
cp "$template" ./"$filename" && chmod 755 ./"$filename"
vim ./"$filename"
Hope that helps!
Thank you drmrgd, that's useful. :)
However, I would like to go with a crontab entry because we are not doing a chmod +x 755 explicitly there.



touch $1 #Create an empty file with whatever name you want
chmod 755 $1 #Set the permissions you want
vim $1 #Start Writing Script

Thanks Mehgnaad, appreciate your help. :)
But would like to go with a crontab entry.

spjackson
June 18th, 2013, 08:49 AM
5.I'm getting some really weird scenarios
Sometimes it works when I open a terminal, write in the test.sh script, close the terminal, reopen the terminal to the required directory and then execute.
It's like it needs a terminal closing trigger to activate the permission or something.
But that's again only sometimes, some other times, the same steps do not work, and so I cannot rely on it.

You do appreciate, don't you, that crontab entries are scheduled to run at set times, or set time intervals? The one you have scheduled (with * in every field) runs once each minute. If you save your script then try to run it before the minute has changed, your cron entry won't have had chance to run yet.

If you want to run it more frequently (e.g. every second) then you can't. Use one of the other suggested methods, or have a looping script:


while [ forAsLongAsYouWant ]
do
chmod da de da de da
sleep 1
done

Or use filesystemwatcher, or bindfs (which was suggested very early in this thread).

shawnhcorey
June 18th, 2013, 04:49 PM
Hello,
Assume I have a folder called /home/IAMTubby/shellscrtipts .I want all files created in this folder to have permission 755.

Use case:
Otherwise, for every shell script I write, before running, it , I have to go and do a chmod +x myshellscript.sh or sh myshellscript.sh.

Thanks.

See:
man find
man chmod

IAMTubby
June 19th, 2013, 07:42 AM
The one you have scheduled (with * in every field) runs once each minute.

Put in a crontab entry * * * * *, and ... problem solved.
Thanks a lot spjackson, no I didn't know that :) . I have now read up about cronjobs using crontab and appreciate sanderj's method. So basically, every minute we are doing a chmod +x to my shell scripts under that folder. Cool!

I just have one final question(/need confirmation really) regarding scheduling cronjobs. So since cronjobs are of this format
MIN HOUR DOM MON DOW CMD, just wanted to confirm that no one writes a cronjob with both the DOM(day of month) and DOW(day of week) both filled in right, it wouldn't make any sense if they were not consistent with each other ?(Assume June19 is a Wednesday and along with filling this, you also fill a Thursday(4) to the DOW column.
Something like * * 19 6 4 /execute-my-script.sh.
Just confirming.

Thanks a lot for the other replies too, I truly appreciate.

ofnuts
June 19th, 2013, 09:36 AM
Note that executing "chmod +x ..." on all the files each time resets their "change date" so they will be picked up by backup managers each time. You may want to change the flag only on files where it is not set yet (see the various filtering options of the "find" command.

trent.josephsen
June 19th, 2013, 11:55 AM
I just have one final question(/need confirmation really) regarding scheduling cronjobs. So since cronjobs are of this format
MIN HOUR DOM MON DOW CMD, just wanted to confirm that no one writes a cronjob with both the DOM(day of month) and DOW(day of week) both filled in right, it wouldn't make any sense if they were not consistent with each other ?


$ man 5 crontab

spjackson
June 19th, 2013, 01:00 PM
I just have one final question(/need confirmation really) regarding scheduling cronjobs. So since cronjobs are of this format
MIN HOUR DOM MON DOW CMD, just wanted to confirm that no one writes a cronjob with both the DOM(day of month) and DOW(day of week) both filled in right, it wouldn't make any sense if they were not consistent with each other ?
It depends what you mean by "make any sense", and precisely what you are trying to do. If you schedule a job for Sunday 1 January, then it won't happen next year, or in 2015 or 2016, but it will in 2017. If for some reason that was your intent, then that would be a way to express it.

So when you say "Assume June19 is a Wednesday...", well some years it is and some years it isn't.

Using this kind of combination is probably unusual but that doesn't make it nonsensical or pointless.

IAMTubby
June 19th, 2013, 01:10 PM
"Assume June19 is a Wednesday...", well some years it is and some years it isn't.
thanks spjackson, I didn't think about that. so basically 30 1 19 6 2 will execute at 1:30 on 19th June only in a year when it is a Tuesday.
So we represent this as a logical condition as follows


/* assume format : MIN HOUR DAYOFMONTH MONTHNO DAYOFWEEK script.sh */
if(currentmin==MIN && currenthour==HOUR && currentdayofmonth==DAYOFMONTH && currentmonth==MONTHNO && currentdayofweek==DAYOFWEEK)
{
system("./script.sh");
}

Thanks for clearing this. If not this year, but some year didn't strike me at all.

trent.josephsen
June 20th, 2013, 01:08 AM
Note: The day of a command's execution can be specified in the follow‐
ing two fields — 'day of month', and 'day of week'. If both fields are
restricted (i.e., do not contain the "*" character), the command will
be run when either field matches the current time. For example,
"30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st
and 15th of each month, plus every Friday.

Like I said...

spjackson
June 20th, 2013, 10:55 AM
Like I said...
Indeed. My mistake. Apologies. I have long held the mistaken impression that all fields are ANDed, Clearly not.

trent.josephsen
June 20th, 2013, 11:35 AM
Eh, understandable. I think that's the only exception.

IAMTubby
June 23rd, 2013, 09:32 AM
Like I said...
Thanks a lot trent.josephsen.