TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

My Favorite One Liners

269 点作者 mr_o47大约 4 年前

39 条评论

someguy101010大约 4 年前
&gt; This commmand allows you to convert your shell output into an image as this makes it much easier than taking a screenshot of your shell if you want to share your output with someone.<p>Why is this becoming a more common practice? There is nothing more annoying than a picture of a block of text that I can&#x27;t copy, quote, or modify and send back to someone.
评论 #27025291 未加载
评论 #27025458 未加载
评论 #27026105 未加载
评论 #27025998 未加载
评论 #27028075 未加载
评论 #27026252 未加载
评论 #27024954 未加载
评论 #27027706 未加载
评论 #27026136 未加载
评论 #27027302 未加载
评论 #27026859 未加载
评论 #27026884 未加载
评论 #27025434 未加载
评论 #27025766 未加载
评论 #27026192 未加载
评论 #27027271 未加载
评论 #27030832 未加载
评论 #27024967 未加载
nicbou大约 4 年前
Great post idea!<p>&quot;python3 -m http.server&quot; is really useful when building simple JavaScript pages, or when you need a simple static file server for testing.<p>&quot;ss -p&quot; is great, but if you just want to see what&#x27;s hogging your bandwidth, iftop and nethogs are much better.<p>My favourite one-liner is &quot;open .&quot;, to open the current directory in Finder. The open command can also open URLs and other files.<p>I also have an alias for &quot;osascript -e &#x27;display notification &quot;&#x27;&quot;$1&quot;&#x27;&quot;&#x27;&quot;, which will display the text you choose in a MacOS notification. It&#x27;s useful when you need to be notified at the end of a long-running task. &quot;printf &#x27;\a&#x27;&quot; is also useful if you need the terminal to &quot;ding&quot;.
评论 #27024971 未加载
评论 #27025109 未加载
评论 #27025643 未加载
评论 #27026203 未加载
评论 #27026236 未加载
评论 #27026184 未加载
评论 #27024992 未加载
评论 #27025772 未加载
评论 #27026270 未加载
评论 #27027533 未加载
lqet大约 4 年前
Nice list, but the descriptions are written like they describe the next command, not the previous one. I would not recommend doing this to cat a bunch of files ;):<p>&gt; If you want to cat bunch of files at once you can this command.<p>&gt; rm -f !(test.txt).
评论 #27025615 未加载
评论 #27024594 未加载
m-hilgendorf大约 4 年前
On MacOS<p><pre><code> .&#x2F;very-long-task.sh &amp;&amp; say &#x27;success&#x27; || say &#x27;fail&#x27; </code></pre> I use it when I need to context switch and leave something running. `espeak` is an alternative for `say` on Linuxes.
评论 #27029061 未加载
评论 #27026271 未加载
评论 #27028451 未加载
评论 #27040714 未加载
nickjj大约 4 年前
Here&#x27;s a few functions &#x2F; aliases I have set up that I use on a pretty regular basis:<p><pre><code> # Generate a random password and copy it to the clipboard. pw () { pwgen -sync &quot;${1:-48}&quot; -1 | xclip } # Get the current weather. weather () { curl https:&#x2F;&#x2F;wttr.in&#x2F;&quot;${1}&quot; } # Get the numeric value of a file &#x2F; directory&#x27;s permissions. alias octal=&quot;stat -c &#x27;%a %n&#x27;&quot; </code></pre> All of my aliases are listed in my dotfiles at: <a href="https:&#x2F;&#x2F;github.com&#x2F;nickjj&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;.aliases" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nickjj&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;.aliases</a>
评论 #27028860 未加载
lqet大约 4 年前
My favorite one liner for a pretty and compact graph-like git log:<p><pre><code> git log --graph --abbrev-commit --decorate --format=format:&#x27;%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n&#x27;&#x27; %C(white)%s%C(reset) %C(dim white)- %an%C(reset)&#x27; --all </code></pre> Add a global alias:<p><pre><code> git config --global alias.lg &quot;log --graph --abbrev-commit --decorate --format=format:&#x27;%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n&#x27;&#x27; %C(white)%s%C(reset) %C(dim white)- %an%C(reset)&#x27; --all&quot; </code></pre> Usage:<p><pre><code> git lg</code></pre>
评论 #27040921 未加载
评论 #27024625 未加载
评论 #27024416 未加载
TheAceOfHearts大约 4 年前
On macOS you can read and write to the clipboard with pbcopy and pbpaste:<p><pre><code> $ ls | pbcopy $ pbpaste &gt; out.txt </code></pre> You can also put this in a function in order to get the path of the frontmost Finder window:<p><pre><code> osascript 2&gt;&#x2F;dev&#x2F;null -e &#x27; tell application &quot;Finder&quot; return POSIX path of (target of window 1 as alias) end tell&#x27; </code></pre> Get the current Finder selection:<p><pre><code> osascript 2&gt;&#x2F;dev&#x2F;null -e &#x27; set output to &quot;&quot; tell application &quot;Finder&quot; to set the_selection to selection set item_count to count the_selection repeat with item_index from 1 to count the_selection if item_index is less than item_count then set the_delimiter to &quot;\n&quot; if item_index is item_count then set the_delimiter to &quot;&quot; set output to output &amp; ((item item_index of the_selection as alias)\&#x27;s POSIX path) &amp; the_delimiter end repeat&#x27;</code></pre>
评论 #27054311 未加载
评论 #27030924 未加载
评论 #27030089 未加载
whalesalad大约 4 年前
On macOS you can use “pbcopy” and “pbpaste” to manipulate or read from the clipboard.<p>For instance, format the JSON in your clipboard: pbpaste | jq .
评论 #27026012 未加载
评论 #27029833 未加载
评论 #27054322 未加载
评论 #27024725 未加载
geocrasher大约 4 年前
I use autohotkey to script some of my favorite things, which is another great way to get good use out of the command line on various systems where I don&#x27;t necessarily need&#x2F;want to set up a .bashrc.<p>hotkey: &quot;du-m&quot;<p>Use: Sort directories by usage:<p>du -m --max-depth=1 | sort -n<p>hotkey: &quot;wpinfo&quot;<p>Use: gets basic WordPress site info:<p>code for AHK<p><pre><code> ::wpinfo:: ( echo Home URL $(wp option get home) ; echo Site URL $(wp option get siteurl);echo &quot;### Plugins ###&quot;; wp plugin list;echo &quot;### Themes ###&quot;; wp theme list;echo &quot;### Users ###&quot;; wp user list; echo &quot;### Roles ###&quot;; wp role list; wp core verify-checksums ) </code></pre> hotkey: awk(1-5)<p>use: saves me typing something I fat finger every. single. time.<p>awk &#x27;{ print $1 }&#x27;<p>awk &#x27;{ print $2 }&#x27;<p>awk &#x27;{ print $3 }&#x27; etc<p>AHK:<p><pre><code> ::awk1::awk &#x27;{{} print $1 {}}&#x27; </code></pre> There are others but these are the ones that come to mind. The main thing isn&#x27;t the scripts but the automation. And AHK can save a <i>ton</i> of error-prone typing or copy&#x2F;pasting in these scenarios.
评论 #27028996 未加载
petepete大约 4 年前
A couple of useful ones in here.<p>However, instead of:<p><pre><code> git log --format=&#x27;%aN&#x27; | sort -u </code></pre> I&#x27;d recommend using git shortlog, as it&#x27;ll provide you with counts.<p><pre><code> git shortlog -sn</code></pre>
scrooched_moose大约 4 年前
As far as I&#x27;m aware, rm -f !(text.txt) isn&#x27;t enabled by default, at least in bash. You have to enable extended globbing by running &#x27;shopt -s extglob&#x27; first (or add it to your profile).<p>Great feature though, it adds a ton of extra pattern matching. Not (!) is definitely my most used though.<p><a href="https:&#x2F;&#x2F;www.linuxjournal.com&#x2F;content&#x2F;bash-extended-globbing" rel="nofollow">https:&#x2F;&#x2F;www.linuxjournal.com&#x2F;content&#x2F;bash-extended-globbing</a>
评论 #27025961 未加载
fooblat大约 4 年前
Nice list!<p>&gt; If you want to cat bunch of files at once you can this command<p>You can use cat itself for this (and save typing a few characters):<p><pre><code> cat *</code></pre>
评论 #27024541 未加载
评论 #27024247 未加载
评论 #27024160 未加载
dwhitney大约 4 年前
We should have a &quot;My Favorite One Liners&quot; thread every month like we do &quot;Who&#x27;s Hiring&quot;. Some of this is going straight into some aliases!
评论 #27026214 未加载
评论 #27025525 未加载
susam大约 4 年前
&gt; grep . *<p>I believe this command is present to prefix each line of output with the filename. But this ignores all the blank lines. That can of course be fixed easily with:<p><pre><code> grep ^ * </code></pre> Here is what I normally use myself if I want to show the content of files with their filenames:<p><pre><code> tail -n +1 * </code></pre> This shows the name of the file once at the beginning of each file and no more. Here is an example:<p><pre><code> $ tail -n +1 * ==&gt; bar.txt &lt;== bar 1 bar 2 bar 3 ==&gt; baz.txt &lt;== baz 1 baz 2 baz 3 ==&gt; foo.txt &lt;== foo 1 foo 2 foo 3</code></pre>
评论 #27027900 未加载
jolmg大约 4 年前
&gt; mkfifo hello; script -f hello<p>&gt; This command will allow you to share your terminal session in real time.<p>Using `script` for this seems nice for when you don&#x27;t want them to be able to control the same computer. Like person foo on their computer would<p><pre><code> nc -l 9000 </code></pre> then you could<p><pre><code> script -f &gt;(nc foo 9000) </code></pre> to show them your session. One could also add some encryption to the pipe.<p>If both controlling the same computer is not a problem or is even desired, then it&#x27;s probably simplest to just share a tmux session. One runs `tmux` and the other `tmux a`.
评论 #27027700 未加载
评论 #27031009 未加载
tyingq大约 4 年前
One I used to use when website performance seemed bad. Prints out hits in the current log with a count for each unique client ip:<p><pre><code> awk &#x27;{print $1}&#x27; &lt; apache.log | sort -n | uniq -c | sort -rn |less </code></pre> Produces output like:<p><pre><code> 4482 66.249.73.135 3264 46.105.14.53 3157 130.237.218.86 2073 75.97.9.59 1013 50.16.19.13 ... </code></pre> And fairly easy to throw in a &quot;grep&quot; for specific fetched urls, slices of time, etc.
评论 #27025565 未加载
zfxfr大约 4 年前
-&gt; grep * : &quot;If you want to cat bunch of files at once you can (use) this command.&quot;<p>Yes but I don&#x27;t understand the advantage of that instead of simply using :<p><i>cat *.*<p>or if you want to display the filenames :<p></i>tail *<p>works fine too. Maybe someone can explain.
throwaway823882大约 4 年前
Less-common one-liners that I use for work:<p><pre><code> # Use jq to craft json documents jq -en --rawfile key some_id_rsa \ &#x27;{ &quot;ssh_private_key&quot;: $key, &quot;hosts&quot;: [ { &quot;type&quot;: &quot;EC2&quot;, &quot;name&quot;: &quot;backenddev&quot;, &quot;id&quot;: &quot;i-0123456789&quot;, &quot;address&quot;: &quot;1.2.3.4&quot;, &quot;region&quot;: &quot;us-west-1&quot; } ] }&#x27; &gt; metadata.json # Use jq to import Terraform resources from a state file jq -er &#x27;.modules[].resources | to_entries | .[] | [ &quot;terraform&quot;, &quot;import&quot;, .key, .value.primary.id ] | @sh&#x27; \ generated&#x2F;aws&#x2F;route53&#x2F;terraform.tfstate \ | xargs -L1 env # Use Bash to check for a TCP connection (like &#x27;nc -z&#x27;) timeout 1 bash -c &#x27;cat &lt; &#x2F;dev&#x2F;null &gt; &#x2F;dev&#x2F;tcp&#x2F;127.0.0.1&#x2F;3306&#x27;</code></pre>
评论 #27024772 未加载
jeswin大约 4 年前
Shameless plug for bashojs (bashojs.org), for the JavaScripters:<p>A one liner to update all your npm deps to latest. I use this quite a lot.<p><pre><code> basho --import &#x27;.&#x2F;package.json&#x27; pack -j pack -j &#x27;Object.keys(x.dependencies)&#x27; -m x -e &#x27;npm install ${x}@latest&#x27; </code></pre> Explanation of how this works:<p>1) Import package.json, call it &#x27;pack&#x27;. 2) Put pack in the pipeline. 3) map pack to pack.dependencies. 4) flatMap (-m x) to remove nesting. Because pipeline is an array, and it contains pack.dependencies - another array. 5) Exec command with &#x27;-e&#x27;
defulmere大约 4 年前
My favorite is piping stuff into awk to make bar graphs. Here&#x27;s system load from atopsar for the current day:<p><pre><code> atopsar -p | tail -n +7 | grep &#x27;\S&#x27; | awk &#x27;{printf(&quot;\n%s %6.2f &quot;,$1, $5); for (i = 0; i&lt;$5; i++) {printf(&quot;&quot;)}}&#x27;; echo </code></pre> sample: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;defulmere&#x2F;bec1aef40ca4cddb5c421ffa5f9b9e1f&#x2F;edit" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;defulmere&#x2F;bec1aef40ca4cddb5c421ffa5f...</a>
评论 #27026028 未加载
评论 #27028797 未加载
kmstout大约 4 年前
Among my favorites is this little zinger (awk):<p><pre><code> !seen[$0]++ </code></pre> which gives the union of the lines of all the input files, with none of that &#x27;sort | uniq&#x27; business.<p>It&#x27;s not a one-liner, but here&#x27;s intersection:<p><pre><code> ! buf[$0]++ { acc[$0]++ } ENDFILE { delete buf; files++ } END { for (k in acc) if (acc[k] == files) print k } </code></pre> and here&#x27;s set difference:<p><pre><code> ! filenum { acc[$0] = 1 } filenum { delete acc[$0] } ENDFILE { filenum++ } END { for (k in acc) print k } </code></pre> I keep these in my ~&#x2F;bin with evocative names (e.g., &#x27;union&#x27; and &#x27;set-diff&#x27;). Once I did that, lots of other things became one liners.
banana_giraffe大约 4 年前
The comment about sharing a terminal using mkfifo reminds me:<p>Screen has a multi-display mode you can use with &quot;screen -x&quot; that lets multiple clients connect to the same session. Useful if you want to walk someone through a process.
boojing大约 4 年前
I thought these would be jokes!
评论 #27024719 未加载
评论 #27024491 未加载
Crazyontap大约 4 年前
I sometimes wish there was a linux command that when piped into turned anything into json readable format, so `ps | json-pipe` would output something like [{pid: 133, name, etc}, ...] or `ls | json-pipe` would turn into [{name, size, etc}].<p>It would handle most known commands and maybe had plugin support for the rest.<p>Just a thought I had. Don&#x27;t really know if it&#x27;s possible or feasible. But it would make working with command line much more predictable and easier especially if it also supported jmespath! What do you guys think?
评论 #27024991 未加载
评论 #27034535 未加载
评论 #27028106 未加载
评论 #27027005 未加载
revscat大约 4 年前
Rails dev. This runs the most recently modified test in your spec&#x2F; directory:<p><pre><code> bin&#x2F;rspec $(find spec -type f -exec stat -f &#x27;%a %N&#x27; {} \; | sort -r | head -1 | awk &#x27;{print $NF}&#x27;) </code></pre> So if you are working on a spec &amp; save it, running this will execute that spec &amp; only that spec. I have it hooked up to a mapping in neovim, so hitting `&lt;leader&gt;rt` runs the test.<p>Needs some tweaking, and I&#x27;m aware that guard exists. This is simpler. Quick &amp; does the job.
criticas大约 4 年前
Using &quot;wc -l&quot; or &quot;sort | uniq -c | sort&quot; to count and classify things.<p>Count processes and threads by user on Linux:<p># ps -Teo euser=,comm= | sort | uniq -c | sort -nr
评论 #27025424 未加载
gbrown_大约 4 年前
Sending typescript to a FIFO seems odd. Sure you can see live input rather than following a line at a time with tail -f but you also lose persistence. To share something live I&#x27;d just use tmux or screen, granted if you want to ensure the observer has read only access there may be a couple more steps than using a FIFO.
kown7大约 4 年前
One can always dig their own IP address<p><pre><code> dig TXT -4 +short o-o.myaddr.l.google.com @ns1.google.com</code></pre>
评论 #27030443 未加载
loulouxiv大约 4 年前
import png:-|xclip -selection clipboard -t image&#x2F;png -i<p>Copy a selected rectangle of the screen into clipboard
评论 #27025433 未加载
评论 #27028512 未加载
z5h大约 4 年前
Speaking of one liners... is there a way to get w3m or lynx to print a PRETTY formatted output to the console and exit? Seems all the dump commands are for plain text. Hoping someone here knows as this arcane knowledge doesn’t seem to exist elsewhere.
评论 #27026066 未加载
rahimiali大约 4 年前
Some of these are useful. Some of these have better alternatives:<p>&gt; grep . *<p>you can &quot;cat *&quot;<p>&gt; ps aux | convert label:@- process.png<p>copy-pastable text is a better way to exchange text.<p>&gt; mkfifo hello; script -f hello<p>you forget to mention that the other side needs to cat this file. but also, tmux might be better for this.
评论 #27029752 未加载
CalChris大约 4 年前
fvi - find vi - recursively grep for a pattern and pass the successful files into the editor. I use this to find examples in a codebase. Sure, it would be nice to set editor&#x27;s pattern and have it jump to the first occurrence but that has been a bridge too far. Maybe I&#x27;ll try again with VSC. (Yeah, VSC doesn&#x27;t seem to have a way of setting the search pattern.)<p><pre><code> function fvi { grep -rl $1 . | xargs zsh -c &#x27;code &quot;$@&quot; &lt;$0&#x27; &#x2F;dev&#x2F;tty }</code></pre>
sandreas大约 4 年前
Maybe some of these could be shared at <a href="http:&#x2F;&#x2F;www.bashoneliners.com" rel="nofollow">http:&#x2F;&#x2F;www.bashoneliners.com</a> :-)
FriedrichN大约 4 年前
I use this as a one liner script in my PATH to review code before I commit it.<p><pre><code> svn diff &quot;$@&quot; | colordiff | less -R -x4</code></pre>
评论 #27024547 未加载
tornato7大约 4 年前
I thought this was going to be about jokes :(
chromejs10大约 4 年前
Was really hoping for a list of jokes when I clicked that link. A bit disappointed.
评论 #27028872 未加载
dvh大约 4 年前
I pulled history and my favorite seems to be: pmount sdb1
kaladin_1大约 4 年前
ffmpeg -i &lt;sth.mp4&gt; &lt;sth.mp3&gt; ## converting videos to mp3
评论 #27027508 未加载
secondcoming大约 4 年前
git checkout -f develop