TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

439 pointsby bfmabout 3 years ago

19 comments

archduckabout 3 years ago
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&#x27;t work on MacOS; the only substitute I&#x27;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 &#x27;&#x27; $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.
评论 #31254851 未加载
评论 #31256953 未加载
评论 #31255183 未加载
评论 #31254298 未加载
评论 #31257173 未加载
评论 #31254443 未加载
评论 #31292109 未加载
bfmabout 3 years ago
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&#x27;m still missing a way to share modules with others like it can be done with ansible&#x2F;terraform, but I have not found an optimal way to do it yet.<p>[shellcheck] <a href="https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koalaman&#x2F;shellcheck</a> [shfmt] <a href="https:&#x2F;&#x2F;github.com&#x2F;mvdan&#x2F;sh" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mvdan&#x2F;sh</a> [sub] <a href="https:&#x2F;&#x2F;github.com&#x2F;qrush&#x2F;sub" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;qrush&#x2F;sub</a> [bashdb] <a href="http:&#x2F;&#x2F;bashdb.sourceforge.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;bashdb.sourceforge.net&#x2F;</a> [vscode-bash-debug] <a href="https:&#x2F;&#x2F;github.com&#x2F;rogalmic&#x2F;vscode-bash-debug" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rogalmic&#x2F;vscode-bash-debug</a>
评论 #31257490 未加载
mdanielabout 3 years ago
<p><pre><code> sed -i </code></pre> Watch out, that&#x27;s a Linux-ism and macOS&#x27;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 &quot;sed -i~&quot; or &quot;sed -i.bak&quot; to make it portable, although there are plenty of work-arounds trying to detect which is which and &quot;${SED_I} -e whatever&quot; 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 &#x27;\033]0;%s\007&#x27; &quot;$*&quot;; } </code></pre> and there&#x27;s one for tmux, too<p><pre><code> printf &#x27;\033]2;%s\007&#x27; &quot;$*&quot;; </code></pre> with the two infinitely handy resources:<p>* <a href="https:&#x2F;&#x2F;www.xfree86.org&#x2F;current&#x2F;ctlseqs.html" rel="nofollow">https:&#x2F;&#x2F;www.xfree86.org&#x2F;current&#x2F;ctlseqs.html</a><p>* <a href="https:&#x2F;&#x2F;iterm2.com&#x2F;documentation-escape-codes.html" rel="nofollow">https:&#x2F;&#x2F;iterm2.com&#x2F;documentation-escape-codes.html</a>
评论 #31253803 未加载
评论 #31253875 未加载
评论 #31254596 未加载
评论 #31252985 未加载
archielcabout 3 years ago
That&#x27;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:&#x2F;&#x2F;dwmkerr.com&#x2F;effective-shell-part-1-navigating-the-command-line&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dwmkerr.com&#x2F;effective-shell-part-1-navigating-the-co...</a>) printed out.
评论 #31253173 未加载
评论 #31252320 未加载
hamoschabout 3 years ago
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 `#`
评论 #31252065 未加载
评论 #31252216 未加载
评论 #31253807 未加载
评论 #31252739 未加载
评论 #31252937 未加载
评论 #31254362 未加载
hiisukunabout 3 years ago
My favourite example in this page is used four times but not pointed out specifically. It&#x27;s the use of &lt;(some stuff) to create a temporary file descriptor. Since it&#x27;s not explained I&#x27;ll give it a quick go.<p>If you ever found yourself doing something like this:<p><pre><code> sort file1 &gt; file1_sorted sort file2 &gt; file2_sorted diff file1_sorted file2_sorted </code></pre> You can instead skip making two new files, and do:<p><pre><code> diff &lt;(sort file1) &lt;(sort file2) </code></pre> And voila - you&#x27;ve got two &#x27;files&#x27; 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&#x27;t necessarily be obvious what&#x27;s going on.
teddyhabout 3 years ago
&gt; <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.
superasnabout 3 years ago
This is a rather long list so i&#x27;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 $&#x27;cd \&#x27;$dir\&#x27; &amp;&amp; ...&#x27; </code></pre> Before this it used to confuse the hell out of me.<p>More details are here: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;16605140&#x2F;1031454" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;16605140&#x2F;1031454</a>
评论 #31262801 未加载
chasilabout 3 years ago
New users on my systems commonly ask me &quot;what implements your pps process search?&quot;<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 &quot;local,&quot; 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=&#x27;\0&#x27;; ps ax | while read a do [ &quot;$b&quot; ] || c=1; for b; do case &quot;$a&quot; in *&quot;$b&quot;*) c=1;; esac; done; [ &quot;$c&quot; ] &amp;&amp; printf &#x27;%s\n&#x27; &quot;$a&quot; &amp;&amp; c=; done; } $ pps systemd PID TTY STAT TIME COMMAND 1 ? Ss 5:11 &#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;systemd --switched-root --system --deserialize 22 557 ? Ss 0:19 &#x2F;usr&#x2F;lib&#x2F;systemd&#x2F;systemd-journald ...</code></pre>
评论 #31257335 未加载
mgr86about 3 years ago
&gt; 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.
评论 #31257590 未加载
评论 #31255137 未加载
asicspabout 3 years ago
See also &quot;The Art of Command Line&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;jlevy&#x2F;the-art-of-command-line" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jlevy&#x2F;the-art-of-command-line</a>
ttanevabout 3 years ago
A really nice list, thanks a lot for sharing! :)<p>I&#x27;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 &#x2F; | 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 &#x2F;some&#x2F;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 &#x2F;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:&#x2F;&#x2F;github.com&#x2F;onceupon&#x2F;Bash-Oneliner#using-ctrl-keys" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;onceupon&#x2F;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&#x2F;f for a word back&#x2F;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&#x2F;forth to update it.
meksterabout 3 years ago
<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&#x27;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.
评论 #31296187 未加载
tambourine_manabout 3 years ago
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.
评论 #31252721 未加载
评论 #31252429 未加载
评论 #31256428 未加载
评论 #31259514 未加载
abetuskabout 3 years ago
I love these one-liners. It&#x27;s also about knowing your tools better.<p>I hadn&#x27;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:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man1&#x2F;look.1.html" rel="nofollow">https:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man1&#x2F;look.1.html</a><p>[1] <a href="https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;1&#x2F;socat" rel="nofollow">https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;1&#x2F;socat</a>
评论 #31254808 未加载
评论 #31257533 未加载
评论 #31253037 未加载
评论 #31257581 未加载
评论 #31254441 未加载
swahabout 3 years ago
Related: the Warp terminal has the concept of &quot;workflows&quot; - 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.
arittrabout 3 years ago
this is awesome, great work. this is really the first time i&#x27;ve opened up one of these &quot;awesome list of X&quot; repos and immediately learned a ton.<p>required reading for the bash newbie and mage alike. 10&#x2F;10 will reference again and again.
a_square_pegabout 3 years ago
This is a fantastic list - thank you for sharing.
dangomabout 3 years ago
Great training dataset for copilot.