Hacker News new | past | comments | ask | show | jobs | submit login
Linux Command One-Liners (commandlinefu.com)
338 points by arkj 11 months ago | hide | past | favorite | 93 comments



Nice list, here are suggestions for improving a few of the commands:

  Generate Random Passwords
  >_ < /dev/urandom  tr -dc _A-Z-a-z-0-9 | head -c6
This produces a 6-character password which only has 35 bits of entropy. It also doesn't print a newline, causing the password to be on the same line as the next prompt. Better to use something like:

  >_ < /dev/urandom  tr -dc _A-Z-a-z-0-9 | head -c12 && echo
Or the "pwgen" utility which can do this for you.

  Find the process that is using a certain port e.g. port 3000
  >_  lsof -P | grep ':30000'
This command will be extremely slow on large systems with a lot of open files. It is essentially listing every open file (the -P flag just inhibits port mappings from e.g. "port 22" to "SSH") and then grepping the output. Much better to run:

  >_  lsof -i :30000
Which is not only shorter, but much faster as it directly gets info about that port.

  Add your public SSH key to a server in one command
  >_  cat .ssh/id_rsa.pub | ssh hostname 'cat >> .ssh/authorized_keys'
This is what the "ssh-copy-id" utility, which is part of the openssh-client package, does already.

  Harder, Faster, Stronger SSH clients
  >_  ssh -4 -C -c blowfish-cbc
All three of those points are debatable with that command. Just use the defaults.


For generating passwords, I like this one:

  head -c 33 /dev/urandom | base64
It has 264 bits of entropy.


Or use 'shuf' for passwords:

shuf -r -e --random-source=/dev/urandom {a..z} {0..9} {A..Z} | paste -d "" -s | fold -w 25


The list contains a few different password generators, but I'm a little disappointed they didn't mention https://linux.die.net/man/1/apg


A couple helpful tips: pwgen is a package available on most linux's. It's great for generating passwords.

For finding a process on a port, use ss

  ss -pn| grep :22
for a listening process

  ss -lpn |grep :22


another,

Find biggest 10 files in current and subdirectories and sort by file size

>_ find . -type f -print0 | xargs -0 du -h | sort -hr | head -10

alternative,

>find ./ -printf '%s--%p\n'| sort -nr | head


Author of linuxcommandlibrary.com here.

You and some other commenters have a great knowledge. Can't wait to dig into it and add it to the page! Thanks a lot


>_ find . -empty -type d -exec rmdir {} +

alternative

>_ find . -empty -type d -delete


Please don't use -exec. Use -print0 and xargs -0 instead, as it's much safer, and correctly passes newlines and other problem characters. Even BSD adopted this idiom.

GNU find says this very specifically: "There are unavoidable security problems surrounding the -exec action..."


I'm pretty sure it's a bit faster too. Or for some reason I moved to it.


It would be faster because the exec takes place for every single result of the find.

Simple xargs builds the longest possible command line with as many find results as can fit.

If the -n option were used with xargs, it might slow down to similar performance as -exec, but it would still be safer.

Multiple processes can also be launched by the xargs, increasing performance by using multiple CPUs.

Here is a script using xargs -0 to launch 6 compression processes at a time on files specified to a shell script:

    #!/bin/sh

    N_CPUs=6

    EXT="xz"

    S="$(mktemp -t PARALLEL-XXXXXX)"

    printf '#!/bin/sh \n exec xz -9e "$1"' > "$S"

    chmod 500 "$S"

    if [[ -z "$1" ]]
    then echo "Specify files to pack into ${EXT} files."
    else for x
         do printf '%s\0' "$x"
         done | xargs -0 -P "$N_CPUs" -Ifname "$S" fname
    fi

    rm -f "$S"


> It would be faster because the exec takes place for every single result of the find.

Only if you end the exec with \;

If you end it with + then the results are built up and appended to the command much like xargs.


>_

This part will likely confuse people who are not familiar with shell and will create a bunch of thousands "_" files worldwide.

Quick explanation:

  >_ echo hello
or equivalently:

  echo hello >_
when executed in a shell creates a file named "_" with contents of "hello". Author seems to use ">_" as a terminal icon, but many will copy it as part of a command.

That is, if I'm not missing anything clever here.

Upd: copy button doesn't copy the >_ part, so the "icon" theory seems to be correct.


A ‘trick’ for fancy prompts containing information is to give them the form,

    : all the stuff;
As long as ‘all the stuff’ is parseable as shell words (which you can ensure by quoting with printf '%q' if there's doubt), then you can copy and paste entire lines.

The trivial copyable prompt would be

    :;


Note that this isn't guaranteed to be a no-op for arbitrary "all the stuff". Specifically it won't work for commands involving pipes, or commands involving command substitution / process substitution. That is:

    : bar "$(foo)"
    : bar <(foo)
    : < <(foo) bar
    : bar | foo
will all still execute `foo`


That's why I mentioned quoting.


Thank you for this!! TIL. Immediately adopting. Now wishing everyone else knew this too.


I read about this long ago, possibly in the Blit era, possibly in ;login: (other order, other reasons). It's old lore, but not widely known, so I try to mention in when the subject of prompts comes up.


Agreed, including the ">_" prompt in the command line isn't really helpful. The copy icon works, it's extra unnecessary cognitive overhead. Also, ">_" is an unusual prompt, I don't know what distro ships with that by default.


">" is the prompt on MS-DOS, and "_" is the cursor.

Somehow this has resulted in ">" or ">_" being the terminal "icon" even in unixy parts of the world; looking at the generic "utilities-terminal" icon: the default theme for Gnome 3 ("Adwaita") uses ">_" in the icon, the default theme for KDE ("Breeze") uses ">", and the runner-up ("Oxygen", which also comes bundled with KDE), also uses ">_".


That's unfortunate, because on Linux the shells tend to use ">" for a continuation prompt, where you can not enter complete commands.


Also, the SSH app on Chromebooks.


Author of linuxcommandlibrary.com here.

You are 100% right. That was quite dump. I think I got the idea from the SSH icon I used in the basics page.

Replaced ">_" with "$". Looks much better and I also updated some of the design and content on the site. Update is live.


the author should hide that "> _" by including it as a CSS :before or :after content, which by definition is not selectable.


Author of linuxcommandlibrary.com here.

I did a quick fix and replaced ">_" with "$". I will dig into CSS :before during the next update. Thanks for the idea.


Yes, I wonder what benefit there is to include `>_` as the prompt. The obvious drawback is confusing some poor souls. Also it's ugly, who would want to use `>_` as their prompt?


Am I some sort of weirdo because I don't recall seeing >_ used before as a prompt?


_ is the blinking cursor. Prompt is just >


But it doesn't blink.... > is kind of and odd prompt as well.


That is a default in some current and old Unix systems, as well as the tcsh shell.


I think it is from the dos/windows world.

  A:\>_


Or

    Acorn Electron
    BASIC
    >_
or whatever, it's a standard prompt if you're old enough!


It could have at least a less prominent color. And it could be added with a `::before` pseudo-element so you can't select it (just for decoration). Might be also annoying for screen readers without `aria-hidden`.


also it’s called a shell prompt for what it’s worth.




The oneliners are very useful, but the design of this website is awful. The content deserves better.


Yeah, I hate being that kind of guy but seriously... oneliners displayed over 5 lines because the columns are very small, commands in dark red on black...



Author of linuxcommandlibrary.com here.

Reasonable feedback. The design isn't great and can only get better from here one. I pushed some quick design updates with a new standard font, decreased the font size for the headline, slightly bigger columns and some other fixes and improvements


I wrote a shell script [1] that allows me to write my own mini-man pages to capture things exactly like this. Whenever I have to do something non-trivial on the command line that I will 1) likely need to do again at some point and 2) don’t do often enough to commit it to memory, I simply create a new little note. They are viewable in the terminal just like normal man pages so the notes are trivial to read once written. It’s worked extremely well for me!

[1]: https://git.sr.ht/~gpanders/ual


Nice, this is exactly what I needed. I had my one liners in Notable to look them up but this is way more seamless AND it integrates great with Notable at the same time with the UAL folder in the Notable notes directory and a slightly more complex UAL "edit" to add the Notable metadata.



Nitpick but the icon for GIT is actually that of Github.

These should be used instead: https://git-scm.com/downloads/logos


The comment above referred to the URL before they changed it to command-fu. The previous URL was: https://linuxcommandlibrary.com/basic/oneliners.html


Great idea! Already found some useful oneliners.

Actually I do a similar thing by collecting useful commands in a git repository: https://github.com/TimDaub/commands


+1 for content

-1 for formatting. Can we at least get a monospace font here?


Thanks for the feedback. sans-serif is the new standard font. Looks better you were right.


I think we should change the link to sort by votes all-time [1]. It's just fun to go through and see some of this stuff. The longer I use the terminal, the more of its power I come to respect. There's so many tools, so many parameters for each tool. It takes a really long time to build up muscle memory of what things you can do and remembering how to do them in that perfect situation.

Recently I've been trying to take at least 20 minutes a day to try and do things the tricky way by stringing these commands together. It's fun and the productivity wins of being able to string together several of these things in a pinch is fantastic.

Although it's definitely more fun than useful at this point~

[1] https://www.commandlinefu.com/commands/browse/sort-by-votes


Using conditional includes in your global gitconfig based on the path is a much less error prone way of having different identities for different projects.

https://www.motowilliams.com/conditional-includes-for-git-co...


My most obscure cool one liner...

1. Pointing your webserver to your current directory. Simplifies copying files to your local box.

    The simple python 3.x way...

    python -m http.server 8001

    And the original way I used to do it...

    function sfiles() { x=`ipconfig getifaddr en0`;echo $x:8001;ruby -run -ehttpd . -p8001; }

    And please use this with *extreme* caution because any files you've got access to on your remote box will have no authentication when they're exposed via HTTP.
My most frequently used one liner...

2. Regex searching files in directories.

    grep -inR <keyword> *

    The Silver Searcher, aka ag, is better but not natively installed so...


I'm unsure what the "sort dotted quads" one-liner is really supposed to buy you. Yes, you can define multiple keys and all that. But an IPv4 address in dotted notation is literally indistinguishable from a multi-part semantic version number.

    sort -V


I'd link to this site sorted by votes, rather than sorted by new: https://www.commandlinefu.com/commands/browse/sort-by-votes


Author of linuxcommandlibrary.com here.

Thanks! That's how it actually was in the past. Makes a huge difference. Fixed it.


There is an easy way to display re-display a lines with a time stamp. While I enjoy seeing people awk, the command is a bit terse.

Instead I recommend something like:

tail -f /some/file | perl -nle 'print scalar(localtime),":",$_'

and it a bit easier to type IMHO.


I fully agree: each time a command in awk or sed or bash becomes complex, it is the right time to switch to perl. Perl does not have all the limitations of (awk, sed, bash) while having concise syntax.


Does anyone have the original link this was submitted as? I looked at it earlier today and thought I should look at it again later but now it points to much less interesting commands.



I love the site, I was looking for something similar a while ago and I guess I found it today. As I spend most of my time in command line this website can really teach me new ways


I still primarily rely on StackOverflow for most of my shell one-liner needs, but I've found it extremely helpful to make a list/spreadsheet of the commands I repeatedly find myself having to google: https://github.com/dannguyen/bashfoo#toc


I save all my history and have a little script to grep it for commands I've used.

There was a thread on here about a shell add-on that would recommend one-liners, but I can't recall it ... anyone have any ideas?

Also I'd like a more robust version of my saved commands history (after 10 years or so it's getting to be like I want to make it a bit slicker), maybe backed by sqlite or something. Any suggestions?


Do you know about ctrl-R? At the prompt in bash, it lets you search your previous commands and edit and run them right there. Combined with a large HISTSIZE it's really useful.


Yes, thank you.

Also starting to type a command and using up-arrow to scroll through (I assume) endings of that command line that are in history. That's great too. But I often don't do, say,a do-release-upgrade (I use Kubuntu but prefer command line upgrade) within the time the command stays in history.


>_ cat <file> > /dev/null

It mentions that this command loads the file to RAM for faster usage. I do not understand how this works. How do you access the "RAM version" of the file.

Also, I thought that /dev/null was just an "abyss" you would send unwanted outputs.


You can't specifically access the cached version, but the OS will (assuming the file isn't too large) remember the blocks read from disk in the cache for a while (until the memory is needed by something else) so that next time the file is ready those blocks do not need to be read from disk. This can make a huge difference on traditional drives.

/dev/null is the abyss, but the cat command will read all the blocks comprising the file from the filesystem before sending them to that abyss.

You can see the effect like so:

  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m3.830s
  user    0m0.000s
  sys     0m0.468s
  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m0.165s
  user    0m0.004s
  sys     0m0.164s
  dspillett@swann2:/tmp$ time cat a.bigish.file > /dev/null
  real    0m0.148s
  user    0m0.000s
  sys     0m0.144s
(if "a.bigish.file" has only just been created, it will likely already be in RAM, run

  sync; echo 3 > /proc/sys/vm/drop_caches
as root or other privileged user to clear the not-currently-active cache & buffers, or chose an older file) (note though that this clears all cache machine-wide, not just cached information from your current session)


Thank you for the detailed explanation.

So any interaction with the file would put it in memory will do. Of course assuming that you interact with the whole file.

So if you have a big file and use "head", I guess it will not be enough. But you use "less" and scroll the whole thing you achieve the same result as in the command above right?


Basically, yes.

There are some complications:

* If the file is large then blocks at the start will be dropped from cache as you move further in, or will quickly be pushed out by IO activity elsewhere on your system.

* Some commands open files with options that inhibit caching, so not everything will have this effect.

> Of course assuming that you interact with the whole file.

Reading some of the file will have the effect for that portion of the file, so if subsequent commands only touch the same parts then the effect is the same.


Great explanation! Thank you!


Maybe is relying on the linux caching the file in memory? Then if you read it again is already in memory.

If so I don't think it is particularly useful, given that using the file anyway already puts it into the cache for later use

And yes, /dev/null is a abyss and everything written in it is discarded



Because you read the file, until the ram doesn't get purged, the file will be cached there (assuming it all fits)


Probably my favorite MacOS one liner (which is slightly different on Linux) is recursive search and replace)

find . -name "*.tsx" -exec sed -i "" -e "s/SomeVariable/ReplacementVariable/g" {} +

Use this so much with refactoring.

Also... wish HN supported at least a bit of markdown.


2 spaces is a code block - https://news.ycombinator.com/formatdoc


Thanks

  It's like the bad-old days where every site had it's own markup though


I LOVE stuff like this. Thanks!


These are neat, but is there an explanation for how/why these actually work? (Joke alert!) What sort of n00b just copies and runs random shell commands from the internet without understanding them? (End 'joke)


Who? Maybe all folks who use "curl ... | bash" or even "curl ... | sudo bash" which seems to be a trend lately?


Perhaps https://explainshell.com/ does the necessary?


These are great but, ideally one shouldn't have to leave the command line to get a cheatsheet, it would be great to get a TUI and scroll through bash snippets right from the command line. Is there such tool?


There seems to multiple `rename` commands - messy. https://wiki.linuxquestions.org/wiki/Rename


BTW:

> Remove all spaces from all files in current folder

should really be "from all file names".



> Get your external IP address > > >_ curl ip.appspot.com

IMHO, entries like this are misleading for a beginner (a bad apple in the basket)


Why misleading? I use `curl icanhazip.com` pretty regularly. Are you considering security issues of echoing the content to the console, or is something else the matter?


It would be nice to have these available as snippets on the command line with fuzzy search.


Its always good to have a cheat sheet


This seems to be a rip off of https://www.commandlinefu.com/

In fact, I just checked a few commands and descriptions on this site, and they are identical in formulation of both the command and the description.

Also, one of the first commands on this site is:

    New Maintainer for CommandLineFu
    >_ mail tech@commandlinefu.com
    
Why would it even mention this other site's name here unless it was a complete copy/paste of the commands from the other site?


Oh wow, can we get the link changed to https://www.commandlinefu.com/ instead?


https://www.commandlinefu.com/commands/view/25015/display-in...

Wow it looks like every thread is full of spam comments, really unfortunate.


SEO playing dirty. They have injected random content and a link. Most probably, they are profiting the reputation of commandlinefu.com, trying increase the google score of their sites.


Author of linuxcommandlibrary.com here

Yes I used the https://www.commandlinefu.com/site/api to get the data. I should have mentioned it on the site. And I will add in in the next update.

https://linuxcommandlibrary.com/basics.html has also lots of other info/tips to offer not just the one liners so it's not just a rip off of the particular website :)


tbh both sites UI is extremely user unfriendly.

I'd rather search google.


which one came first?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: