Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a scenario where multiple users are connected to a server using SSH. I have a file which is read and written by all the users.

The file contains numbers like this -

14554
14887
23451
15135
15154
15165
1236
1516
15616
9821
98715

now is the file was created by user X and user Y tried to executing the following command he gets an error:

sed -i '/14554/d' /tmp/file.txt
sed: cannot rename /tmp/sedtm3Vhx: Operation not permitted

The file permissions reads as -

ls -l /tmp/file.txt
-rwxrwxrwx. 1 USERX USERX 63 Apr 25 11:58 /tmp/file.txt

However if the user Y opens the file, it is possible to read/write into the file. I think there are no permissions to delete or rename the file created by another user.

Will it be possible to remove this line which has the number 14554, without deleting or renaming file.

Note : Sed was good because it would give me atomic access in case of multiple users trying to read or write the same file. This is one reason, I would also not like the for loops in bash script to edit this file.

Thanks !

share|improve this question

migrated from stackoverflow.com Apr 25 '13 at 12:09

This question came from our site for professional and enthusiast programmers.

    
So your real question is that you cannot run sed on a file if another user has it open? –  Miquel Apr 25 '13 at 10:01
    
no, if another user has created this file which is shared by multiple users. Another user is not able to run sed on that file. –  Raj Apr 25 '13 at 11:21
add comment

2 Answers 2

up vote 1 down vote accepted

Miguel is right with the sticky bit. I reproduced your problem in /tmp/. Do not remove the sticky bit from /tmp. Just work one level deeper in /tmp:

    USERX$ mkdir /tmp/work
    USERX$ chmod a+rwx /tmp/work
    USERX$ echo "aaabbbcccddd" >/tmp/work/file
    USERX$ ls -l /tmp/workdir/file
    -rw-rw-r-- 1 USERX USERX 13 Apr 25 14:59 /tmp/workdir/file

    USERY$ echo "xxx" >/tmp/work/file
    bash: /tmp/workdir/file: Permission denied

This fails as '>' tries to open file with RW access and truncate it and then write the text but USERY has no write permission on the file. But the sed will work:

    USERY$ sed -i 's/a/x/g' /tmp/workdir/file
    USERY$ cat /tmp/workdir/file
    xxxbbbcccddd
    USERY$ ls -l /tmp/workdir/file
    -rw-rw-r-- 1 USERY USERY 13 Apr 25 14:59 /tmp/workdir/file

Nice problem to understand Unix file/directory permissions.

share|improve this answer
    
put all in /var/tmp; and then do chmod -R 777 /var/tmp/ and apply what-ever sed –  YumYumYum Dec 9 '13 at 8:21
add comment

You have rwx access on the file, which means you can edit its contents. However, what you're trying to do with sed, is storing things into a temporary file, and then renaming the file sedtm3Vhx.txt to file.txt. You are not changing the contents of file.txt, you are changing the directory entry for file.txt to point to the contents of sedtm3Vhx.txt and that requires write access to the parent folder.

Also, if you are using really /tmp and that's not an example, you need to contend with special permissions, since /tmp/ is by default sticky

UPDATE: Regarding sticky and the comments below. If you are in /tmp, I cannot reproduce your problem. Here:

tester@mypc:/tmp$ echo "hi" > text.txt
tester@mypc:/tmp$ chmod a+w text.txt 
tester@mypc:/tmp$ ls -l text.txt 
-rw-rw-rw- 1 tester tester 3 Apr 25 13:57 text.txt
tester@mypc:/tmp$ exit
miquel@mypc:/tmp$ echo bye > text.txt 
miquel@mypc:/tmp$ sed -i s/bye/hello/ text.txt 
miquel@mypc:/tmp$ ls -l text.txt 
-rw-rw-rw- 1 miquel miquel 6 Apr 25 13:58 text.txt
miquel@mypc:/tmp$ ls -ld /tmp
drwxrwxrwt 11 miquel miquel 3551232 Apr 25 13:58 /tmp

So either you can change the contents in the file and replace it with sed, or you can't do either...

share|improve this answer
    
I don't want to change the permissions on /tmp/, would it then be possible? –  Raj Apr 25 '13 at 11:34
    
So, if you are using temp, something's odd here. In a sticky directory, the user that can modify the contents of the file has to be able to delete it or rename it. I've added a terminal dump in the question –  Miquel Apr 25 '13 at 12:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.