0

I have two users and groups in Unix: user1:user1 and user2:user2.

I have a directory myDirectory with below details (ls -ltr)

drwxr-xr-x.  4 user2 user2   35 Apr 27 10:16 myDirectory

Now, I am running a script, myScript.sh as user1:

#!/bin/sh

whoami

if [ -f /myDirectory/*.tar.gz ]
then
    rm -f /myDirectory/*.tar.gz
fi

cp -f someDirectory/*.tar.gz /myDirectory/

However, I get the following error:

cp: cannot create regular file ‘/myDirectory/myTar.tar.gz’: Permission denied

What am I doing wrong? How can I fix this?

Vicky
  • 205

4 Answers4

1

You can do this with group permissions. You need to create a user group, add the two users, and change the group ownership of the target directory. First, create a group:

groupadd newusergroup

Then add the users to it:

usermod -G newusergroup user1
usermod -G newusergroup user2

Then change the permissions on the target directory:

chgrp newusergroup /myDirectory

This is all assuming you want both users to have access to the directory. If you only want user1 to have access, just change the permissions like this:

chown user1.user1 /myDirectory
dogoncouch
  • 539
  • 2
  • 7
  • Will user1 be able to access myDirectory if I change the group to root ? i.e. owner user2 but group root... user2:root ? – Vicky Apr 27 '17 at 03:40
  • No. It would need to be a group that user1 was in, and the root group is only for the user root. – dogoncouch Apr 27 '17 at 03:49
0

The group permissions to mydirectory are coming in your way of removing or creating a file in that directory.

as user1 run the following commnd first:

chmod g+w mydirectory
0

It would be better if the user who needs to do the writing, i.e. the user you are copying to, ran the script. That way, you would not have to grant special write permissions on the target directory.

Also, the test

if [ -f /myDirectory/*.tar.gz ]

is faulty. The -f test takes one argument, but if the pattern matches several files, you pass several arguments. Since you want to empty out the directory myDirectory of compressed tar archives, it's enough to

rm -f /myDirectory/*.tar.gz

The -f flag to rm will cause it to not error out if there is no files to delete.

Kusalananda
  • 333,661
-1

Well, I see many mistakes:

  1. You are are not allowed to write in myDirectory. Only user2 is allowed to write in there.
  2. You are mixing up absolute with relative paths. /myDirectory is a different directory than myDirectory. This could also cause the permission denied error, because your script is trying to copy it in the filesystem root folder myDirectory. If myDirectory is relative to the path from the current command line path (in which context the script is called), then ./myDirectory would be right. Otherwise consider to give us more detailed information where myDirectory is really located at. This is important, because the scope of relative path to myDirectory can differ in several ways. Without knowing your explicit case, an answer related to this would only be an general answer, that might not be applicable to your specific case.
  3. The [ and ] condition expression is only available in Bash, not in Shell. The definition of Shell depends also on your current working UNIX System. Typically, in Linux it points to the Bash, because Linux has no direct Shell implementation, neither BSD has it. Only Mac has its own direct Shell implementation. Therefore, Shell is just a theoretical UNIX standard, that ends up nowadays in many different dialects and interpretations of that theoretical document, that really depends on your current running UNIX system. So, use better directly Bash instead. This works 100 % on most UNIX platforms. I don't know any UNIX system, in which the Bash is not available.

Some other interesting points to think about: You may want to add a several group, which are user1 and user2. Then you can change the primary folder group of myDirectory to that group. That is as far as I know the common approach for that.

You may also use the Bash via the /usr/bin/env shebang command, which ensures that you really end up in any Bash:

#!/usr/bin/env bash

This is also an nice approach for other script languages like perl, php, etc.

alpham8
  • 61