6 ms·
Bash-Oneliners: A collection of terminal tricks for Linux
- ramon156 2y agoI'd love to make a fish shell version of this but I'm far from advanced enough with fish, perhaps it's a good excuse to start learning professional fish(ing)
- noufalibrahim 2y agoI have a small section in my regular training for fresher developers where I teach shell. I sometimes wonder if it's needed anymore.
- DaSHacka 2y agoNot needed because they typically know as much already, because knowing how to use a shell is no longer useful?
- worksonmine 2y agoSecond I assume. It's frustrating how little my peers care about the magic of the shell.
- lukan 2y agoWell, what do they use instead?
- worksonmine 2y agoVsCode, some git GUI and whatever command they can copy/paste from the docs to run the project. Understanding the why isn't important. This means they stagnate and don't explore further, they don't even understand how much can be done with coreutils and pipes. To them the terminal emulator is for installing packages and running projects. Just trying to get them to use aliases is impossible and to them I'm a showoff for actually liking vim.
- ricktdotorg 2y agoso much of the orchestration for servers that we greybeards had to for our unixen back in the day was shell script-based. nowadays its puppet or terraform or ansible or capistrano or [...]
- anal_reactor 2y agoPresenter: I have changed this ten-line function to `xbdbwhh%¢|=€=%[©=¢==33` which is much shorter. It crashes for inputs that have four consecutive letters "j", but we don't have inputs that four consecutive letters "j". Audience: Why on Earth would you do this Bash community: FUCK YES
- hifromwork 2y agoAs a fish shell user, I'd like to notice that "bash oneliners" and "terminal tricks" are not the same. TFA mixes them freely, but makes it clear they're two different things, HN title kinda conflates them.
- BossingAround 2y agoWhat does TFA mean in this context?
- dijit 2y agoThe (Freaking|Fu**ing|Fine|Founding|First) Article. A way of referencing the article that has been submitted and these comments are discussing.
- BossingAround 2y agoProbably one of the most useless and pointless TLA I've come across so far. TIHI.
- baruz 2y agoIt derives from an expression deep in the history of Usenet or e-mail (hyphenated), when hapless newbies (two syllables) were told by harried sysops to RTFM, read the f* manual.
- aquafox 2y agoThere's already commandlinefu.com
- asicsp 2y agoYeah, https://www.commandlinefu.com/commands/browse/sort-by-votes https://www.commandlinefu.com/commands/browse/sort-by-votes is a treasure trove.
- chasil 2y agoStarting off at the top, "sudo !!" doesn't work outside of bash/csh. On BSD systems with a pdksh variant, "r" runs the previous history command. On Debian/Ubuntu, dash doesn't implement this. The list is problematic from the first entry.
- boffinAudio 2y agoI so rarely see my (oft-repeated) favourite BASH script hack - commenting ones command lines: $ mv -n ~/Desktop/*.pdf ~/Documents/PDF_Archive/ #pdfsync This command, which sweeps all the PDF files off my Desktop into a PDF Archive, is easily recalled with the key-combo Ctrl-R "pdfsync" - this is very convenient because it doesn't require me to add a custom shell script anywhere, in any particular path, and can also be used to refer to combinations of commands, for example: $ for i in some_collection_of_git_repos/ ; do ; pushd . ; cd $i ; git fetch --all ; popd ; done #refreshgits Ctrl-R "refreshgits", and voila: all of the repos I'm interested in get a full automatic refresh .. Command commenting is my favourite trick to teach ops newbies, too .. right after they learn about just how important "history > some_history.txt #historylog" is as a command, as well ..
- weikju 2y agoThat's a good one! I didn't know the comments also showed up in reverse search. TIL!
- hencoappel 2y agoAt this point why not create an alias or function in bashrc? Then is also portable to other setups?
- boffinAudio 2y agoWhere does it live? In a .bashrc I have to maintain? The point is, not having to create an alias or function, because the tool lives in history. None of these are that complicated that they deserve a dedicated shell script and chmod'ing, or presence in a cloneable repo, etc.
- dredmorbius 2y agoTools that live in history are fine and good ... until they roll out of history. Mind: I rely on shell history all the time, and I'm noting, admiring, and planning to adopt your commenting hack, it's ingenious. But once a particular shell hack / one-liner / function has become sufficiently useful and proven, I'll typically add it to a shell function file (~/.bash_functions, sourced from ~/.bash_profile or ~/.bashrc), or my scripts directory (typically ~/.bin/ for personal hacks, /usr/local/bin/ if it's something I'll use from multiple accounts. Also park that under git and back it up / archive it somewhere or several somewheres.
- sampling 2y agoPrevious discussion (2022) with 108 comments: https://news.ycombinator.com/item?id=31250275 https://news.ycombinator.com/item?id=31250275
- tripflag 2y agoI can warmly recommend making your own oneliners.txt over time, especially if you hop between boxes a lot. Some of the more hyper-specific entries in my own list has saved my bacon more times than I'd like to admit... https://ocv.me/doc/unix/oneliners/ https://ocv.me/doc/unix/oneliners/
- setopt 2y agoIf you don’t jump around a lot between boxes, having a large HISTSIZE can be sufficient: you can write inline comments after fancy one-liners, and use Ctrl-R to find keywords in the comments. Another option is to make functions, aliases, or scripts as a way of saving those tricks. Not applicable to all tricks, but stuff like “embed all referenced fonts in a PDF file using `gs`” or “get me the ISO 8601 week using `date`” is way easier to use when you wrap it like this.
- dijit 2y agoAlso related: bash_history is your best productivity tool; https://martinheinz.dev/blog/110 https://martinheinz.dev/blog/110 I have a weird story about that. When I was a junior sysadmin I worked for a company providing a SaaS solution for e-commerce. The environment I worked in was bare-metal (as was the style at the time), with a 2 tier network architecture, lots of internal tooling (mostly in Perl fwiw) and highly complex interactions between components (cache items recorded in MySQL, centralised job control through a pair of activeMQ servers that had a postgres proof of record etc). During this time, I fancied myself the growing expert, and thus I created the most gnarly one-liners you could imagine. But they did everything. I annotated them with a “function parameter” comment at the end $ if []; then x; fi ## FUNCTIONDOES: THING then I would control+R constantly from the bastion host to do my daily work. I had managed to automate a huge amount of common requests this way. The existing team was disgruntled and started leaving, eventually making me the most senior in the team. Of course, I also left. I kept good contact with the team that onboarded as I was leaving, and one day while drinking I was told (off-handedly but in no uncertain terms) that I was the author of “the bash_history”. After enquiring to what they meant, they indicated that after I resigned, someone found my bash_history file (because I had been onboarding them and they had seen my workflow) and it had been immortalised by: A) Being copied to everyones profile and placed in /etc/skel B) being chattr +i (immutable) I was honoured. and horrified.
- axkdev 2y agoI'd like to point out that, for example, Ctrl+a is not specific to bash, it's a readline[0] keybinding. A subset of those will work in the text box in which I am typing this comment. C-f and C-b work, even C-h, but not C-w. Perhaps this key combo is reserved for closing the firefox tab on windows? Anyway, I love readline and wish it would work on literally all text inputs. 0. https://en.wikipedia.org/wiki/GNU_Readline https://en.wikipedia.org/wiki/GNU_Readline
- Aperocky 2y agoHear the gospel: $ set -o vi
- gkfasdfasdf 2y agoBetter yet, set it in .inputrc https://www.reddit.com/r/archlinux/s/cYoHW9h9I0 https://www.reddit.com/r/archlinux/s/cYoHW9h9I0
- anotherevan 2y agoBetter still, /etc/inputrc The three lines that go in the top of /etc/inputrc on my boxes: set completion-ignore-case on set editing-mode vi set keymap vi-command For completion, I also set the following in the bottom of /etc/bash.bashrc export EDITOR=vi set -o vi
- Aperocky 2y agoTIL
- chasil 2y agoPOSIX specifically calls out "set -o vi" for any and all compliant shells. The .inputrc is specific to readline, and will not be honored by libedit (used extensively on MacOS and BSD-centric systems). "vi: Allow shell command line editing using the built-in vi editor. Enabling vi mode shall disable any other command line editing mode provided as an implementation extension. It need not be possible to set vi mode on for certain block-mode terminals." The standard does not specify an emacs option. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- prashp 2y agoRecently I have found that claude sonnet 3.5 is extremely good at coming up with bash one liners that do what you ask for fantastically.
- tripflag 2y agoPlease be extremely careful blindly trusting oneliners from GPT's! Plenty of horror stories, but I'll name a recent one as example: Someone wanted to use FFmpeg to (losslessly) convert video files from one container to another and then delete the original. ChatGPT made a script which created a list of filenames and passed them into a loop which executed ffmpeg on each file. Thus it ran headfirst into several edgecases, for example how FFmpeg by default (unless you -nostdin) consumes stdin byte-by-byte, which messed up the list of filenames. Of course it appeared to work fine, as it clearly was processing a bunch of files. Secondly, it forgot to map the input streams correctly, so several files came out with subtly missing tracks. But more crucially, it failed to check the returncode for each file, assuming everything went fine... And deleting the lone copy of several files which failed to convert. Of course there were no backups. So if you must rely on GPT's, then do so with extreme caution. Don't get fooled by their "self-confidence".
- deleted 2y ago[deleted]
- brendanfay 2y agoFwiw, Ctrl+l is not the same as clear. clear will exit with code 0, Ctrl+l will not exit with any code.
- extraduder_ire 2y agoI think it's "insert page break", technically and scrolling down the terminal is a result of that. "clear" actually clears the screen and the scrollback.
- Sohcahtoa82 2y agoA guy I used to work with aliased "fuck" to "sudo" because he would frequently run commands that require sudo, but forget sudo, and being able to type "fuck !!" was far more satisfying than "sudo !!"
- boffinAudio 2y agoThis immediately disqualifies the person from having operator duties, imho.