You are not logged in.

#1 2007-04-18 16:55:09

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

How do I make rm follow symlinks? - SOLVED

I need a way to remove symlinked files. I have a honking number of symlinks in a directory. I just burned all this stuff to dvd, so now I want to remove it, but it's all over the harddrive, and if I do it by hand it's gonna take awhile (esp. since I need to do this or something similar nearly every day). So I either need a way to make rm follow symlinks (which I don't think is possible).
I tried doing something with awking the output of ls and piping this to "xargs rm", but got nowhere, fast.

Last edited by kamagurka (2007-04-18 19:56:22)


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#2 2007-04-18 17:25:17

nj
Member
Registered: 2007-04-06
Posts: 93

Re: How do I make rm follow symlinks? - SOLVED

The following command seems to work: (add the '| xargs rm' to actually delete of course)

ls -l | grep -- '->' | sed -e's/.*-> //'

You may run into problems if your files actually contain '->' in the filename, but I doubt that's an issue.

Offline

#3 2007-04-18 19:56:04

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: How do I make rm follow symlinks? - SOLVED

Absolutely incredible. Do I see it correctly that the sed command replaces everything up to and including the arrow with nothing? That's ingenious. I was trying to accomplish something similar with awk, but something never worked quite right.
Why the hell doesn't rm just have a "follow symlinks" switch?


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#4 2007-04-18 21:15:22

raymano
Member
Registered: 2006-10-13
Posts: 357
Website

Re: How do I make rm follow symlinks? - SOLVED

Here's another way. It actually checks to see if a file is a symbolic link:

for x in *; do if [ -L $x ]; then rm $x; fi ; done

Last edited by raymano (2007-04-18 21:15:48)


FaunOS: Live USB/DVD Linux Distro: http://www.faunos.com

Offline

#5 2007-04-19 08:27:36

F
Member
Registered: 2006-10-09
Posts: 322

Re: How do I make rm follow symlinks? - SOLVED

raymano wrote:

Here's another way. It actually checks to see if a file is a symbolic link:

for x in *; do if [ -L $x ]; then rm $x; fi ; done

Holy crap, thank GOD I wasn't root when I accidentially ran that command in /dev.

Offline

#6 2007-04-19 11:42:50

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: How do I make rm follow symlinks? - SOLVED

raymano wrote:

Here's another way. It actually checks to see if a file is a symbolic link:

for x in *; do if [ -L $x ]; then rm $x; fi ; done

he doesn't want to just delete the symlink, he wants to delete the file it's linking to as well.

Offline

#7 2007-04-19 13:01:00

klixon
Member
From: Nederland
Registered: 2007-01-17
Posts: 525

Re: How do I make rm follow symlinks? - SOLVED

how 'bout this one

ls -l | awk '/^l/ { print $9 " " $11}' | xargs rm

Or, to prevent the selection of symlinks to directories:

ls -l | awk '/^l/ && !/\/$/ { print $9 " " $11}' | xargs rm

To delete symlink and linked-to file in one go...

Last edited by klixon (2007-04-19 13:01:50)


Stand back, intruder, or i'll blast you out of space! I am Klixon and I don't want any dealings with you human lifeforms. I'm a cyborg!

Offline

#8 2007-04-19 17:46:02

raymano
Member
Registered: 2006-10-13
Posts: 357
Website

Re: How do I make rm follow symlinks? - SOLVED

Cerebral wrote:
raymano wrote:

Here's another way. It actually checks to see if a file is a symbolic link:

for x in *; do if [ -L $x ]; then rm $x; fi ; done

he doesn't want to just delete the symlink, he wants to delete the file it's linking to as well.

You are right. The above only deletes the links not what they point to. I miss read the question.


FaunOS: Live USB/DVD Linux Distro: http://www.faunos.com

Offline

#9 2007-04-20 18:19:28

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: How do I make rm follow symlinks? - SOLVED

I just discovered a fun little problem: If any of the files or paths contain spaces or special characters (like &), rm (of course) croaks. Is there a more pleasant way to solve this than replacing all of the offending characters with their escaped counterparts via sed?

Last edited by kamagurka (2007-04-20 18:22:43)


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#10 2007-04-20 18:45:54

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: How do I make rm follow symlinks? - SOLVED

use xargs -0 and such


To know recursion, you must first know recursion.

Offline

#11 2007-04-20 19:34:28

raymano
Member
Registered: 2006-10-13
Posts: 357
Website

Re: How do I make rm follow symlinks? - SOLVED

for x in *; do if [ -L $x ]; then rm -rf `readlink $x` && rm $x; fi ; done

Very dangerous though!


FaunOS: Live USB/DVD Linux Distro: http://www.faunos.com

Offline

#12 2007-04-21 23:03:19

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: How do I make rm follow symlinks? - SOLVED

lloeki wrote:

use xargs -0 and such

As I understand it, xargs -0 interprets newlines as \n, which futzes up the whole thing.


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#13 2007-04-22 11:07:50

lloeki
Member
From: France
Registered: 2007-02-20
Posts: 456
Website

Re: How do I make rm follow symlinks? - SOLVED

which futzes up the whole thing

-0 is not magical and not to be used alone.

try

'ls | xargs echo'

then

'ls | xargs -0 echo'

in a directory with filenames spaces.
I use it all the time to do stuff like

ls | xargs -0 echo  | sed 's/\(.*\)/"\1"/'

you can also use it with find -print0 and some other commands allowing the input/output of zero-bounded strings

Last edited by lloeki (2007-04-22 11:12:05)


To know recursion, you must first know recursion.

Offline

#14 2007-04-22 19:06:05

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: How do I make rm follow symlinks? - SOLVED

OK.

 ls -l | grep -- '->' | sed -e's/.*-> //'|xargs -0 echo| sed 's/\(.*\)/"\1"/'|xargs rm

That really and truly seems to do what I want. It's a lot longer than I thought the solution to such a simple problem would be, but it works.


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#15 2007-05-04 19:19:43

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: How do I make rm follow symlinks? - SOLVED

just wanted to say I find raymano solution more elegant smile
You just need to quote the readlink part for dealing with spaces :

for x in *; do if [ -L $x ]; then rm -i "`readlink $x`" && rm -i $x; fi ; done

pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

Board footer

Powered by FluxBB