The ones I get a lot of use out of are:<p>curly brace substitution:<p><pre><code> $ mkdir -p new_project/{img,js,css}
mkdir -p new_project/img new_project/js new_project/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 "We're all mad here. I'm mad. You're mad."
we're all mad here. I'm mad. You're mad.
$ !!:gs/mad/HN/
we're all HN here. I'm HN. You're HN.
</code></pre>
I have a (WIP) ebook with more such tricks on GitHub if anyone is interested:
<a href="https://tenebrousedge.github.io/shell_guide/" rel="nofollow">https://tenebrousedge.github.io/shell_guide/</a>
Disappointingly, this list treats "yes" like a toy that just prints things over and over, and doesn't mention actual useful uses for "yes", like accepting all defaults without having to press enter.<p>Practical example: when you are doing "make oldconfig" on the kernel, and you don't care about all those questions:<p>yes "" | make oldconfig<p>Or, if you prefer answering no instead:<p>yes "n" | yourcommand<p>Also, the author refers to watch as a "supervisor" ("supervise command" - his words). That is bad terminology. Process supervision has well defined meaning in this context, and watch isn'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).
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'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'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've found useful that might be worth adding:<p>* You don't need to specify the source directory in GNU find (you do on FreeBSD et al) so if you're lazy then `find` will default to your working directory:<p><pre><code> find -type f -name "*.txt"
</code></pre>
* You can do case insensitive named matches with `-iname` (this is also GNU specific):<p><pre><code> find -type f -iname "invoice*"</code></pre>
<p><pre><code> grep -P "\t"
</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 "[ctrl-v]TAB"</code></pre>
> 6. List contents of tar.gz and extract only one file<p>> tar -ztvf file.tgz<p>> tar -zxvf file.tgz filename<p>Tar no longer requires the modifier for the compression when extracting an archive. So no matter if it's a .tar.gz, .tar.Z, .tar.bz2, etc you can just use "tar xvf"
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 :)
MMV is the latest interesting one I've found recently: <a href="https://ss64.com/bash/mmv.html" rel="nofollow">https://ss64.com/bash/mmv.html</a><p>For example, I can do the following:<p><pre><code> mmv "flight.*" "flight-new.#1"
</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've got a bunch of different files with the same name but with different extensions such as html, txt and sms.
I probably should be embarrassed to admit I didn't know about much of the stuff on this page about parameter expansion:<p><a href="http://wiki.bash-hackers.org/syntax/pe" rel="nofollow">http://wiki.bash-hackers.org/syntax/pe</a><p>Bash is insane.
I guess I'm just overly sensitive (and maybe English is not the poster's first language), but I cringe at "the Linux shell"... I have used ksh, bash, (a little) csh, and zsh over the years, and love the architecture that makes "the shell" just another non-special binary that's not unique to the OS.<p>And yes, a lot of the things mentioned work in a lot of shells, but some don't, or act differently.
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 '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) *2^(8-i));if(k)printf("%0o ",k);print}'
</code></pre>
Description: Output primary terminal colors<p><pre><code> Command: for i in {0..16}; do echo -e "\e[38;05;${i}m\\\e[38;05;${i}m"; done | column -c 80 -s ' '; echo -e "\e[m"
</code></pre>
Description: NPM list with top-level only<p><pre><code> Command: npm list --depth=0 2>/dev/null
</code></pre>
Description: Show active connections<p><pre><code> Command: netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | sed -e 's/::ffff://' |cut -d: -f1 | sort | uniq -c | sort -rn | head</code></pre>
Nice! Point 23 is a bit misleading though: comm only works on <i>sorted</i> input files. Also, "disown -h" in Point 21 works on bash but not on zsh. Also, in Point 22, "timeout" only works if the command does not trap SIGTERM (or you have to use -k).
> 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.
nice collection! upvoted for "15. `namei -l`" alone, which is far too little known.<p>a better "20. Randomize lines in file":<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.)
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.
I really like the "ag" 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.
You'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.
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're missing out on anything. I went <i>years</i> without knowing about ctrl+R (search command history).<p>[0] <a href="http://man7.org/linux/man-pages/man3/readline.3.html#EDITING_COMMANDS" rel="nofollow">http://man7.org/linux/man-pages/man3/readline.3.html#EDITING...</a>
Not really a single command, it'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://jugad2.blogspot.in/2008/09/unix-one-liner-to-kill-hanging-firefox.html" rel="nofollow">https://jugad2.blogspot.in/2008/09/unix-one-liner-to-kill-ha...</a>
Throw this in your ~/bin as a script named math:<p><pre><code> #!/bin/sh
scale=4 # results will print to the 4th decimal
echo "scale=$scale; $@" | bc -l
</code></pre>
Now you can do math.<p><pre><code> $ math '1+1'
2
$ math '2/3'
.6666
</code></pre>
This is especially useful in shell scripts with interpolated variables:<p><pre><code> x=10
x=`math $x - 1`</code></pre>
Related Resource: <a href="http://www.commandlinefu.com/commands/browse" rel="nofollow">http://www.commandlinefu.com/commands/browse</a>