Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Why doesn't this work?

rm $(gsettings get org.gnome.desktop.background picture-uri  | awk '{gsub("file://","");print $0}')
rm: cannot remove `\'/home/foggy/obrazky/wallpapers/24/Dark_Oxygen_by_Falco101.png\'': No such file or directory

Perhaps there is some character that rm doesn't accept?

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

Look closely and you'll notice that it has embedded single quotes, which you will have to remove yourself. I would change the awk to a sed:

rm $(gsettings get org.gnome.desktop.background picture-uri |
     sed -e "s/'//g" -e 's,file://,,g')

(Although actually I would look at exactly what the unmodified output of gsettings looks like first, so as to make something a bit more bulletproof.)

share|improve this answer
    
works. thanks a lot. –  Vojta May 11 '12 at 14:34
    
As noted by mgd in a comment to his answer, this isn't completely correct - filenames can contain single quotes, so stripping all of them out is wrong. –  Izkata May 11 '12 at 18:16
    
@Izkata, that (and a number of similar issues) is what I meant by the comment about looking at the actual output and bulletproofing. –  geekosaur May 11 '12 at 18:19
    
Well, since its purpose is just to delete a wallpaper it is not very important and I can live with possibility that there would be such a file that will not be deleted... –  Vojta May 12 '12 at 11:02
    
You are right, but another time it will not be a wall paper. So to strip off the ' chars correctly you should use see like this sed -e "s|^'file://\(.*\)'$|\1|g" or maybe easier to read sed -e "s|^'file://||g" -e "s|'$||". Note the ^ matches the beginning of the line and $ matches the end of the line. I will update my answer. –  mgd May 12 '12 at 11:54
add comment

It looks like you have an extra pair of single quote characters around your filename that need to be stripped off.

Here, I have a file named "hello":

$ ls
hello

Trying to delete it with extra single quotes like this:

$ rm "'hello'"

gives exactly the error message you see:

rm: cannot remove `\'hello\'': No such file or directory

the \' before and after the name are single quotes you need to strip off.

EDIT:

In your case (as another answerer has noted) the output of

gsettings get org.gnome.desktop.background picture-uri

returns a result like:

'file:///usr/share/backgrounds/Tie_My_Boat_by_Ray_Garc%C3%ADa.jpg'

So you want to strip the leading 'file:// and the ending '. This can be done correctly using sed like this (using only one expression with a group)

sed -e "s|^'file://\(.*\)'$|\1|g"

or maybe easier to read (using two simple expressions)

sed -e "s|^'file://||g" -e "s|'$||"

Please note:

  • ^ matches the beginning of the line
  • $ matches the end of the line.
  • any character can be used in sed to surround the search and replace expressions. Normally, you would use / but in this case it is easier to use something else, so I use |.
share|improve this answer
    
Thanks a lot for a quick and right answer. It works now. I will write here the result. –  Vojta May 11 '12 at 14:29
    
I cannot comment on the answer currently flagged as the correct one, so I will write my comment here. Please be careful not to strip out every single 'character as suggested but only the ones surrounding your file name (in the rare event that your file name contains a single quote -- seldom but seen). –  mgd May 11 '12 at 15:34
add comment

gsettings get org.gnome.desktop.background picture-uri returns a result like:

'file:///usr/share/backgrounds/Tie_My_Boat_by_Ray_Garc%C3%ADa.jpg'

Those single-quotes at the ends are your undoing :-)

I'll assume you're using awk for a reason, and give you two simple solutions which strip the quotes before feeding the output of gsettings to awk:

  • cut -d "'" -f2
  • tr "'" " "

Stick them in the middle and you're done, e.g.:

rm $(gsettings get org.gnome.desktop.background picture-uri |   
`cut -d "'" -f2` | sed -e "s/'//g" -e 's,file://,,g')

rm $(gsettings get org.gnome.desktop.background picture-uri |   
`tr "'" " "` | sed -e "s/'//g" -e 's,file://,,g')
share|improve this answer
    
Thanks. It took me a while to find out how to strip the quotes and you have even more solutions :) –  Vojta May 11 '12 at 14:41
add comment

This would work too (although not particularly secure):

eval rm $(gsettings get org.gnome.desktop.background picture-uri  | awk '{gsub("file://","");print $0}')
share|improve this answer
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.