Linux Command One-Liners
commandlinefu.com
commandlinefu.com
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.alternative
>_ find . -empty -type d -delete
GNU find says this very specifically: "There are unavoidable security problems surrounding the -exec action..."
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"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.
For finding a process on a port, use ss
ss -pn| grep :22
for a listening process ss -lpn |grep :22Find 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
shuf -r -e --random-source=/dev/urandom {a..z} {0..9} {A..Z} | paste -d "" -s | fold -w 25
head -c 33 /dev/urandom | base64
It has 264 bits of entropy.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
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.
: 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
:; : bar "$(foo)"
: bar <(foo)
: < <(foo) bar
: bar | foo
will all still execute `foo`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 ">_".
A:\>_ Acorn Electron
BASIC
>_
or whatever, it's a standard prompt if you're old enough!I did a quick fix and replaced ">_" with "$". I will dig into CSS :before during the next update. Thanks for the idea.
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.
* http://www.compciv.org/unix-tools/
* https://www.commandlinefu.com/commands/browse/sort-by-votes
* https://linoxide.com/linux-how-to/linux-commands-brief-outli...
* https://github.com/learnbyexample/Command-line-text-processi... --> this is my own repo on text processing commands like grep/sed/awk/sort/pr/paste/etc
Bonus:
* https://github.com/jlevy/the-art-of-command-line
* https://stackoverflow.com/questions/68372/what-is-your-singl...
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
These should be used instead: https://git-scm.com/downloads/logos
Actually I do a similar thing by collecting useful commands in a git repository: https://github.com/TimDaub/commands
-1 for formatting. Can we at least get a monospace font here?
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
https://www.motowilliams.com/conditional-includes-for-git-co...
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... sort -VThanks! That's how it actually was in the past. Makes a huge difference. Fixed it.
Instead I recommend something like:
tail -f /some/file | perl -nle 'print scalar(localtime),":",$_'
and it a bit easier to type IMHO.
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?
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.
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.
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
/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)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?
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.
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.
It's like the bad-old days where every site had it's own markup though> Remove all spaces from all files in current folder
should really be "from all file names".
IMHO, entries like this are misleading for a beginner (a bad apple in the basket)
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?Wow it looks like every thread is full of spam comments, really unfortunate.
I'd rather search google.
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 :)