truncate deleted files from lsof

lsof|gawk '$4~/txt/{next};/REG.*\(deleted\)$/{printf ">/proc/%s/fd/%d\n", $2,$4}'
While the posted solution works, I'm a bit uneasy about the "%d" part. This would be hyper-correct approach: lsof|gawk '$4~/txt/{next};/REG.*\(deleted\)$/{sub(/.$/,"",$4);printf ">/proc/%s/fd/%s\n", $2,$4}' Oh, and you gotta pipe the result to sh if you want it to actually trim the files. ;) Btw, this approach also removes false negatives (OP's command skips any deleted files with "txt" in their name).

1
By: wejn
2014-03-11 10:40:32

1 Alternatives + Submit Alt

What Others Think

Now there are two versions of this command, but neither of you have explained WHY you'd do this. Deleted doesn't necessarily mean not-in-use. Lots of programs create then immediately delete temporary files, but keep a handle to them (to continue working). That means when the program closes, the files automatically disappear.
flatcap · 445 weeks and 6 days ago
@flatcap I figured jim80net had a reason for writing a thing like this. I merely took it as an exercise to make the one-liner more effective (and hopefully free of false negatives). As for the reason why you might want to do that... well, suppose you have a network server written in a garbage collected language (i.e. Java). The said server has a strong ref that prevents it from releasing a (now useless) fd. Perhaps a reference leak of some kind. So even if you delete the file it still doesn't free the resources allocated by that file (since there's open fd in the system). Truncating the file via /proc/$pid/fd/$fdno frees the allocated blocks... and allows you to keep the abovementioned server running (if you don't mind a stray open fd). Now, in my 15+ year career I've only used something akin to this a handful of times... but it sometimes happens that you really need to free allocated blocks from already deleted file without killing some kind of a service. It's not an everyday task... but i'd say that if you play long enough with UN*Xes, you're bound to stumble upon a situation that calls for this.
wejn · 445 weeks and 6 days ago
@wejn: Thanks for the reply. It's the sort of command that shouldn't be necessary... :-)
flatcap · 445 weeks and 6 days ago
My only comment is '{printf ">/proc/%s/fd/%d\n", $2,$4}' should be '{printf ":>/proc/%s/fd/%d\n", $2,$4}' I agree with flatcap as well, it's usually only called for from misbehaving or misconfigured programs. Next time I need it though, I'll use wejn's version, as I'm impressed by the gawk foo! =]
jim80net · 445 weeks and 6 days ago
@jim80net for bash ">file" works the same as ":>file". And I'm actually not fond of the latter, because ":" could be aliased or defined as some function. Reminds me of the notorious: :(){ :|:& };: fork bomb I'm wearing on my t-shirt. ;) If I had to stick something before the ">" character I'd probably go with /bin/true. ;) YMMV, though.
wejn · 445 weeks and 5 days ago
where to get shrooms dried mushrooms african pyramid mushroom african transkei mushroom albino penis envy b+ mushrooms blue meanies mushroom brazilian magic mushroom burmese mushroom golden teacher mushroom ko samui super strain malabar coast martinique mexican mushroom mushroom british columbia super thai mushroom nepalese chitwan mushroom penis envy texas penis envy 6 texas yellow caps vietnamese white jedi mind fuck tri coloured ecuadorian mushroom magic mushsroom edibles mexican cubensis psychedelic chocolate bar where to get shrooms dried mushrooms african pyramid mushroom african transkei mushroom albino penis envy b+ mushrooms blue meanies mushroom brazilian magic mushroom burmese mushroom golden teacher mushroom ko samui super strain malabar coast martinique mexican mushroom mushroom british columbia super thai mushroom nepalese chitwan mushroom penis envy texas penis envy 6 texas yellow caps vietnamese white jedi mind fuck tri coloured ecuadorian mushroom magic mushsroom edibles mexican cubensis psychedelic chocolate bar midnight mint psychedelic chocolate bar origin mango psilo gummies 0.25g-1g origin strawberry psilo gummies 0.25g-1g penis envy psychedelic chocolate bar psilocybin cube gummies vegan dark chocolate psychedelic chocolate bar microdoses brain capsules 50mg-200mg chill capsules 50mg-200mg clarity capsules 50mg-200mg health capsules 50mg-200mg love capsules 50mg-200mg mood capsules 50mg-200mg pure capsules 50mg-200mg social capsules 30 pack 350mg social capsules 5 pack 350mg buy weed online without medical card marijuana flowers types of concentrates cannabis cartridges buy edibles online legal marijuana shatter cannabis wax marijuana hash buy psychedelic online tinctures for anxiety cannabis seeds bank weed smoker accessories birthday cake kush acdc strain ace killer og strain ak 47 strain alaskan thunder fuck alien kush strain ambrosia kush strain atomik moon rocks cbd 40.57% atomik moon rocks diamond 58.77% banana og strain bc big bud strain birthday cake kush blue cheese strain blue dream og kush blue moonrock kush blue rhino strain blueberry kush strain bruce banner strain cannabis trim cherry pie strain cannatonic strain citrix strain dark star kush durban poison strain eleven roses strain g13 haze strain gelato strain girl scout cookies flavors girl scout cookies online granddaddy purple grape nana strain grape pie strain green crack strain guava kush strain hawaiian skunk jack herer strain mendo breath weed mighty mango mr nice guy weed blue mystic strain northern lights strain northern lights moon rock platinum og kush gas og strain pink og kush platinum hawaiian strain pootie tang strain purple mimosa strain purple white widow strain smart buds sour diesel weed strain sour moonrock strain space monkey tins strawberry banana weed sun rocks super citrus haze strain super silver haze wedding cake strain exotic white widows strain zombie kush strain hybrid weed indica strains sativa strains moon rock for sale cookies sfhaightst alien labs garrison lane gashouse grandiflora insane jokes up lemonade parlay runtz alien labs biskante 3.5g alien labs kryptochronic preroll alien labs kryptochronic smalls 7.0g alien labs melonade x sherbacio livesauce 1.0g alien labs xeno 3.5g alien labs xeno 5 pack prerolls alien labs baklava preroll berry pie cannatique sweet tartz 3.5g cbd flower 8th bag carrot cake cbd flower 8th bag lemon drop cbd flower 8th bag monk fruit cookies cream cookies gary payton 20 3.5g cookies kings road 3.5g cookies london poundcake 3.5g cookies sunset sherbert 3.5g cookies sweet tea 3.5g fiore pomelo anderson 3.5g fiore cherry cream pie 3.5g fiore guava berry 3.5g galactic gas london chello georgia pie juiced suds 3.5g snow man summer lane strain grandi melon hectane strain pineapple piss lemonade project 4516 ya hemi cookies/a> yadada strain insane og strain insane mac eleven strain insane pound cake strain insane rockstar strain lucky charmz baby kiwi chiclet lemonade lemon cookies strain lemonnade lychee lemonnade medellin cookies lemonnade sorbet lemonnade tangeray lions mane strain ooh la la indoor pineapple piss lemonade yellow fruit stripes lemonade jetlato strain mia runtz potato runtz runtz rock devine runt hawaiian runtz live resin runtz og obama runtz white runtz hawaiian runtz moneybagg runtz pink runtz mia runtz potato runtz blue m&m strain cherry cherry lemonade cherry pie og x jack the ripper cosmic cookies strain cutton candy kush mint midnight cannabis oreo cookies rasberry lemonade sunset strain white chocolate trufflez strains wonderbrett black truffle strain dope house trufflez london truffle strain dessert truffle weed pirate truffles weed strain purple truffles strain snow white truffle strain truffle cake strain truffle pie strain beyond blueberry black orchid grapes of warth lemon oz kush melon og orange banana oz kush pineapple og pink picasso strawberry bliss wonderbrett chomp banana pound cake marujuana apple fritter strain banana lato marijuana forbidden fruit strain got bars og by synergy kiwi strawberry strain papaya marijuana strain fruity pebbles marijuana golden state banana peanut butter mochi super og stiiizy mochilato liiit 3.5g
johnson20007 · 33 weeks and 6 days ago
Amazing! This blog looks just like my old one! It's on a completely different subject but it has pretty much the same layout and design. Wonderful choice of colors! Pug Puppies for Sale Near Me PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio
rahimhh21 · 5 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: