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.

Interesting commands for the Linux shell

372 pointsby victorlfover 7 years ago

26 comments

sethrinover 7 years ago
The ones I get a lot of use out of are:<p>curly brace substitution:<p><pre><code> $ mkdir -p new_project&#x2F;{img,js,css} mkdir -p new_project&#x2F;img new_project&#x2F;js new_project&#x2F;css $ mv some_file.txt{,.old} mv some_file.txt some_file.txt.old </code></pre> Caret substitution:<p><pre><code> # systemctl status mysql.service -- snip output -- # ^status^restart systemctl restart mysql.service </code></pre> Global substitution (and history shortcut):<p><pre><code> $ echo &quot;We&#x27;re all mad here. I&#x27;m mad. You&#x27;re mad.&quot; we&#x27;re all mad here. I&#x27;m mad. You&#x27;re mad. $ !!:gs&#x2F;mad&#x2F;HN&#x2F; we&#x27;re all HN here. I&#x27;m HN. You&#x27;re HN. </code></pre> I have a (WIP) ebook with more such tricks on GitHub if anyone is interested: <a href="https:&#x2F;&#x2F;tenebrousedge.github.io&#x2F;shell_guide&#x2F;" rel="nofollow">https:&#x2F;&#x2F;tenebrousedge.github.io&#x2F;shell_guide&#x2F;</a>
评论 #15248775 未加载
评论 #15250620 未加载
disconnectedover 7 years ago
Disappointingly, this list treats &quot;yes&quot; like a toy that just prints things over and over, and doesn&#x27;t mention actual useful uses for &quot;yes&quot;, like accepting all defaults without having to press enter.<p>Practical example: when you are doing &quot;make oldconfig&quot; on the kernel, and you don&#x27;t care about all those questions:<p>yes &quot;&quot; | make oldconfig<p>Or, if you prefer answering no instead:<p>yes &quot;n&quot; | yourcommand<p>Also, the author refers to watch as a &quot;supervisor&quot; (&quot;supervise command&quot; - his words). That is bad terminology. Process supervision has well defined meaning in this context, and watch isn&#x27;t even close to doing it.<p>Examples of actual supervisors are supervisord, runit, monit, s6, and of course systemd (which also does service management and, er, too much other stuff, honestly).
评论 #15250159 未加载
评论 #15249010 未加载
评论 #15248235 未加载
hnlmorgover 7 years ago
Some handy tips there but I would recommend changing some of the `find` examples:<p><pre><code> find . -exec echo {} \; # One file by line </code></pre> You don&#x27;t need to execute echo to do that as find will output by default anyway. There is also a `-print` flag if you wish to force `find` to output.<p><pre><code> find . -exec echo {} \+ # All in the same line </code></pre> This is think is a dangerous example because any files with spaces will look like two files instead of one delimited by space.<p>Lastly, in both the above examples you&#x27;re returning files <i>and directories</i> rather than just files. If you wanted to exclude directories then use the `-type f` flag to specify files:<p><pre><code> find . -type f ... </code></pre> (equally you could specify only directories with `-type d`)<p>Other `find` tips I&#x27;ve found useful that might be worth adding:<p>* You don&#x27;t need to specify the source directory in GNU find (you do on FreeBSD et al) so if you&#x27;re lazy then `find` will default to your working directory:<p><pre><code> find -type f -name &quot;*.txt&quot; </code></pre> * You can do case insensitive named matches with `-iname` (this is also GNU specific):<p><pre><code> find -type f -iname &quot;invoice*&quot;</code></pre>
评论 #15247692 未加载
评论 #15251312 未加载
评论 #15250316 未加载
joostersover 7 years ago
<p><pre><code> grep -P &quot;\t&quot; </code></pre> Not criticising the author here, as grep -P is good, but you might not also know that you can enter tabs and other codes in bash by pressing ctrl-v. So you could also type:<p><pre><code> grep &quot;[ctrl-v]TAB&quot;</code></pre>
评论 #15246893 未加载
评论 #15246870 未加载
评论 #15249145 未加载
评论 #15246865 未加载
stiangrindvollover 7 years ago
Doesn&#x27;t matter how long you&#x27;ve been using a GNU&#x2F;Linux shell, you&#x27;ll always learn something new every day. Thanks for this.
评论 #15246508 未加载
评论 #15247135 未加载
评论 #15250037 未加载
Tepixover 7 years ago
&gt; 6. List contents of tar.gz and extract only one file<p>&gt; tar -ztvf file.tgz<p>&gt; tar -zxvf file.tgz filename<p>Tar no longer requires the modifier for the compression when extracting an archive. So no matter if it&#x27;s a .tar.gz, .tar.Z, .tar.bz2, etc you can just use &quot;tar xvf&quot;
评论 #15246875 未加载
评论 #15266324 未加载
abhiragover 7 years ago
As long as we are on the topic of Linux shell commands I would like to share a tip which has helped me, whenever you are using wildcards with rm you can first test them with ls and see the files that will be deleted and then replace the ls with rm. This along with -i and -I flags makes rm just a tad bit less scary for me. Kinda basic but hopefully somebody finds it helpful :)
评论 #15249370 未加载
评论 #15248252 未加载
评论 #15248018 未加载
评论 #15248463 未加载
aembletonover 7 years ago
MMV is the latest interesting one I&#x27;ve found recently: <a href="https:&#x2F;&#x2F;ss64.com&#x2F;bash&#x2F;mmv.html" rel="nofollow">https:&#x2F;&#x2F;ss64.com&#x2F;bash&#x2F;mmv.html</a><p>For example, I can do the following:<p><pre><code> mmv &quot;flight.*&quot; &quot;flight-new.#1&quot; </code></pre> and this will rename all of my files that start with flight. to flight-new. preserving the file extension. So useful, when you&#x27;ve got a bunch of different files with the same name but with different extensions such as html, txt and sms.
评论 #15248772 未加载
评论 #15246709 未加载
leephillipsover 7 years ago
I probably should be embarrassed to admit I didn&#x27;t know about much of the stuff on this page about parameter expansion:<p><a href="http:&#x2F;&#x2F;wiki.bash-hackers.org&#x2F;syntax&#x2F;pe" rel="nofollow">http:&#x2F;&#x2F;wiki.bash-hackers.org&#x2F;syntax&#x2F;pe</a><p>Bash is insane.
评论 #15249390 未加载
michaelcampbellover 7 years ago
I guess I&#x27;m just overly sensitive (and maybe English is not the poster&#x27;s first language), but I cringe at &quot;the Linux shell&quot;... I have used ksh, bash, (a little) csh, and zsh over the years, and love the architecture that makes &quot;the shell&quot; just another non-special binary that&#x27;s not unique to the OS.<p>And yes, a lot of the things mentioned work in a lot of shells, but some don&#x27;t, or act differently.
评论 #15251151 未加载
Exumaover 7 years ago
Here are some that I use in Pet:<p>Description: Remove executable recursively for all files<p><pre><code> Command: chmod -x $(find . -type f) </code></pre> Description: List files with permissions number<p><pre><code> Command: ls -l | awk &#x27;{k=0;for(i=0;i&lt;=8;i++)k+=((substr($1,i+2,1)~&#x2F;[rwx]&#x2F;) *2^(8-i));if(k)printf(&quot;%0o &quot;,k);print}&#x27; </code></pre> Description: Output primary terminal colors<p><pre><code> Command: for i in {0..16}; do echo -e &quot;\e[38;05;${i}m\\\e[38;05;${i}m&quot;; done | column -c 80 -s &#x27; &#x27;; echo -e &quot;\e[m&quot; </code></pre> Description: NPM list with top-level only<p><pre><code> Command: npm list --depth=0 2&gt;&#x2F;dev&#x2F;null </code></pre> Description: Show active connections<p><pre><code> Command: netstat -tn 2&gt;&#x2F;dev&#x2F;null | grep :80 | awk &#x27;{print $5}&#x27; | sed -e &#x27;s&#x2F;::ffff:&#x2F;&#x2F;&#x27; |cut -d: -f1 | sort | uniq -c | sort -rn | head</code></pre>
评论 #15249097 未加载
a3_nmover 7 years ago
Nice! Point 23 is a bit misleading though: comm only works on <i>sorted</i> input files. Also, &quot;disown -h&quot; in Point 21 works on bash but not on zsh. Also, in Point 22, &quot;timeout&quot; only works if the command does not trap SIGTERM (or you have to use -k).
pteroover 7 years ago
&gt; If a program eats too much memory, the swap can get filled with the rest of the memory and when you go back to normal, everything is slow. Just restart the swap partition to fix it: sudo swapoff -a; sudo swapon -a<p>Is this true? Not impossible, but I am surprised. If true, what is this fixing? In my naive view (never studied swap management), if at the current time a page is swapped out (and by now we have more memory -- we can kill swap completely and do fine), it should get swapped in when needed next time. As there is more memory now it should not, in general, be swapped out again.<p>If true we are exchanging a number of short delays later for a longer delay now, which to me hardly looks like a win.
评论 #15247226 未加载
评论 #15247133 未加载
评论 #15248241 未加载
xearlover 7 years ago
nice collection! upvoted for &quot;15. `namei -l`&quot; alone, which is far too little known.<p>a better &quot;20. Randomize lines in file&quot;:<p><pre><code> shuf file.txt </code></pre> instead of<p><pre><code> cat file.txt | sort -R </code></pre> (sort -R sorts by hash, which is not really randomisation.)
评论 #15249777 未加载
评论 #15246580 未加载
评论 #15246512 未加载
评论 #15246422 未加载
kleffover 7 years ago
Never knew about <i>readlink</i> for files, but I use<p><pre><code> pwd -P </code></pre> all the time to get the real full path (no symlinks) of the current directory. Really easy to remember as well, <i>P</i>rint <i>W</i>orking <i>D</i>irectory.
评论 #15249227 未加载
yodsanklaiover 7 years ago
I really like the &quot;ag&quot; command. Very convenient to grep in a bunch of files filtered by type. Example:<p><pre><code> ag --ocaml to_string </code></pre> Very fast and simple syntax.
评论 #15246894 未加载
hathymover 7 years ago
cached version: <a href="https:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:JQ7qBa09bekJ:https:&#x2F;&#x2F;www.lopezferrando.com&#x2F;30-interesting-shell-commands&#x2F;+&amp;cd=1&amp;hl=en&amp;ct=clnk&amp;gl=fr" rel="nofollow">https:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:JQ7qBa...</a>
yellowappleover 7 years ago
You&#x27;ll definitely want to check `free -m` before calling `swapoff` to make sure you really have enough memory for everything in swap, lest you want to invoke the wrath of the OOM killer.
Pete_Dover 7 years ago
Not a command as such, but I recommend skimming the readline man page[0] and trying some of the bindings out to see if you&#x27;re missing out on anything. I went <i>years</i> without knowing about ctrl+R (search command history).<p>[0] <a href="http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;readline.3.html#EDITING_COMMANDS" rel="nofollow">http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man3&#x2F;readline.3.html#EDITING...</a>
vram22over 7 years ago
Not really a single command, it&#x27;s a one-liner - a pipeline, but might be interesting, not only for the one-liner but for the comments about Unix processes that ensued on my blog:<p>UNIX one-liner to kill a hanging Firefox process:<p><a href="https:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2008&#x2F;09&#x2F;unix-one-liner-to-kill-hanging-firefox.html" rel="nofollow">https:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2008&#x2F;09&#x2F;unix-one-liner-to-kill-ha...</a>
sillysaurus3over 7 years ago
Throw this in your ~&#x2F;bin as a script named math:<p><pre><code> #!&#x2F;bin&#x2F;sh scale=4 # results will print to the 4th decimal echo &quot;scale=$scale; $@&quot; | bc -l </code></pre> Now you can do math.<p><pre><code> $ math &#x27;1+1&#x27; 2 $ math &#x27;2&#x2F;3&#x27; .6666 </code></pre> This is especially useful in shell scripts with interpolated variables:<p><pre><code> x=10 x=`math $x - 1`</code></pre>
评论 #15249197 未加载
评论 #15249474 未加载
ghotliover 7 years ago
ctrl-r for fuzzy searching your history is all you really need to know.
husamiaover 7 years ago
Related Resource: <a href="http:&#x2F;&#x2F;www.commandlinefu.com&#x2F;commands&#x2F;browse" rel="nofollow">http:&#x2F;&#x2F;www.commandlinefu.com&#x2F;commands&#x2F;browse</a>
cutlerover 7 years ago
CentOS 7 specifies `man rename` : rename [options] expression replacement file...<p>The rename example fails with &quot;rename: not enough arguments&quot;.
评论 #15246765 未加载
评论 #15247271 未加载
评论 #15255213 未加载
sirwittiover 7 years ago
Thanks, every once in a while these lists are useful!
jherikoover 7 years ago
.... or just use a modern os where all this stuff is intuitive and easy :P