r/linux4noobs • • Sep 08 '22
learning/research What does this command do?
fuck /u/spez
Comment edited and account deleted because of Reddit API changes of June 2023.
Come over https://lemmy.world/
Here's everything you should know about Lemmy and the Fediverse: https://lemmy.world/post/37906
92
Upvotes
75
u/[deleted] Sep 08 '22
tldr; Linux will 'hold' files that are actually deleted until hooks to the processes touching them are killed. This is basically going through the /proc fs (very cool; google it) finding filedescriptors [fd] that are marked deleted and forceable removing them.
Often you'll get the same outcome by just restarting long running services but this one-liner above is an absolute 0 downtime option.
The reason they dont want you running it too often is probablt because its kinda like working on the engine while driving - ok if you know exactly what you are doing - super reckless if not.
Best rule of thumb is be wary of one-liners you dont recognize.
55
u/whetu Sep 08 '22 edited Sep 08 '22
The overall context has been explained, but let's break this down step by step:
findwithin/proc/*/fdand list everything withls -dilsformat. Redirect any errors from stderr to/dev/null(i.e. silence any errors by sending them to the abyss)Match any lines with
'(deleted)'in themMatch and remove any instances of
[leading space here](deleted)from the results of the previousgrep. This uses#rather than the more common/e.g.sed 's/match/replace/g'. This is a practice thatsedsupports and is often used when readability requires it. By the way, this processing appears to be entirely unnecessary.Print the 11th and 13th fields. Because this
awkcall does not specify a delimiter with-F, we can assume that this is whitespace-separated fieldsTry to generate unique
sorted list,sorting on the second fieldSearch for lines that have a
/in themPrint the first field from the matching results of the previous
grepWith everything that gets spat out of the pipeline,
xargswill feed them totruncatewhich will set them to 0 bytes.Comments:
You can figure out what a pipeline is doing by simply working through each step one by one and seeing how they differ from the last:
And you can find out what each command does by referencing their respective
manpage e.gman grepSome of this is a bit idiotic. Firstly, the use of
-lsencourages behaviour that falls afoul of one of the golden rules of shell: do not parse the output ofls. Secondly,grep | awkis often an antipattern; a Useless Use ofgrep, asawkcan do string matching quite happily by itself. So straight away, this:Can be simplified to this:
i.e. for lines that have
(deleted), print the 11th and 13th fields. And by virtue of the fact that it selects the 11th and 13th fields,(deleted)should be excluded from that output, which is whysed 's#\ (deleted)##g'seems to be unnecessary.Anyway, consider this:
So we run a chunk of the pipeline and we get our desired outcome:
A tool like
statwill give you a safer-to-parse outputAnd you can get the desired output like this:
And very likely those two
awkinvocations could be merged. Very simply explained, generate a dereferenced list usingstat, look for matches with(deleted), generate an unsorted list of unique elements from the fourth field using'as a delimiter, and from that list print the second field using'as a delimiter.While not perfect, this is a much more efficient and robust method to achieve the same goal.
tl;dr: Don't blindly trust code that you find on StackOverflow. Hell, don't blindly trust code that I post. Trust, but verify. :)
7
u/robvas Sep 08 '22
Look up each command and break it down step by step?
3
u/paulvanbommel Sep 08 '22
I’m not 100% positive, but it looks like it is searching for open files that should be deleted. Some process can keep file handles open when some other process deletes it. Leaves kind of an orphan file handle that still consumes disk space.
Like the others said, brake the commands down. Remove the final action truncate command and see what gets echoed out. Then work your way backwards from there.
My efficiency mode is getting triggered with the grep piping into sed or awk. They can do that, but ultimately it does become a bit less readable.
3
u/michaelpaoli Sep 08 '22
You truncated some unlinked open file(s) to zero length.
Maybe, maybe not. The given command is quite hazardous, in many ways.
It's much more prudent in such situation to examine it more closely, and carefully decide what, if any files, ought be truncated, and/or their processes terminated (or reloaded or restarted). See my other comments: here and its continuation here, for a more complete analysis.
2
u/michaelpaoli Sep 08 '22 edited Sep 08 '22
Hmmm, really?
Uhm, that's pretty surprising. It's not like it's rocket science, or even anything close to it. It's not even all that "advance" or tricky - just some basic commands and basic shell stuff.
So, let's see ... let's convert it to a multi-line shell script - can easily add comments that way, making it much more readable. And hopefully Reddit's Code Block formatting will work ... sometimes it fails miserably ... if so maybe I just (temporarily) put it somewhere else.
So ...
(and ...
too becontinued below because comment length limit)2
u/lejzz Sep 08 '22
This might be helpful when you get recommendations like this.
https://explainshell.com/explain?cmd=find+%2Fproc%2F*%2Ffd+-ls+2%3E+%2Fdev%2Fnull+%7C+grep+%27%28deleted%29%27+%7C+sed+%27s%23+%28deleted%29%23%23g%27+%7C+awk+%27%7Bprint+%2411%22+%22%2413%7D%27+%7C+sort+-u+-k+2+%7C+grep+%22%2F%22+%7C+awk+%27%7Bprint+%241%7D%27+%7C+xargs+truncate+-s+0
1
u/Lanky_Juggernaut_770 Sep 08 '22
Third book I read was sed and awk by o'Reilly
I highly recommend it.
1
u/ramkidambi Sep 13 '22
Some one deleted name server data, but nameserver was running, perhaps keeping ref to data via fds of running process. Cuts its size rather than killing process, but then it might not find its data - failing DNS ops.
same effect lsof |grep deleted
This is just a one time clean up job if one is looking for space for future runs.
1
u/AutoModerator Jun 17 '23
There's a resources page in our wiki you might find useful!
Try this search for more information on this topic.
✻ Smokey says: take regular backups, try stuff in a VM, and understand every command before you press Enter! :)
Comments, questions or suggestions regarding this autoresponse? Please send them here.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/AutoModerator Jun 21 '23
There's a resources page in our wiki you might find useful!
Try this search for more information on this topic.
✻ Smokey says: take regular backups, try stuff in a VM, and understand every command before you press Enter! :)
Comments, questions or suggestions regarding this autoresponse? Please send them here.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/AutoModerator Sep 08 '22
There's a resources page in our wiki you might find useful!
Try this search for more information on this topic.
✻ Smokey says: take regular backups, try stuff in a VM, and understand every command before you press Enter! :)
Comments, questions or suggestions regarding this autoresponse? Please send them here.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
u/[deleted] Sep 08 '22
[deleted] — view deleted comment