This question came to my mind last day when I wanted to free up some space on my HDDs and when it comes to what to delete (feel the pain), I wanted to know without lot of right-clicks which folders are the fattiest. As a good Windows user, I installed Total Commander, because some random Google result told me to do so.
Then I realised, heck, I have an entire UNIX environment on my PC (MSYS2), so maybe there is an utterly simple one liner command for achieve this. And guess what, it has: :)
$ du -sh * | sort -h
Life hack, place this in your .bashrc
file:
# Within the current directory, list all folders and files
# and print their size, then sort it from smallest to largest:
alias du='du -sh * | sort -h'
What are your UNIX pipeline commands, where you can combine some program's standard output and creating something wicked simple time saver?
Top comments (42)
The most common pipes I use are:
Here's a
realcommand I'm using in a demo in 5 minutes to show something that's hidden deep in our logs:EDIT: Unfortunately that doesn't work because of file buffering. Here's an actual working command for a log that is getting updated in live, but it does effectively the same thing:
Bonus: Here's a super evil one >:)
sort -u to optimize sort | uniq ;)
Unless you need to count the unique results: blah | sort -n | uniq -c
Cheers for the tip about grep options. I do a lot of grepping, but I think those options will save me heaps of time in the future.
I think process substitution is incredible helpful. Took me a few years to find out about it.
A super powerful command is
xargs
. Also make sure to have a look at the-I
and-p
flags, both really powerful.I just used these two features as part of an article I wrote,
Automate Your Mac Setup and Keep It Up to Date
Compare the output of two commands, get the result list and pass in one line to another command:
And some aliases I like:
If you are curious which commands you use a lot, you can find out like this:
What Are Your Most Used Shell Commands?
Oooh I feel like this is a weakness of mine. Looking forward to the responses
Examples:
The
Just Commit Everything
command:Having a shortcut to add and commit is priceless. I use this function with a useful commit message and commit often.
This might be very dangerous if you don't
git pull
after your commit. So I suggest:Thank you! I have updated my function.
I have something similar, but I'm inserting a timestamp as a commit message. Still totally useless. :)
I also replaced
origin
word in the git repo creation atgit remote add gitlab ...
. So if I pushing something to gitlab, I use thegitlab
command, which usinggit push -u gitlab --all
. I have similar to Github.Why do you use git? Just use dropbox
xargs
is amazing for just about anything that doesn't like piped results. Want to pull a bunch of Docker images in one command?So if I understand correctly, this is equivalent to the following?
I never thought of using
xargs
this way, good to know!I guess you could parallellize the loop by adding
-P 0
to thexargs
invocation.I didn't consider using parallelization, that's a great idea! Though, what happens with Docker if it pulls in two copies of the same image at once? If two images have the same dependency, will Docker deal with this parallel pull fine, or will it bork?
EDIT: Docker seems to handle this rather well; It just queues up the layer pulls in some arbitrary order. Still, I don't know if I entirely trust this, but I can't see a reason for this to not work (And I can see a reason that one might want this to work!).
This is cool! I never thought about that.
Some dumb and possibly dangerous variations I use:
I often use this one get the pid of the parents of zombie process.
Apart from that I often use
grep
,sort -u
,sed
.A funny one is to pipe
fortune
withcowsay
I have used perl in pipes a lot for when you need to change something easily using a regular expression.
$ find . -name "*Relevant*.pm" | xargs perl -pi -e 's/something/somethingelse/'
The find name part can be tweaked to suit your needs and the regular expression in the Perl part also.
I know it's about pipe commande but since I saw some other interesting elements without pipe in it, I want to share some of them that help me daily. Hope you don't mind.
I have multiple aliases in my
.aliases
but some also in my.gitconfig
more specific to git.My favorite one is :
You can find more of them into my .dotfiles repo on github