You are not logged in.
Pages: 1
Topic closed
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
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
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
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
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
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
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
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
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
use xargs -0 and such
To know recursion, you must first know recursion.
Offline
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
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
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
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
just wanted to say I find raymano solution more elegant
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
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.
Instead of wrapping the output via pipeline with sed you can wrap the quotes on xargs.
ls | xargs -I {} rm "{}"
Offline
Thanks for sharing. However, please note the age of this topic, and avoid bumping such old topics in the future.
Closing.
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline
Pages: 1
Topic closed