<p>We all know how to use <code>&lt;ctrl&gt;-R</code> to reverse search through history, but did you know you can use <code>&lt;ctrl&gt;-S</code> to forward search if you set <code>stty stop ""</code>?  Also, have you ever tried running bind -p to see all of your keyboard shortcuts listed?  There are over 455 on Mac OS X by default.  </p>

<p>What is your single most favorite obscure trick, keyboard shortcut or shopt configuration using bash?</p>

[{'answer_id': 68386, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>Well, this may be a bit off topic, but if you are an Emacs user, I would say "emacs" is the most powerful trick...  before you downvote this, try out "M-x shell" within an emacs instance... you get a shell inside emacs, and have all the power of emacs along with the power of a shell (there are some limitations, such as opening another emacs within it, but in most cases it is a lot more powerful than a vanilla bash prompt).</p>\n'}, {'answer_id': 68388, 'author': 'HFLW', 'author_id': 252822, 'author_profile': 'https://Stackoverflow.com/users/252822', 'pm_score': 4, 'selected': False, 'text': '<p>You can use the watch command in conjunction with another command to look for changes. An example of this was when I was testing my router, and I wanted to get up-to-date numbers on stuff like signal-to-noise ratio, etc.</p>\n\n<pre><code>watch --interval=10 lynx -dump http://dslrouter/stats.html\n</code></pre>\n'}, {'answer_id': 68390, 'author': 'ctcherry', 'author_id': 10322, 'author_profile': 'https://Stackoverflow.com/users/10322', 'pm_score': 5, 'selected': False, 'text': '<p>More of a novelty, but it\'s clever...</p>\n\n<p>Top 10 commands used:</p>\n\n<pre><code>$ history | awk \'{print $2}\' | awk \'BEGIN {FS="|"}{print $1}\' | sort | uniq -c | sort -nr | head\n</code></pre>\n\n<p>Sample output:</p>\n\n<pre><code> 242 git\n  83 rake\n  43 cd\n  33 ss\n  24 ls\n  15 rsg\n  11 cap\n  10 dig\n   9 ping\n   3 vi\n</code></pre>\n'}, {'answer_id': 68397, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 7, 'selected': False, 'text': "<pre><code>cd -\n</code></pre>\n\n<p>It's the command-line equivalent of the back button (takes you to the previous directory you were in).</p>\n"}, {'answer_id': 68400, 'author': 'Tim Williscroft', 'author_id': 2789, 'author_profile': 'https://Stackoverflow.com/users/2789', 'pm_score': 1, 'selected': False, 'text': "<p>bash can redirect to and from TCP/IP sockets.\n/dev/tcp/ and /dev/udp.</p>\n\n<p>Some people think it's a security issue,\nbut that's what OS level security like Solaris X's jail is for.</p>\n\n<p>As Will Robertson notes, change prompt to do stuff... print the command # for !nn \nSet the Xterm terminal name. If it's an old Xterm that doesn't sniff traffic to set it's title.</p>\n"}, {'answer_id': 68411, 'author': 'Will Robertson', 'author_id': 4161, 'author_profile': 'https://Stackoverflow.com/users/4161', 'pm_score': 2, 'selected': False, 'text': '<p>I like a splash of colour in my prompts:</p>\n\n<pre><code>export PS1="\\[\\033[07;31m\\] \\h \\[\\033[47;30m\\] \\W \\[\\033[00;31m\\] \\$ \\[\\e[m\\]"\n</code></pre>\n\n<p>I\'m afraid I don\'t have a screenshot for what that looks like, but it\'s supposed to be something like (all on one line):</p>\n\n<pre><code>[RED BACK WHITE TEXT] Computer name \n[BLACK BACK WHITE TEXT] Working Directory \n[WHITE BACK RED TEXT] $\n</code></pre>\n\n<p>Customise as per what you like to see <code>:)</code></p>\n'}, {'answer_id': 68412, 'author': 'dreamlax', 'author_id': 10320, 'author_profile': 'https://Stackoverflow.com/users/10320', 'pm_score': 3, 'selected': False, 'text': "<p>When downloading a large file I quite often do:</p>\n\n<pre><code>while ls -la &lt;filename&gt;; do sleep 5; done\n</code></pre>\n\n<p>And then just ctrl+c when I'm done (or if <code>ls</code> returns non-zero). It's similar to the <code>watch</code> program but it uses the shell instead, so it works on platforms without <code>watch</code>.</p>\n\n<p>Another useful tool is netcat, or <code>nc</code>. If you do:</p>\n\n<pre><code>nc -l -p 9100 &gt; printjob.prn\n</code></pre>\n\n<p>Then you can set up a printer on another computer but instead use the IP address of the computer running netcat. When the print job is sent, it is received by the computer running netcat and dumped into <code>printjob.prn</code>.</p>\n"}, {'answer_id': 68413, 'author': 'Sergio Morales', 'author_id': 9506, 'author_profile': 'https://Stackoverflow.com/users/9506', 'pm_score': 0, 'selected': False, 'text': '<p>Some useful mencoder commands I found out about when looking for some audio and video editing tools:</p>\n\n<p>from .xxx to .avi</p>\n\n<pre><code>mencoder movie.wmv -o movie.avi -ovc lavc -oac lavc \n</code></pre>\n\n<p>Dump sound from a video:</p>\n\n<pre><code>mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile fileout.mp3 filein.avi \n</code></pre>\n'}, {'answer_id': 68419, 'author': 'amrox', 'author_id': 4468, 'author_profile': 'https://Stackoverflow.com/users/4468', 'pm_score': 5, 'selected': False, 'text': '<p><kbd>ESC</kbd><kbd>.</kbd></p>\n\n<p>Inserts the last arguments from your last bash command.  It comes in handy more than you think.</p>\n\n<pre><code>cp file /to/some/long/path\n</code></pre>\n\n<p>cd <kbd>ESC</kbd><kbd>.</kbd></p>\n'}, {'answer_id': 68421, 'author': 'hoyhoy', 'author_id': 3499, 'author_profile': 'https://Stackoverflow.com/users/3499', 'pm_score': 7, 'selected': True, 'text': '<p>When running commands, sometimes I\'ll want to run a command with the previous ones arguments.  To do that, you can use this shortcut:</p>\n\n<pre><code>$ mkdir /tmp/new\n$ cd !!:*\n</code></pre>\n\n<p>Occasionally, in lieu of using find, I\'ll break-out a one-line loop if I need to run a bunch of commands on a list of files.  </p>\n\n<pre><code>for file in *.wav; do lame "$file" "$(basename "$file" .wav).mp3" ; done;\n</code></pre>\n\n<p>Configuring the command-line history options in my .bash_login (or .bashrc) is really useful.  The following is a cadre of settings that I use on my Macbook Pro. </p>\n\n<p>Setting the following makes bash erase duplicate commands in your history:</p>\n\n<pre><code>export HISTCONTROL="erasedups:ignoreboth"\n</code></pre>\n\n<p>I also jack my history size up pretty high too.  Why not?  It doesn\'t seem to slow anything down on today\'s microprocessors.</p>\n\n<pre><code>export HISTFILESIZE=500000\nexport HISTSIZE=100000\n</code></pre>\n\n<p>Another thing that I do is ignore some commands from my history.  No need to remember the exit command. </p>\n\n<pre><code>export HISTIGNORE="&amp;:[ ]*:exit"\n</code></pre>\n\n<p>You definitely want to set histappend.  Otherwise, bash overwrites your history when you exit.</p>\n\n<pre><code>shopt -s histappend\n</code></pre>\n\n<p>Another option that I use is cmdhist.  This lets you save multi-line commands to the history as one command.</p>\n\n<pre><code>shopt -s cmdhist\n</code></pre>\n\n<p>Finally, on Mac OS X (if you\'re not using vi mode), you\'ll want to reset &lt;CTRL&gt;-S from being scroll stop.  This prevents bash from being able to interpret it as forward search. </p>\n\n<pre><code>stty stop ""\n</code></pre>\n'}, {'answer_id': 68424, 'author': 'Jiaaro', 'author_id': 2908, 'author_profile': 'https://Stackoverflow.com/users/2908', 'pm_score': 6, 'selected': False, 'text': "<p>rename</p>\n\n<p>Example:</p>\n\n<pre><code>$ ls\nthis_has_text_to_find_1.txt\nthis_has_text_to_find_2.txt\nthis_has_text_to_find_3.txt\nthis_has_text_to_find_4.txt\n\n$ rename 's/text_to_find/been_renamed/' *.txt\n$ ls\nthis_has_been_renamed_1.txt\nthis_has_been_renamed_2.txt\nthis_has_been_renamed_3.txt\nthis_has_been_renamed_4.txt\n\n</code></pre>\n\n<p>So useful</p>\n"}, {'answer_id': 68429, 'author': 'amrox', 'author_id': 4468, 'author_profile': 'https://Stackoverflow.com/users/4468', 'pm_score': 7, 'selected': False, 'text': '<p>Another favorite:</p>\n\n<pre><code>!!\n</code></pre>\n\n<p>Repeats your last command.  Most useful in the form:</p>\n\n<pre><code>sudo !!\n</code></pre>\n'}, {'answer_id': 68441, 'author': 'Jon Ericson', 'author_id': 1438, 'author_profile': 'https://Stackoverflow.com/users/1438', 'pm_score': 4, 'selected': False, 'text': "<p>I like to construct commands with <strong>echo</strong> and pipe them to the shell:</p>\n\n<pre><code>$ find dir -name \\*~ | xargs echo rm\n...\n$ find dir -name \\*~ | xargs echo rm | ksh -s\n</code></pre>\n\n<p>Why?  Because it allows me to look at what's going to be done before I do it.  That way if I have a horrible error (like removing my home directory), I can catch it before it happens.  Obviously, this is most important for destructive or irrevocable actions.</p>\n"}, {'answer_id': 68449, 'author': 'porges', 'author_id': 10311, 'author_profile': 'https://Stackoverflow.com/users/10311', 'pm_score': 4, 'selected': False, 'text': '<p>Here\'s a couple of configuration tweaks:</p>\n\n<p><code>~/.inputrc</code>:</p>\n\n<pre><code>"\\C-[[A": history-search-backward\n"\\C-[[B": history-search-forward\n</code></pre>\n\n<p>This works the same as <code>^R</code> but using the arrow keys instead. This means I can type (e.g.) <code>cd /media/</code> then hit up-arrow to go to the last thing I <code>cd</code>\'d to inside the <code>/media/</code> folder.</p>\n\n<p>(I use Gnome Terminal, you may need to change the escape codes for other terminal emulators.)</p>\n\n<p>Bash completion is also incredibly useful, but it\'s a far more subtle addition. In <code>~/.bashrc</code>:</p>\n\n<pre><code>if [ -f /etc/bash_completion ]; then\n    . /etc/bash_completion\nfi\n</code></pre>\n\n<p>This will enable per-program tab-completion (e.g. attempting tab completion when the command line starts with <code>evince</code> will only show files that evince can open, and it will also tab-complete command line options).</p>\n\n<p>Works nicely with this also in <code>~/.inputrc</code>:</p>\n\n<pre><code>set completion-ignore-case on\nset show-all-if-ambiguous on\nset show-all-if-unmodified on\n</code></pre>\n'}, {'answer_id': 68489, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>As an extension to CTRL-r to search backwards, you can auto-complete your current input with your history if you bind \'history-search-backward\'.  I typically bind it to the same key that it is in tcsh:  ESC-p.  You can do this by putting the following line in your .inputrc file:</p>\n\n<p><code>"\\M-p": history-search-backward</code></p>\n\n<p>E.g.  if you have previously executed \'make some_really_painfully_long_target\' you can type:</p>\n\n<p><code>&gt; make &lt;ESC p&gt;</code></p>\n\n<p>and it will give you</p>\n\n<p><code>&gt; make some_really_painfully_long_target</code></p>\n'}, {'answer_id': 68496, 'author': 'Alex M', 'author_id': 9652, 'author_profile': 'https://Stackoverflow.com/users/9652', 'pm_score': 6, 'selected': False, 'text': '<p>I\'m a fan of the <code>!$</code>, <code>!^</code> and <code>!*</code> expandos, returning, from the most recent submitted command line: the last item, first non-command item, and all non-command items. To wit (Note that the shell prints out the command first):</p>\n\n<pre><code>$ echo foo bar baz\nfoo bar baz\n$ echo bang-dollar: !$ bang-hat: !^ bang-star: !*\necho bang-dollar: baz bang-hat: foo bang-star: foo bar baz\nbang-dollar: baz bang-hat: foo bang-star: foo bar baz\n</code></pre>\n\n<p>This comes in handy when you, say <code>ls filea fileb</code>, and want to edit one of them: <code>vi !$</code> or both of them: <code>vimdiff !*</code>. It can also be generalized to "the <code>n</code>th argument" like so:</p>\n\n<pre><code>$ echo foo bar baz\n$ echo !:2\necho bar\nbar\n</code></pre>\n\n<p>Finally, with pathnames, you can get at parts of the path by appending <code>:h</code> and <code>:t</code> to any of the above expandos:</p>\n\n<pre><code>$ ls /usr/bin/id\n/usr/bin/id\n$ echo Head: !$:h  Tail: !$:t\necho Head: /usr/bin Tail: id\nHead: /usr/bin Tail: id\n</code></pre>\n'}, {'answer_id': 68547, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$ touch {1,2}.txt\n$ ls [12].txt\n1.txt  2.txt\n$ rm !:1\nrm [12].txt\n$ history | tail -10\n...\n10007  touch {1,2}.txt\n...\n$ !10007\ntouch {1,2}.txt\n$ for f in *.txt; do mv $f ${f/txt/doc}; done\n</code></pre>\n'}, {'answer_id': 68551, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 1, 'selected': False, 'text': '<p>And this one is key for me actually:</p>\n\n<p>set -o vi</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 68576, 'author': 'user10741', 'author_id': 10741, 'author_profile': 'https://Stackoverflow.com/users/10741', 'pm_score': 5, 'selected': False, 'text': "<p>^R reverse search. Hit ^R, type a fragment of a previous command you want to match, and hit ^R until you find the one you want. Then I don't have to remember recently used commands that are still in my history. Not exclusively bash, but also: ^E for end of line, ^A for beginning of line, ^U and ^K to delete before and after the cursor, respectively.</p>\n"}, {'answer_id': 68590, 'author': 'baudtack', 'author_id': 10738, 'author_profile': 'https://Stackoverflow.com/users/10738', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve always liked this one.\nAdd this to your /etc/inputrc or ~/.inputrc</p>\n\n<p>"\\e[A":history-search-backward\n"\\e[B":history-search-forward</p>\n\n<p>When you type <code>ls &lt;up-arrow&gt;</code> it will be replaced with the last command starting with "ls " or whatever else you put in.</p>\n'}, {'answer_id': 68600, 'author': 'user10765', 'author_id': 10765, 'author_profile': 'https://Stackoverflow.com/users/10765', 'pm_score': 7, 'selected': False, 'text': '<p>Renaming/moving files with suffixes quickly:<br>\n<code>cp /home/foo/realllylongname.cpp{,-old}</code></p>\n\n<p>This expands to:<br>\n<code>cp /home/foo/realllylongname.cpp /home/foo/realllylongname.cpp-old</code></p>\n'}, {'answer_id': 68781, 'author': 'jdt141', 'author_id': 10774, 'author_profile': 'https://Stackoverflow.com/users/10774', 'pm_score': 3, 'selected': False, 'text': '<p><code>pushd</code> and <code>popd</code> almost always come in handy</p>\n'}, {'answer_id': 69024, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 3, 'selected': False, 'text': "<p>Using 'set -o vi' from the command line, or better, in .bashrc, puts you in vi editing mode on the command line. You start in 'insert' mode so you can type and backspace as normal, but if you make a 'large' mistake you can hit the esc key and then use 'b' and 'f' to move around as you do in vi. cw to change a word. Particularly useful after you've brought up a history command that you want to change.</p>\n"}, {'answer_id': 69031, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 1, 'selected': False, 'text': '<p>This prevents <a href="http://en.wikipedia.org/wiki/Less_%28Unix%29" rel="nofollow noreferrer">less</a> (less is more) from clearing the screen at the end of a file:</p>\n\n<pre><code>export LESS="-X"\n</code></pre>\n'}, {'answer_id': 69033, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 0, 'selected': False, 'text': '<p>I prefer reading man pages in vi, so I have the following in my .profile or .bashrc file</p>\n\n<pre><code>man () {\n    sought=$*\n    /usr/bin/man $sought | col -b | vim -R -c "set nonumber" -c "set syntax=man"  -\n}\n</code></pre>\n'}, {'answer_id': 69039, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 2, 'selected': False, 'text': '<p>I have various typographical error corrections in aliases</p>\n\n<pre><code>alias mkae=make\n\nalias mroe=less\n</code></pre>\n'}, {'answer_id': 69041, 'author': 'Bernard', 'author_id': 61, 'author_profile': 'https://Stackoverflow.com/users/61', 'pm_score': 2, 'selected': False, 'text': '<p><kbd>Ctrl</kbd> + <kbd>L</kbd> will usually clear the screen. Works from the Bash prompt (obviously) and in <a href="http://en.wikipedia.org/wiki/GNU_Debugger" rel="nofollow noreferrer">GDB</a>, and a lot of other prompts. </p>\n'}, {'answer_id': 69056, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 2, 'selected': False, 'text': '<p>The easiest keystrokes for me for "last argument of the last command" is !$</p>\n\n<pre><code>echo what the heck?\n\nwhat the heck?\n\necho !$\n\nheck?\n</code></pre>\n'}, {'answer_id': 69058, 'author': 'TChen', 'author_id': 10913, 'author_profile': 'https://Stackoverflow.com/users/10913', 'pm_score': 3, 'selected': False, 'text': '<p>String multiple commands together using the <strong>&amp;&amp;</strong> command:</p>\n\n<pre><code>./run.sh &amp;&amp; tail -f log.txt\n</code></pre>\n\n<p>or</p>\n\n<pre><code>kill -9 1111 &amp;&amp; ./start.sh\n</code></pre>\n'}, {'answer_id': 69087, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 3, 'selected': False, 'text': '<p>One preferred way of navigating when I\'m using multiple directories in widely separate places in a tree hierarchy is to use acf_func.sh (listed below). Once defined, you can do</p>\n\n<p>cd -- </p>\n\n<p>to see a list of recent directories, with a numerical menu</p>\n\n<p>cd -2</p>\n\n<p>to go to the second-most recent directory.</p>\n\n<p>Very easy to use, very handy.</p>\n\n<p>Here\'s the code:</p>\n\n<pre><code># do ". acd_func.sh"\n# acd_func 1.0.5, 10-nov-2004\n# petar marinov, http:/geocities.com/h2428, this is public domain\n\ncd_func ()\n{\n  local x2 the_new_dir adir index\n  local -i cnt\n\n  if [[ $1 ==  "--" ]]; then\n    dirs -v\n    return 0\n  fi\n\n  the_new_dir=$1\n  [[ -z $1 ]] &amp;&amp; the_new_dir=$HOME\n\n  if [[ ${the_new_dir:0:1} == \'-\' ]]; then\n    #\n    # Extract dir N from dirs\n    index=${the_new_dir:1}\n    [[ -z $index ]] &amp;&amp; index=1\n    adir=$(dirs +$index)\n    [[ -z $adir ]] &amp;&amp; return 1\n    the_new_dir=$adir\n  fi\n\n  #\n  # \'~\' has to be substituted by ${HOME}\n  [[ ${the_new_dir:0:1} == \'~\' ]] &amp;&amp; the_new_dir="${HOME}${the_new_dir:1}"\n\n  #\n  # Now change to the new dir and add to the top of the stack\n  pushd "${the_new_dir}" &gt; /dev/null\n  [[ $? -ne 0 ]] &amp;&amp; return 1\n  the_new_dir=$(pwd)\n\n  #\n  # Trim down everything beyond 11th entry\n  popd -n +11 2&gt;/dev/null 1&gt;/dev/null\n\n  #\n  # Remove any other occurence of this dir, skipping the top of the stack\n  for ((cnt=1; cnt &lt;= 10; cnt++)); do\n    x2=$(dirs +${cnt} 2&gt;/dev/null)\n    [[ $? -ne 0 ]] &amp;&amp; return 0\n    [[ ${x2:0:1} == \'~\' ]] &amp;&amp; x2="${HOME}${x2:1}"\n    if [[ "${x2}" == "${the_new_dir}" ]]; then\n      popd -n +$cnt 2&gt;/dev/null 1&gt;/dev/null\n      cnt=cnt-1\n    fi\n  done\n\n  return 0\n}\n\nalias cd=cd_func\n\nif [[ $BASH_VERSION &gt; "2.05a" ]]; then\n  # ctrl+w shows the menu\n  bind -x "\\"\\C-w\\":cd_func -- ;"\nfi\n</code></pre>\n'}, {'answer_id': 69118, 'author': 'Leonard', 'author_id': 10888, 'author_profile': 'https://Stackoverflow.com/users/10888', 'pm_score': 1, 'selected': False, 'text': "<p>When navigating between two separate directories and copying files back and forth, I do this:</p>\n\n<pre><code>cd /some/where/long\nsrc=`pwd`\ncd /other/where/long\ndest=`pwd`\n\ncp $src/foo $dest\n\ncommand completion will work by expanding the variable, so you can use tab completion to specify a file you're working with.\n</code></pre>\n"}, {'answer_id': 69198, 'author': 'Drew Frezell', 'author_id': 10954, 'author_profile': 'https://Stackoverflow.com/users/10954', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve always been partial to:</p>\n\n<pre><code>ctrl-E # move cursor to end of line\nctrl-A # move cursor to beginning of line\n</code></pre>\n\n<p>I also use <code>shopt -s cdable_vars</code>, then you can create bash variables to common directories.  So, for my company\'s source tree, I create a bunch of variables like:</p>\n\n<pre><code>export Dcentmain="/var/localdata/p4ws/centaur/main/apps/core"\n</code></pre>\n\n<p>then I can change to that directory by <code>cd Dcentmain</code>.</p>\n'}, {'answer_id': 69385, 'author': 'Kimbo', 'author_id': 10960, 'author_profile': 'https://Stackoverflow.com/users/10960', 'pm_score': 1, 'selected': False, 'text': '<p>&lt;anything> | sort | uniq -c | sort -n</p>\n\n<p>will give you a count of all the different occurrences of &lt;anything>.</p>\n\n<p>Often, awk, sed, or cut help with the parsing of data in &lt;anything>.</p>\n'}, {'answer_id': 69396, 'author': 'Kimbo', 'author_id': 10960, 'author_profile': 'https://Stackoverflow.com/users/10960', 'pm_score': 1, 'selected': False, 'text': '<p>du -a | sort -n | tail -99</p>\n\n<p>to find the big files (or directories of files) to clean up to free up disk space.</p>\n'}, {'answer_id': 69449, 'author': 'Steve Lacey', 'author_id': 11077, 'author_profile': 'https://Stackoverflow.com/users/11077', 'pm_score': 4, 'selected': False, 'text': "<p>I use the following a lot:</p>\n\n<p>The <code>:p</code> modifier to print a history result. E.g.</p>\n\n<pre><code>!!:p\n</code></pre>\n\n<p>Will print the last command so you can check that it's correct before running it again. Just enter <code>!!</code> to execute it.</p>\n\n<p>In a similar vein:</p>\n\n<pre><code>!?foo?:p\n</code></pre>\n\n<p>Will search your history for the most recent command that contained the string 'foo' and print it.</p>\n\n<p>If you don't need to print,</p>\n\n<pre><code>!?foo\n</code></pre>\n\n<p>does the search and executes it straight away.</p>\n"}, {'answer_id': 69475, 'author': 'T Percival', 'author_id': 954, 'author_profile': 'https://Stackoverflow.com/users/954', 'pm_score': 2, 'selected': False, 'text': "<p><code>$_</code> (dollar underscore): the last word from the previous command. Similar to <code>!$</code> except it doesn't put its substitution in your history like <code>!$</code> does.</p>\n"}, {'answer_id': 69495, 'author': 'user10109', 'author_id': 10109, 'author_profile': 'https://Stackoverflow.com/users/10109', 'pm_score': 5, 'selected': False, 'text': '<p>My favorite command is "ls -thor"</p>\n\n<p>It summons the <a href="http://en.wikipedia.org/wiki/Thor" rel="nofollow noreferrer">power of the gods</a> to list the most recently modified files in a conveniently readable format.</p>\n'}, {'answer_id': 69560, 'author': 'shiva', 'author_id': 11018, 'author_profile': 'https://Stackoverflow.com/users/11018', 'pm_score': 2, 'selected': False, 'text': '<p>Eliminate duplicate lines from a file</p>\n\n<pre><code>#sort -u filename &gt; filename.new\n</code></pre>\n\n<p>List all lines that do not match a condition</p>\n\n<pre><code>#grep -v ajsk filename\n</code></pre>\n\n<p>These are not necessarily Bash specific (but hey neither is <code>ls -thor</code> :) )</p>\n\n<p>Some other useful cmds:</p>\n\n<p>prtdiag, psrinfo, prtconf - more info <a href="http://shiv.me/2008/09/09/unix-removing-duplicates-from-a-file/" rel="nofollow noreferrer">here</a> and <a href="http://shiv.me/2007/09/08/unix-how-to-check-system-configuration-on-solaris/" rel="nofollow noreferrer">here</a> (posts on my blog).</p>\n'}, {'answer_id': 69716, 'author': 'dajobe', 'author_id': 11177, 'author_profile': 'https://Stackoverflow.com/users/11177', 'pm_score': 4, 'selected': False, 'text': '<pre><code>type -a PROG\n</code></pre>\n\n<p>in order to find all the places where PROG is available, usually somewhere in ~/bin\nrather than the one in /usr/bin/PROG that might have been expected.</p>\n'}, {'answer_id': 69737, 'author': 'paranoio', 'author_id': 11124, 'author_profile': 'https://Stackoverflow.com/users/11124', 'pm_score': 0, 'selected': False, 'text': "<pre><code>alias mycommand = 'verylongcommand -with -a -lot -of -parameters'\nalias grep='grep --color'\n</code></pre>\n\n<p>find more than one word with grep :</p>\n\n<pre><code>netstat -c |grep 'msn\\|skype\\|icq'\n</code></pre>\n"}, {'answer_id': 69756, 'author': 'seth', 'author_id': 8590, 'author_profile': 'https://Stackoverflow.com/users/8590', 'pm_score': 6, 'selected': False, 'text': '<p>My favorite is \'^string^string2\' which takes the last command, replaces string with string2 and executes it</p>\n\n<pre><code>$ ehco foo bar baz\nbash: ehco: command not found\n$ ^ehco^echo\nfoo bar baz\n</code></pre>\n\n<p><a href="http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/" rel="nofollow noreferrer">Bash command line history guide</a></p>\n'}, {'answer_id': 69757, 'author': 'sven', 'author_id': 46, 'author_profile': 'https://Stackoverflow.com/users/46', 'pm_score': 2, 'selected': False, 'text': '<p>Not really obscure, but one of the features I absolutely <em>love</em> is tab completion.<br>\nReally useful when you are navigating trough an entire subtree structure, or when you are using some obscure, or long command!</p>\n'}, {'answer_id': 69848, 'author': 'Jon Ericson', 'author_id': 1438, 'author_profile': 'https://Stackoverflow.com/users/1438', 'pm_score': 1, 'selected': False, 'text': '<p>A few years ago, I discovered the <strong>p*</strong> commands or get information about processes: <strong>ptree</strong>, <strong>pgrep</strong>, <strong>pkill</strong>, and <strong>pfiles</strong>.  Of course, the mother of them all is <strong>ps</strong>, but you need to pipe the output into <strong>less</strong>, <strong>grep</strong> and/or <strong>awk</strong> to make sense of the output under heavy load.  <strong>top</strong> (and variants) help too.</p>\n'}, {'answer_id': 69870, 'author': 'tzot', 'author_id': 6899, 'author_profile': 'https://Stackoverflow.com/users/6899', 'pm_score': 0, 'selected': False, 'text': '<p>You changed to a new directory and want to move a file from the new directory to the old one. In one move: <code>mv file $OLDPWD</code></p>\n'}, {'answer_id': 69974, 'author': 'user11285', 'author_id': 11285, 'author_profile': 'https://Stackoverflow.com/users/11285', 'pm_score': 0, 'selected': False, 'text': '<p>If I am searching for something in a directory, but I am not sure of the file, then I just grep the files in the directory by:</p>\n\n<pre><code>find . -exec grep whatIWantToFind {} \\;\n</code></pre>\n'}, {'answer_id': 70190, 'author': 'Weidenrinde', 'author_id': 11344, 'author_profile': 'https://Stackoverflow.com/users/11344', 'pm_score': 0, 'selected': False, 'text': '<pre><code>alias -- ddt=\'ls -trFld\'\ndt () { ddt --color "$@" | tail -n 30; }\n</code></pre>\n\n<p>Gives you the most recent files in the current directory. I use it all the time...</p>\n'}, {'answer_id': 70523, 'author': 'user11535', 'author_id': 11535, 'author_profile': 'https://Stackoverflow.com/users/11535', 'pm_score': 3, 'selected': False, 'text': '<p>Similar to many above, my current favorite is the keystroke [alt]. (Alt and "." keys together) this is the same as $! (Inserts the last argument from the previous command) except that it\'s immediate and for me easier to type. (Just can\'t be used in scripts)</p>\n\n<p>eg:</p>\n\n<pre><code>mkdir -p /tmp/test/blah/oops/something\ncd [alt].\n</code></pre>\n'}, {'answer_id': 70637, 'author': 'nikolay', 'author_id': 11505, 'author_profile': 'https://Stackoverflow.com/users/11505', 'pm_score': 2, 'selected': False, 'text': '<p>A simple thing to do when you realize you just typed the wrong line is hit Ctrl+C; if you want to keep the line, but need to execute something else first, begin a new line with a back slash - \\, then Ctrl+C. The line will remain in your history.</p>\n'}, {'answer_id': 71285, 'author': 'Srikanth', 'author_id': 7205, 'author_profile': 'https://Stackoverflow.com/users/7205', 'pm_score': 4, 'selected': False, 'text': "<p>I often have aliases for vi, ls, etc. but sometimes you want to escape the alias. Just add a back slash to the command in front:</p>\n\n<p>Eg:</p>\n\n<pre><code>$ alias vi=vim\n$ # To escape the alias for vi:\n$ \\vi # This doesn't open VIM\n</code></pre>\n\n<p>Cool, isn't it?</p>\n"}, {'answer_id': 71326, 'author': 'pixelbeat', 'author_id': 4421, 'author_profile': 'https://Stackoverflow.com/users/4421', 'pm_score': 2, 'selected': False, 'text': '<p>You should be able to paste the following into a bash terminal window.</p>\n\n<p><strong>Display ANSI colour palette:</strong></p>\n\n<pre><code>e="\\033["\nfor f in 0 7 `seq 6`; do\n  no="" bo=""\n  for b in n 7 0 `seq 6`; do\n    co="3$f"; p="  "\n    [ $b = n ] || { co="$co;4$b";p=""; }\n    no="${no}${e}${co}m   ${p}${co} ${e}0m"\n    bo="${bo}${e}1;${co}m ${p}1;${co} ${e}0m"\n  done\n  echo -e "$no\\n$bo"\ndone\n</code></pre>\n\n<p><strong>256 colour demo:</strong></p>\n\n<pre><code>yes "$(seq 232 255;seq 254 -1 233)" |\nwhile read i; do printf "\\x1b[48;5;${i}m\\n"; sleep .01; done\n</code></pre>\n'}, {'answer_id': 71418, 'author': 'andyuk', 'author_id': 2108, 'author_profile': 'https://Stackoverflow.com/users/2108', 'pm_score': 1, 'selected': False, 'text': '<p>Want to get the last few lines of a log file?</p>\n\n<pre><code>tail /var/log/syslog\n</code></pre>\n\n<p>Want to keep an eye on a log file for when it changes?</p>\n\n<pre><code>tail -f /var/log/syslog\n</code></pre>\n\n<p>Want to quickly read over a file from the start?</p>\n\n<pre><code>more /var/log/syslog\n</code></pre>\n\n<p>Want to quickly find if a file contains some text?</p>\n\n<pre><code>grep "find this text" /var/log/syslog\n</code></pre>\n'}, {'answer_id': 71426, 'author': 'Vilmantas Baranauskas', 'author_id': 11662, 'author_profile': 'https://Stackoverflow.com/users/11662', 'pm_score': 2, 'selected': False, 'text': '<p><strong>CTRL+D</strong> quits the shell.</p>\n'}, {'answer_id': 75005, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>To be able to quickly edit a shell script you know is in your $PATH (do not try with ls...):</p>\n\n<pre><code>function viscr { vi $(which $*); }\n</code></pre>\n'}, {'answer_id': 75152, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Apropos history -- using cryptic carets, etc. is not entirely intuitive.  To print all history items containing a given string:</p>\n\n<pre><code>function histgrep { fc -l -$((HISTSIZE-1)) | egrep "$@" ;}\n</code></pre>\n'}, {'answer_id': 75765, 'author': 'neu242', 'author_id': 13365, 'author_profile': 'https://Stackoverflow.com/users/13365', 'pm_score': 1, 'selected': False, 'text': '<p>The FIGNORE environment variable is nice when you want TAB completion to ignore files or folders with certain suffixes, e.g.:</p>\n\n<pre><code>export FIGNORE="CVS:.svn:~"\n</code></pre>\n\n<p>Use the IFS environment variable when you want to define an item separator other than space, e.g.:</p>\n\n<pre><code>export IFS="\n"\n</code></pre>\n\n<p>This will make you able to loop through files and folders with spaces in them without performing any magic, like this:</p>\n\n<pre><code>$ touch "with spaces" withoutspaces\n$ for i in `ls *`; do echo $i; done\nwith\nspaces\nwithoutspaces\n$ IFS="\n"\n$ for i in `ls *`; do echo $i; done\nwith spaces\nwithoutspaces\n</code></pre>\n'}, {'answer_id': 76086, 'author': 'Mostlyharmless', 'author_id': 12881, 'author_profile': 'https://Stackoverflow.com/users/12881', 'pm_score': 3, 'selected': False, 'text': '<p><code>!</code>&lt;first few characters of the command> will execute the last command which matches.</p>\n\n<p>Example:</p>\n\n<p><code>!b</code> will run "build whatever -O -p -t -i -on"\n<code>!.</code> will run <code>./a.out</code></p>\n\n<p>It works best with long and repetitive commands, like compile, build, execute, etc. It saved me sooo much time when coding and testing.</p>\n'}, {'answer_id': 88815, 'author': 'Robert Swisher', 'author_id': 1852, 'author_profile': 'https://Stackoverflow.com/users/1852', 'pm_score': 1, 'selected': False, 'text': '<p>Good for making an exact recursive copy/backup of a directory including symlinks (rather than following them or ignoring them like cp):</p>\n\n<pre><code>$ mkdir new_dir\n$ cd old_dir\n$ tar cf - . | ( cd ../old_dir; tar xf - )\n</code></pre>\n'}, {'answer_id': 93587, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Custom Tab Completion (compgen and complete bash builtins)</p>\n\n<p>Tab Completion is nice, but being able to apply it to more than just filenames is great.  I have used it to create custom functions to expand arguments to commands I use all the time.  For example, lets say you often need to add the FQDN as an argument to a command (e.g. <code>ping blah.really.long.domain.name.foo.com</code>).  You can use compgen and complete to create a bash function that reads your /etc/hosts file for results so all you have to type then is:</p>\n\n<pre><code>ping blah.&lt;tab&gt;\n</code></pre>\n\n<p>and it will display all your current match options.</p>\n\n<p>So basically anything that can return a word list can be used as a function.</p>\n'}, {'answer_id': 94335, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>When running a command with lots of output (like a big "make") I want to not only save the output, but also see it:</p>\n\n<p>make install 2>&amp;1 | tee E.make</p>\n'}, {'answer_id': 94725, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>As a quick calculator, say to compute a percentage:</p>\n\n<pre><code>$ date\nThu Sep 18 12:55:33 EDT 2008\n$ answers=60\n$ curl "http://stackoverflow.com/questions/68372/what-are-some-of-your-favorite-command-line-tricks-using-bash"  &gt; tmp.html\n$ words=`awk \'/class="post-text"/ {s = s $0} \\\n&gt; /&lt;\\/div&gt;/ { gsub("&lt;[^&gt;]*&gt;", "", s); print s; s = ""} \\\n&gt; length(s) &gt; 0 {s = s $0}\' tmp.html \\\n&gt; |  awk \'{n = n + NF} END {print n}\'`\n$ answers=`awk \'/([0-9]+) Answers/ {sub("&lt;h2&gt;", "", $1); print $1}\' tmp.html`\n</code></pre>\n\n<p>and finally:</p>\n\n<pre><code>$ echo $words words, $answers answers, $((words / $answers)) words per answer\n4126 words, 60 answers, 68 words per answer\n$\n</code></pre>\n\n<p>Not that division is truncated, not rounded.  But often that\'s good enough for a quick calculation.</p>\n'}, {'answer_id': 95627, 'author': 'neu242', 'author_id': 13365, 'author_profile': 'https://Stackoverflow.com/users/13365', 'pm_score': 1, 'selected': False, 'text': '<p>Top 10 commands again (like <a href="https://stackoverflow.com/questions/68372/what-are-some-of-your-favorite-command-line-tricks-using-bash#68390">ctcherry</a>\'s post, only shorter):</p>\n\n<pre><code>history | awk \'{ print $2 }\' | sort | uniq -c |sort -rn | head\n</code></pre>\n'}, {'answer_id': 103337, 'author': 'rgcb', 'author_id': 8178, 'author_profile': 'https://Stackoverflow.com/users/8178', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m new to programming on a mac, and I miss being able to launch gui programs from bash...so I have to create functions like this:</p>\n\n<pre><code>function macvim\n{\n/Applications/MacVim.app/Contents/MacOS/Vim "$@" -gp &amp;\n}\n</code></pre>\n'}, {'answer_id': 104649, 'author': 'dr-jan', 'author_id': 2599, 'author_profile': 'https://Stackoverflow.com/users/2599', 'pm_score': 0, 'selected': False, 'text': '<p>I always set my default prompt to "username@hostname:/current/path/name/in/full> "</p>\n\n<pre><code>PS1=\'\\u@\\h:\\w&gt; \'\nexport PS1\n</code></pre>\n\n<p>Saves lots of confusion when you\'re dealing with lots of different machines.</p>\n'}, {'answer_id': 109998, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<pre><code>while IFS= read -r line; do\necho "$line"\ndone &lt; somefile.txt\n</code></pre>\n\n<p>This is a good way to process a file line by line. Clearing IFS is needed to get whitespace characters at the front or end of the line. The "-r" is needed to get all raw characters, including backslashes.</p>\n'}, {'answer_id': 111118, 'author': 'e-satis', 'author_id': 9951, 'author_profile': 'https://Stackoverflow.com/users/9951', 'pm_score': 4, 'selected': False, 'text': '<p>I have got a secret weapon : <a href="http://www.shell-fu.org/" rel="nofollow noreferrer">shell-fu.</a></p>\n\n<p>There are thousand of smart tips, cool tricks and efficient recipes that most of the time fit on a single line. </p>\n\n<p>One that I love (but I cheat a bit since I use the fact that Python is installed on most Unix system now) :</p>\n\n<pre><code>alias webshare=\'python -m SimpleHTTPServer\'\n</code></pre>\n\n<p>Now everytime you type "webshare", the current directory will be available through the port 8000. Really nice when you want to share files with friends on a local network without usb key or remote dir. Streaming video and music will work too.</p>\n\n<p>And of course the classic fork bomb that is completely useless but still a lot of fun :</p>\n\n<pre><code>$ :(){ :|:&amp; };:\n</code></pre>\n\n<p>Don\'t try that in a production server...</p>\n'}, {'answer_id': 127705, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Some Bash nuggets also here:</p>\n\n<p><a href="http://codesnippets.joyent.com/tag/bash" rel="nofollow noreferrer">http://codesnippets.joyent.com/tag/bash</a></p>\n'}, {'answer_id': 139674, 'author': 'edomaur', 'author_id': 14262, 'author_profile': 'https://Stackoverflow.com/users/14262', 'pm_score': 1, 'selected': False, 'text': '<p>One of my favorites tricks with bash is the "tar pipe". When you have a monstrous quantity of files to copy from one directory to another, doing "cp * /an/other/dir" doesn\'t work if the number of files is too high and explode the bash globber, so, the tar pipe :</p>\n\n<p><code>(cd /path/to/source/dir/ ; tar cf - * ) | (cd /path/to/destination/ ; tar xf - )</code></p>\n\n<p>...and if you have netcat, you can even <a href="http://compsoc.dur.ac.uk/~djw/tarpipe.html" rel="nofollow noreferrer">do the "netcat tar pipe" through the network !!</a></p>\n'}, {'answer_id': 139785, 'author': 'jk.', 'author_id': 21284, 'author_profile': 'https://Stackoverflow.com/users/21284', 'pm_score': 2, 'selected': False, 'text': '<p>Delete everything <strong>except</strong> <em>important-file</em>:</p>\n\n<pre><code># shopt -s extglob\n# rm -rf !(important-file)\n</code></pre>\n\n<p>The same in <a href="http://www.zsh.org/" rel="nofollow noreferrer">zsh</a>:</p>\n\n<pre><code># rm -rf *~important-file\n</code></pre>\n\n<p>Bevore I knew that I had to move the important fiels to an other dictionary, delete everything and move the important back again.</p>\n'}, {'answer_id': 152576, 'author': 'neu242', 'author_id': 13365, 'author_profile': 'https://Stackoverflow.com/users/13365', 'pm_score': 1, 'selected': False, 'text': '<p>I have a really stupid, but extremely helpful one when navigating deep tree structures.  Put this in .bashrc (or similar):</p>\n\n<pre><code>alias cd6="cd ../../../../../.."\nalias cd5="cd ../../../../.."\nalias cd4="cd ../../../.."\nalias cd3="cd ../../.."\nalias cd2="cd ../.."\n</code></pre>\n'}, {'answer_id': 153700, 'author': 'Florian Jenn', 'author_id': 23813, 'author_profile': 'https://Stackoverflow.com/users/23813', 'pm_score': 2, 'selected': False, 'text': '<p>Using history substiution characters <code>!#</code> to access the current command line, in combination with <code>^</code>, <code>$</code>, etc.</p>\n\n<p>E.g. move a file out of the way with an "old-" prefix:</p>\n\n<p><code>mv file-with-long-name-typed-with-tab-completion.txt old-!#^</code></p>\n'}, {'answer_id': 164795, 'author': 'Philip Durbin', 'author_id': 19464, 'author_profile': 'https://Stackoverflow.com/users/19464', 'pm_score': 5, 'selected': False, 'text': '<p>Sure, you can "<code>diff file1.txt file2.txt</code>", but Bash supports <a href="http://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html" rel="nofollow noreferrer">process substitution</a>, which allows you to <code>diff</code> the output of commands.</p>\n\n<p>For example, let\'s say I want to make sure my script gives me the output I expect.  I can just wrap my script in &lt;( ) and feed it to <code>diff</code> to get a quick and dirty unit test:</p>\n\n<pre><code>$ cat myscript.sh\n#!/bin/sh\necho -e "one\\nthree"\n$\n$ ./myscript.sh \none\nthree\n$\n$ cat expected_output.txt\none\ntwo\nthree\n$\n$ diff &lt;(./myscript.sh) expected_output.txt\n1a2\n&gt; two\n$\n</code></pre>\n\n<p>As another example, let\'s say I want to check if two servers have the same list of RPMs installed.  Rather than sshing to each server, writing each list of RPMs to separate files, and doing a <code>diff</code> on those files, I can just do the <code>diff</code> from my workstation:</p>\n\n<pre><code>$ diff &lt;(ssh server1 \'rpm -qa | sort\') &lt;(ssh server2 \'rpm -qa | sort\')\n241c240\n&lt; kernel-2.6.18-92.1.6.el5\n---\n&gt; kernel-2.6.18-92.el5\n317d315\n&lt; libsmi-0.4.5-2.el5\n727,728d724\n&lt; wireshark-0.99.7-1.el5\n&lt; wireshark-gnome-0.99.7-1.el5\n$\n</code></pre>\n\n<p>There are more examples in the \nAdvanced Bash-Scripting Guide at <a href="http://tldp.org/LDP/abs/html/process-sub.html" rel="nofollow noreferrer">http://tldp.org/LDP/abs/html/process-sub.html</a>.</p>\n'}, {'answer_id': 164842, 'author': 'Trenton', 'author_id': 2601671, 'author_profile': 'https://Stackoverflow.com/users/2601671', 'pm_score': 1, 'selected': False, 'text': "<p>On Mac OS X,</p>\n\n<pre><code>ESC .\n</code></pre>\n\n<p>will cycle through recent arguments in place. That's: press and release <code>ESC</code>, then press and release <code>.</code> (period key). On Ubuntu, I think it's <code>ALT+.</code>.</p>\n\n<p>You can do that more than once, to go back through all your recent arguments. It's kind of like <kbd>CTRL</kbd> + <kbd>R</kbd>, but for arguments only. It's also much safer than <code>!!</code> or <code>$!</code>, since you <strong>see</strong> what you're going to get before you actually run the command.</p>\n"}, {'answer_id': 171938, 'author': 'edomaur', 'author_id': 14262, 'author_profile': 'https://Stackoverflow.com/users/14262', 'pm_score': 5, 'selected': False, 'text': "<p>How to list <em>only</em> subdirectories in the current one ?</p>\n\n<pre><code>ls -d */\n</code></pre>\n\n<p>It's a simple trick, but you wouldn't know how much time I needed to find that one !</p>\n"}, {'answer_id': 171943, 'author': 'Richard Walton', 'author_id': 15075, 'author_profile': 'https://Stackoverflow.com/users/15075', 'pm_score': 1, 'selected': False, 'text': '<pre><code>sudo !!\n</code></pre>\n\n<p>Runs the last command with administrator privileges. </p>\n'}, {'answer_id': 173682, 'author': 'Oli', 'author_id': 22035, 'author_profile': 'https://Stackoverflow.com/users/22035', 'pm_score': 0, 'selected': False, 'text': '<pre><code>find -iregex \'.*\\.py$\\|.*\\.xml$\' | xargs egrep -niH \'a.search.pattern\'  | vi -R -\n</code></pre>\n\n<p>Searches a pattern in all Python files and all XML files and pipes the result in a readonly <a href="http://en.wikipedia.org/wiki/Vim_%28text_editor%29" rel="nofollow noreferrer">Vim</a> session.</p>\n'}, {'answer_id': 328497, 'author': 'kchoose2', 'author_id': 39870, 'author_profile': 'https://Stackoverflow.com/users/39870', 'pm_score': 0, 'selected': False, 'text': '<p>Two of my favorites are:</p>\n\n<p>1) Make tab-completion case insensitive (e.g. "cd /home/User " converts your command line to: "cd /home/user" if the latter exists and the former doesn\'t. You can turn it on with "set completion-ignore-case on" at the prompt, or add it permanently by adding "set completion-ignore-case on" to your .inputrc file.</p>\n\n<p>2) The built-in \'type\' command is like "which" but aware of aliases also. For example</p>\n\n<pre><code>$ type cdhome\ncdhome is aliased to \'cd ~\'\n$ type bash\nbash is /bin/bash\n</code></pre>\n'}, {'answer_id': 347005, 'author': 'Adrian Pronk', 'author_id': 41861, 'author_profile': 'https://Stackoverflow.com/users/41861', 'pm_score': 0, 'selected': False, 'text': '<p>I like to set a prompt which shows the current directory in the window title of an xterm.  It also shows the time and current directory.  In addition, if bash wants to report that a background job has finished, it is reported in a different colour using ANSI escape sequences.  I use a black-on-light console so my colours may not be right for you if you favour light-on-black.</p>\n\n<pre><code>PROMPT_COMMAND=\'echo -e "\\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\\007\\033[1;31m${PWD/#$HOME/~}\\033[1;34m"\'\nPS1=\'\\[\\e[1;31m\\]\\t \\$ \\[\\e[0m\\]\'\n</code></pre>\n\n<p>Make sure you understand how to use <code>\\[</code> and <code>\\]</code> correctly in your PS1 string so that bash knows how long your prompt-string actually renders on screen. This is so it can redraw your command-line correctly when you move beyond a single line command.</p>\n'}, {'answer_id': 385293, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I want to mention how we can redirect top command output to file using its batch mode (-b)</p>\n\n<p>$ top -b -n 1 > top.out.$(date +%s)</p>\n\n<p>By default, top is invoked using interactive mode in which top runs indefinitely and accepts keypress to redefine how top works.</p>\n\n<p>A post I wrote can be found <a href="http://unstableme.blogspot.com/2008/12/redirect-top-command-output-to-file.html" rel="nofollow noreferrer">here</a> </p>\n'}, {'answer_id': 417987, 'author': 'Epitaph', 'author_id': 48725, 'author_profile': 'https://Stackoverflow.com/users/48725', 'pm_score': 2, 'selected': False, 'text': '<p>Using <strong>alias</strong> can be time-saving</p>\n\n<pre><code>alias myDir = "cd /this/is/a/long/directory; pwd"\n</code></pre>\n'}, {'answer_id': 752619, 'author': 'Jean-Marc Liotier', 'author_id': 91034, 'author_profile': 'https://Stackoverflow.com/users/91034', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.shell-fu.org/" rel="nofollow noreferrer">Shell-fu</a> is a place for storing, moderating and propagating command line tips and tricks. A bit like StackOverflow, but solely for shell. You\'ll find plenty of answers to this question there.</p>\n'}, {'answer_id': 752702, 'author': 'Rob Hruska', 'author_id': 29995, 'author_profile': 'https://Stackoverflow.com/users/29995', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m a big fan of Bash <a href="http://web.mit.edu/gnu/doc/html/features_5.html" rel="nofollow noreferrer">job control</a>, mainly the use of <code>Control-Z</code> and <code>fg</code>, especially if I\'m doing development in a terminal. If I\'ve got emacs open and need to compile, deploy, etc. I just <code>Control-Z</code> to suspend emacs, do what I need, and <code>fg</code> to bring it back. This keeps all of the emacs buffers intact and makes things much easier than re-launching whatever I\'m doing.</p>\n'}, {'answer_id': 1138910, 'author': 'cb0', 'author_id': 85737, 'author_profile': 'https://Stackoverflow.com/users/85737', 'pm_score': 1, 'selected': False, 'text': "<p>Since I always need the <code>for i in $(ls)</code> statement I made a shortcut:</p>\n\n<pre><code>fea(){\n   if test -z ${2:0:1}; then action=echo; else action=$2; fi\n   for i in $(ls $1);\n      do $action $i ;\n   done;\n}\n</code></pre>\n\n<p>Another one is:</p>\n\n<pre><code>echo ${!B*}\n</code></pre>\n\n<p>It will print a list of all defined variables that start with 'B'.</p>\n"}, {'answer_id': 1250809, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.commandlinefu.com" rel="nofollow noreferrer">http://www.commandlinefu.com</a> is also a great site.</p>\n\n<p>I learned quite useful things there like:</p>\n\n<pre><code>sudo !!\n</code></pre>\n\n<p>or</p>\n\n<pre><code>mount | column -t\n</code></pre>\n'}, {'answer_id': 1453399, 'author': 'chillitom', 'author_id': 56679, 'author_profile': 'https://Stackoverflow.com/users/56679', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Insert preceding lines final parameter</strong></p>\n\n<p><kbd>ALT</kbd>-<kbd>.</kbd>  the most useful key combination ever, try it and see, for some reason no one knows about this one.</p>\n\n<p>Press it again and again to select older last parameters. </p>\n\n<p>Great when you want to do something else to something you used just a moment ago.</p>\n'}, {'answer_id': 1520801, 'author': 'Max Masnick', 'author_id': 173351, 'author_profile': 'https://Stackoverflow.com/users/173351', 'pm_score': 3, 'selected': False, 'text': '<p><code>pbcopy</code></p>\n\n<p>This copies to the Mac system clipboard. You can pipe commands to it...try:</p>\n\n<p><code>pwd | pbcopy</code></p>\n'}, {'answer_id': 1581540, 'author': 'Fergal', 'author_id': 102641, 'author_profile': 'https://Stackoverflow.com/users/102641', 'pm_score': 2, 'selected': False, 'text': "<p><code>alias ..='cd ..'</code></p>\n\n<p>So when navigating back up a directory just use <code>..&lt;Enter&gt;</code></p>\n"}, {'answer_id': 1833646, 'author': 'theschmitzer', 'author_id': 2167252, 'author_profile': 'https://Stackoverflow.com/users/2167252', 'pm_score': 0, 'selected': False, 'text': "<p>How to find which files match text, using find | grep -H\nIn this example, which ruby file contains the jump string -</p>\n\n<p>find . -name '*.rb' -exec grep -H jump {} \\;</p>\n"}, {'answer_id': 2469330, 'author': 'kubi', 'author_id': 28422, 'author_profile': 'https://Stackoverflow.com/users/28422', 'pm_score': 0, 'selected': False, 'text': "<p>Mac only. This is simple, but MAN do I wish I had known about this years ago.</p>\n\n<pre><code>open ./\n</code></pre>\n\n<p>Opens the current directory in Finder. You can also use it to open any file with it's default application. Can also be used for URLs, but only if you prefix the URL with <code>http://</code>, which limits it's utility for opening the occasional random site.</p>\n"}, {'answer_id': 2890180, 'author': 'amphetamachine', 'author_id': 237955, 'author_profile': 'https://Stackoverflow.com/users/237955', 'pm_score': 2, 'selected': False, 'text': '<p><strong><em>Curly-Brace Expansion:</em></strong></p>\n\n<p>Really comes in handy when running a <code>./configure</code> with a lot of options:</p>\n\n<pre><code>./configure --{prefix=/usr,mandir=/usr/man,{,sh}libdir=/usr/lib64,\\\nenable-{gpl,pthreads,bzlib,lib{faad{,bin},mp3lame,schroedinger,speex,theora,vorbis,xvid,x264},\\\npic,shared,postproc,avfilter{-lavf,}},disable-static}\n</code></pre>\n\n<p>This is quite literally my configure settings for ffmpeg. Without the braces it\'s 409 characters.</p>\n\n<p>Or, even better:</p>\n\n<pre><code>echo "I can count to a thousand!" ...{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}...\n</code></pre>\n'}, {'answer_id': 2894785, 'author': 'Thomas  ', 'author_id': 348643, 'author_profile': 'https://Stackoverflow.com/users/348643', 'pm_score': 1, 'selected': False, 'text': '<p>ctrl-u delete all written stuff</p>\n'}, {'answer_id': 2922141, 'author': 'amphetamachine', 'author_id': 237955, 'author_profile': 'https://Stackoverflow.com/users/237955', 'pm_score': 1, 'selected': False, 'text': '<p><strong><em>Quick Text</em></strong></p>\n\n<p>I use these sequences of text all too often, so I put shortcuts to them in by <code>.inputrc</code>:</p>\n\n<pre><code># redirection short cuts\n"\\ew":            "2&gt;&amp;1"\n"\\eq":            "&amp;&gt;/dev/null &amp;"\n"\\e\\C-q":         "2&gt;/dev/null"\n"\\eg":            "&amp;&gt;~/.garbage.out &amp;"\n"\\e\\C-g":         "2&gt;~/.garbage.out"\n\n$if term=xterm\n"\\M-w":           "2&gt;&amp;1"\n"\\M-q":           "&amp;&gt;/dev/null &amp;"\n"\\M-\\C-q":        "2&gt;/dev/null"\n"\\M-g":           "&amp;&gt;~/.garbage.out &amp;"\n"\\M-\\C-g":        "2&gt;~/.garbage.out"\n$endif\n</code></pre>\n'}, {'answer_id': 2922147, 'author': 'amphetamachine', 'author_id': 237955, 'author_profile': 'https://Stackoverflow.com/users/237955', 'pm_score': 1, 'selected': False, 'text': "<p><strong><em>Programmable Completion:</em></strong></p>\n\n<p>Nothing fancy. I always disable it when I'm using Knoppix because it gets in the way too often. Just some basic ones:</p>\n\n<pre><code>shopt -s progcomp\n\ncomplete -A stopped -P '%'          bg\ncomplete -A job     -P '%'          fg jobs disown wait\ncomplete -A variable                readonly export\ncomplete -A variable -A function    unset\ncomplete -A setopt                  set\ncomplete -A shopt                   shopt\ncomplete -A helptopic               help\ncomplete -A alias                   alias unalias\ncomplete -A binding                 bind\ncomplete -A command                 type which \\\n                                    killall pidof\ncomplete -A builtin                 builtin\ncomplete -A disabled                enable\n</code></pre>\n"}, {'answer_id': 2922191, 'author': 'amphetamachine', 'author_id': 237955, 'author_profile': 'https://Stackoverflow.com/users/237955', 'pm_score': 1, 'selected': False, 'text': '<p>Not really interactive shell tricks, but valid nonetheless as tricks for writing good scripts.</p>\n\n<p><strong><em>getopts, shift, $OPTIND, $OPTARG:</em></strong></p>\n\n<p>I love making customizable scripts:</p>\n\n<pre><code>while getopts \'vo:\' flag; do\n    case "$flag" in\n        \'v\')\n        VERBOSE=1\n        ;;\n        \'o\')\n        OUT="$OPTARG"\n        ;;\n    esac\ndone\nshift "$((OPTIND-1))"\n</code></pre>\n\n<hr>\n\n<p><strong><em>xargs(1):</em></strong></p>\n\n<p>I have a triple-core processor and like to run scripts that perform compression, or some other CPU-intensive serial operation on a set of files. I like to speed it up using <code>xargs</code> as a job queue.</p>\n\n<pre><code>if [ "$#" -gt 1 ]; then\n    # schedule using xargs\n    (for file; do\n        echo -n "$file"\n        echo -ne \'\\0\'\n    done) |xargs -0 -n 1 -P "$NUM_JOBS" -- "$0"\nelse\n    # do the actual processing\nfi\n</code></pre>\n\n<p>This acts a lot like <code>make -j [NUM_JOBS]</code>.</p>\n'}, {'answer_id': 2922237, 'author': 'fmsf', 'author_id': 26004, 'author_profile': 'https://Stackoverflow.com/users/26004', 'pm_score': 0, 'selected': False, 'text': '<p>./mylittlealgorithm &lt; input.txt > output.txt</p>\n'}, {'answer_id': 2922244, 'author': 'amphetamachine', 'author_id': 237955, 'author_profile': 'https://Stackoverflow.com/users/237955', 'pm_score': 1, 'selected': False, 'text': "<p><strong><em>Signal trapping:</em></strong></p>\n\n<p>You can trap signals sent to the shell process and have them silently run commands in their respective environment as if typed on the command line:</p>\n\n<pre><code># TERM or QUIT probably means the system is shutting down; make sure history is\n# saved to $HISTFILE (does not do this by default)\ntrap 'logout'                      TERM QUIT\n\n# save history when signalled by cron(1) script with USR1\ntrap 'history -a &amp;&amp; history -n'    USR1\n</code></pre>\n"}, {'answer_id': 3436445, 'author': 'jlucktay', 'author_id': 380599, 'author_profile': 'https://Stackoverflow.com/users/380599', 'pm_score': 3, 'selected': False, 'text': "<p><strong>Duplicate file finder</strong></p>\n\n<p>This will run checksums recursively from the current directory, and give back the filenames of all identical checksum results:</p>\n\n<pre><code>find ./ -type f -print0 | xargs -0 -n1 md5sum | sort -k 1,32 | uniq -w 32 -d --all-repeated=separate | sed -e 's/^[0-9a-f]*\\ *//;'\n</code></pre>\n\n<p>You can, of course, change the path around.<br>\nMaybe put it into a function or alias, and pass in the target path as a parameter.</p>\n"}, {'answer_id': 3539492, 'author': 'Omar Ali', 'author_id': 383819, 'author_profile': 'https://Stackoverflow.com/users/383819', 'pm_score': 2, 'selected': False, 'text': '<p>SSH tunnel:</p>\n\n<pre><code>ssh -fNR 1234:localhost:22 root@123.123.123.123\n</code></pre>\n'}, {'answer_id': 3549013, 'author': 'Ken Chen', 'author_id': 312123, 'author_profile': 'https://Stackoverflow.com/users/312123', 'pm_score': 1, 'selected': False, 'text': '<p>For the sheer humor factor, create an empty file "myself" and then: <code>$ touch myself</code></p>\n'}, {'answer_id': 3724298, 'author': 'Gadolin', 'author_id': 410737, 'author_profile': 'https://Stackoverflow.com/users/410737', 'pm_score': 3, 'selected': False, 'text': '<p>I have plenty of directories which I want to access quickly, <code>CDPATH</code> variable is solution \nthat speed up my work-flow enormously: </p>\n\n<pre><code>export CDPATH=.:/home/gadolin/sth:/home/gadolin/dir1/importantDir\n</code></pre>\n\n<p>now with <code>cd</code> I can jump to any of sub directories of <code>/home/gadolin/sth</code> or <code>/home/gadolin/dir1/importantDir</code> without providing the full path. And also <code>&lt;tab&gt;</code> works here just like I would be there!\nSo if there are directories <code>/home/gadolin/sth/1</code> <code>/home/gadolin/sth/2</code>, I type <code>cd 1</code> wherever, and I am there.</p>\n'}, {'answer_id': 4199377, 'author': 'Wesley Rice', 'author_id': 534235, 'author_profile': 'https://Stackoverflow.com/users/534235', 'pm_score': 0, 'selected': False, 'text': '<pre><code># Batch extension renamer (usage: renamer txt mkd)\nrenamer() {\n   local fn\n   for fn in *."$1"; do\n     mv "$fn" "${fn%.*}"."$2"\n   done\n}\n</code></pre>\n'}, {'answer_id': 4199491, 'author': 'chris', 'author_id': 469276, 'author_profile': 'https://Stackoverflow.com/users/469276', 'pm_score': 1, 'selected': False, 'text': '<p>extended globbing:</p>\n\n<pre><code>rm !(foo|bar)\n</code></pre>\n\n<p>expands like <code>*</code> without <code>foo</code> or <code>bar</code>:</p>\n\n<pre><code>$ ls\nfoo\nbar\nfoobar\nFOO\n$ echo !(foo|bar)\nfoobar FOO\n</code></pre>\n'}, {'answer_id': 4201124, 'author': 'Bauna', 'author_id': 450778, 'author_profile': 'https://Stackoverflow.com/users/450778', 'pm_score': 1, 'selected': False, 'text': "<p>pbcopy and pbpaste aliases for GNU/Linux</p>\n\n<pre><code>alias pbcopy='xclip -selection clipboard'\nalias pbpaste='xclip -selection clipboard -o'\n</code></pre>\n"}, {'answer_id': 4256192, 'author': 'hibbelig', 'author_id': 318457, 'author_profile': 'https://Stackoverflow.com/users/318457', 'pm_score': 1, 'selected': False, 'text': '<p>Someone else recommended "M-x shell RET" in Emacs.  I think "M-x eshell RET" is even better.</p>\n'}, {'answer_id': 4615572, 'author': 'bobbogo', 'author_id': 470195, 'author_profile': 'https://Stackoverflow.com/users/470195', 'pm_score': 3, 'selected': False, 'text': '<h3>Expand complicated lines before hitting the dreaded enter</h3>\n\n<ul>\n<li><kbd>Alt</kbd>+<kbd>Ctrl</kbd>+<kbd>e</kbd> &mdash; <em>shell-expand-line</em> (may need to use <kbd>Esc</kbd>, <kbd>Ctrl</kbd>+<kbd>e</kbd> on your keyboard)</li>\n<li><kbd>Ctrl</kbd>+<kbd>_</kbd> &mdash;  <em>undo</em></li>\n<li><kbd>Ctrl</kbd>+<kbd>x</kbd>, <kbd>*</kbd> &mdash;  <em>glob-expand-word</em></li>\n</ul>\n\n<p>$ <code>echo !$ !-2^ *</code> <kbd>Alt</kbd>+<kbd>Ctrl</kbd>+<kbd>e</kbd><br/>\n$ <code>echo aword someotherword *</code> <kbd>Ctrl</kbd>+<kbd>_</kbd><br/>\n$ <code>echo !$ !-2^ *</code> <kbd>Ctrl</kbd>+<kbd>x</kbd>, <kbd>*</kbd><br/>\n$ <code>echo !$ !-2^ LOG Makefile bar.c foo.h</code><br/></p>\n\n<p>&amp;c.</p>\n'}, {'answer_id': 5163728, 'author': 'Mikel', 'author_id': 102182, 'author_profile': 'https://Stackoverflow.com/users/102182', 'pm_score': 2, 'selected': False, 'text': '<p>Not my favorite, by very helpful if you\'re trying any of the other answers using copy and paste:</p>\n\n<pre><code>function $\n{\n    "$@"\n}\n</code></pre>\n\n<p>Now you can paste examples that include a <code>$</code> prompt at the start of each line.</p>\n'}]
['https://Stackoverflow.com/questions/68372', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3499/']