I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry.<p>That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.)<p>And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo` without the hassle of changing the ownership back.<p>I never see `lndir` on these lists either. It makes a copy of a directory, but all of the non-directory files in the target are replaced with symlinks back to the source while directories are preserved as-is. Meaning that when you `cd` into it, you are actually landing in a copied structure of the source directory instead of the source directory itself, as would be the case if you just symlinked the source folder.<p>Once inside, any file you want to modify without affecting the original just needs you to create the symlink into a file, which you can do with `sed -i '' $symlink`. There you have it: effectively a copy of your original directory, with only the modified files actually taking up space (loosely speaking).<p>Looks like I have a few pull requests to submit.
Not one-liners, but some of the tools that I have found helpful for working with large bash codebases are:<p><pre><code> - shellcheck and shfmt for linting and formatting
- sub for organizing subcommands
- bashdb for debugging scripts interactively via the VSCode extension
</code></pre>
I'm still missing a way to share modules with others like it can be done with ansible/terraform, but I have not found an optimal way to do it yet.<p>[shellcheck] <a href="https://github.com/koalaman/shellcheck" rel="nofollow">https://github.com/koalaman/shellcheck</a>
[shfmt] <a href="https://github.com/mvdan/sh" rel="nofollow">https://github.com/mvdan/sh</a>
[sub] <a href="https://github.com/qrush/sub" rel="nofollow">https://github.com/qrush/sub</a>
[bashdb] <a href="http://bashdb.sourceforge.net/" rel="nofollow">http://bashdb.sourceforge.net/</a>
[vscode-bash-debug] <a href="https://github.com/rogalmic/vscode-bash-debug" rel="nofollow">https://github.com/rogalmic/vscode-bash-debug</a>
<p><pre><code> sed -i
</code></pre>
Watch out, that's a Linux-ism and macOS's sed will cheerfully use the thing after it as the backup expression; as far as I know, the absolute safest choice is to always specify a backup extension "sed -i~" or "sed -i.bak" to make it portable, although there are plenty of work-arounds trying to detect which is which and "${SED_I} -e whatever" type silliness<p>My contribution (and yeah, I know, PR it ...) is that I get a lot of mileage out of setting the terminal title from my scripts:<p><pre><code> title() { printf '\033]0;%s\007' "$*"; }
</code></pre>
and there's one for tmux, too<p><pre><code> printf '\033]2;%s\007' "$*";
</code></pre>
with the two infinitely handy resources:<p>* <a href="https://www.xfree86.org/current/ctlseqs.html" rel="nofollow">https://www.xfree86.org/current/ctlseqs.html</a><p>* <a href="https://iterm2.com/documentation-escape-codes.html" rel="nofollow">https://iterm2.com/documentation-escape-codes.html</a>
That's a nice and very extensive collection.<p>As a follow-up, I can also recommend Effective Shell series. I used to have navigation shortcut diagram from Part 1 (<a href="https://dwmkerr.com/effective-shell-part-1-navigating-the-command-line/" rel="nofollow">https://dwmkerr.com/effective-shell-part-1-navigating-the-co...</a>) printed out.
Learnt a neat trick from an old sysadmin colleague.<p>If you’ve written a command but realize you don’t want to run it right now but want to save it in your history you can just put a `#` in front of it (ctrl-a #) making it a comment and allowing you to save it in your history without running it.<p>When you’re ready to run it you find it and remove the preceding `#`
My favourite example in this page is used four times but not pointed out specifically. It's the use of <(some stuff) to create a temporary file descriptor. Since it's not explained I'll give it a quick go.<p>If you ever found yourself doing something like this:<p><pre><code> sort file1 > file1_sorted
sort file2 > file2_sorted
diff file1_sorted file2_sorted
</code></pre>
You can instead skip making two new files, and do:<p><pre><code> diff <(sort file1) <(sort file2)
</code></pre>
And voila - you've got two 'files' you are comparing, but without having to save them to disk. The examples on the page use this with `curl` and `head` to good effect, but it wouldn't necessarily be obvious what's going on.
> <i>$SHELL current shell</i><p>No, $SHELL is the user’s <i>default</i> shell; i.e. the shell started in a new terminal or when logging in on a console or remotely. If another shell program is started, $SHELL will still refer to the default shell, not the running shell program.
This is a rather long list so i'll just mention my latest favorite which I recently learned on HN and it has made my life very easy with bash<p>Whenever you need to use a single-quote on command line add a $ sign before it. It makes escaping everything super easy<p><pre><code> su user -c $'cd \'$dir\' && ...'
</code></pre>
Before this it used to confuse the hell out of me.<p>More details are here: <a href="https://stackoverflow.com/a/16605140/1031454" rel="nofollow">https://stackoverflow.com/a/16605140/1031454</a>
New users on my systems commonly ask me "what implements your pps process search?"<p>When the shell itself filters the output of ps, then removing a grep is unnecessary. Note this uses POSIX shell patterns, not regular expressions.<p>On a truly POSIX shell that does not support "local," remove the keyword, and change the braces to parentheses to force the function into a subshell.<p><pre><code> pps () { local a= b= c= IFS='\0'; ps ax | while read a
do [ "$b" ] || c=1; for b; do case "$a" in *"$b"*) c=1;;
esac; done; [ "$c" ] && printf '%s\n' "$a" && c=; done; }
$ pps systemd
PID TTY STAT TIME COMMAND
1 ? Ss 5:11 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
557 ? Ss 0:19 /usr/lib/systemd/systemd-journald
...</code></pre>
> Ctrl + x + Ctrl + e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands.
--<p>Great, I was just trying to remember that key combination the other day. Just got back to work after being for awhile for a child bonding leave.
See also "The Art of Command Line": <a href="https://github.com/jlevy/the-art-of-command-line" rel="nofollow">https://github.com/jlevy/the-art-of-command-line</a>
A really nice list, thanks a lot for sharing! :)<p>I'd add this when a filesystem gets almost full (but not overfilled, see below). This shows where most of the space goes:<p><pre><code> # du -axm / | sort -n | tail # takes a while on large filesystems, or ones with lots of files
</code></pre>
Then narrow down for each of the most filled directories:<p><pre><code> # du -axm /some/dir | sort -n | tail # subsequent searches are fast, now that metadata is cached.
</code></pre>
In case there is no space at all, <i>sort</i> will complain if the /tmp directory is on the same fs, then the only option is to search any suspect directories with du -sm $dir<p>And about this one: <a href="https://github.com/onceupon/Bash-Oneliner#using-ctrl-keys" rel="nofollow">https://github.com/onceupon/Bash-Oneliner#using-ctrl-keys</a><p>A bit surprised that the Ctrl+b(ack one...) and Ctrl+f(orward one char) shortcuts are not included.<p>As well as their Alt+b/f for a word back/forward too. Very convenient for going through a long command by getting in the beginning or the end of the line, then move words back/forth to update it.
<p><pre><code> Ctrl + s : to stop output to terminal.
Ctrl + q : to resume output to terminal after Ctrl + s.
</code></pre>
Who uses these? These are newb as well as advanced user killers that doesn't seem to serve a purpose.<p>It just makes you think your shell is locked up making you think either the server is out of memory, process is hung to the point no keys would react or the network is hosed.
Something I’ve been wanting to do but haven’t found a perfect solution for yet: storing the output of previous command in some variable by default.<p>I use Terminal.app’s Select Between Marks or tmux, but I wish this was a thing.
I love these one-liners. It's also about knowing your tools better.<p>I hadn't known about `look` [0], which is great.<p>The writer looks to be a bioinformatician, so it might be a bit out of scope, but I also found `socat` [1] quite a good serial communication helper tool.<p>[0] <a href="https://man7.org/linux/man-pages/man1/look.1.html" rel="nofollow">https://man7.org/linux/man-pages/man1/look.1.html</a><p>[1] <a href="https://linux.die.net/man/1/socat" rel="nofollow">https://linux.die.net/man/1/socat</a>
Related: the Warp terminal has the concept of "workflows" - which are a list of snippets you can pull and use with auto-complete. I found that to be a good way to remember those one-liners that I only use once every two months.
this is awesome, great work. this is really the first time i've opened up one of these "awesome list of X" repos and immediately learned a ton.<p>required reading for the bash newbie and mage alike. 10/10 will reference again and again.