5

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?

4 Answers 4

10

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 |.
2
  • Thanks a lot for a quick and right answer. It works now. I will write here the result.
    – Vojta
    May 11, 2012 at 14:29
  • 1
    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, 2012 at 15:34
6

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.)

4
  • 1
    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, 2012 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, 2012 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, 2012 at 11:02
  • 1
    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, 2012 at 11:54
3

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')
2
  • Thanks. It took me a while to find out how to strip the quotes and you have even more solutions :)
    – Vojta
    May 11, 2012 at 14:41
  • 1
    What are the backticks around cut -d "'" -f2 and tr "'" " " for? If they're intended, I'm not sure what those commands are supposed to do. Looking at the markdown of this answer, it looks like they may be typos--block code formatting is achieved in a somewhat unusual way. If they're not intended, then this answer makes sense but fails with a (presumably escaped) ' in the output of gsettings. Then text gets cut off there. See also the related flaw in the accepted answer. Jan 5, 2017 at 2:42
0

This would work too (although not particularly secure):

eval rm $(gsettings get org.gnome.desktop.background picture-uri  | awk '{gsub("file://","");print $0}')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .