Jay Taylor's notes
back to listing indexWhat is your single most favorite command-line trick using Bash?
[web search]
We all know how to use What is your single most favorite obscure trick, keyboard shortcut or shopt configuration using bash? |
|||||||||||||
locked by Robert Harvey♦ Oct 5 '11 at 3:09This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center. closed as not a real question by Shog9♦ May 27 '11 at 21:59It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||
|
158
|
Renaming/moving files with suffixes quickly: This expands to: |
||||||||||||
|
150
|
It's the command-line equivalent of the back button (takes you to the previous directory you were in). |
||||||||||||||||||
|
135
|
Another favorite:
Repeats your last command. Most useful in the form:
|
||||||||||||||||||||||||
|
82
|
My favorite is '^string^string2' which takes the last command, replaces string with string2 and executes it
|
||||||||||||
|
64
|
rename Example:
So useful |
||||||||||||||||||
|
61
|
I'm a fan of the
This comes in handy when you, say
Finally, with pathnames, you can get at parts of the path by appending
|
||||||
|
47
accepted
|
When running commands, sometimes I'll want to run a command with the previous ones arguments. To do that, you can use this shortcut:
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.
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. Setting the following makes bash erase duplicate commands in your history:
I also jack my history size up pretty high too. Why not? It doesn't seem to slow anything down on today's microprocessors.
Another thing that I do is ignore some commands from my history. No need to remember the exit command.
You definitely want to set histappend. Otherwise, bash overwrites your history when you exit.
Another option that I use is cmdhist. This lets you save multi-line commands to the history as one command.
Finally, on Mac OS X (if you're not using vi mode), you'll want to reset <CTRL>-S from being scroll stop. This prevents bash from being able to interpret it as forward search.
|
||||||
|
43
|
How to list only subdirectories in the current one ?
It's a simple trick, but you wouldn't know how much time I needed to find that one ! |
||||||||||||||||||
|
41
|
ESC. Inserts the last arguments from your last bash command. It comes in handy more than you think.
cd ESC. |
||||||
|
36
|
Sure, you can " 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 <( ) and feed it to
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
There are more examples in the Advanced Bash-Scripting Guide at http://tldp.org/LDP/abs/html/process-sub.html. |
|||
|
35
|
My favorite command is "ls -thor" It summons the power of the gods to list the most recently modified files in a conveniently readable format. |
||||||||||||
|
28
|
More of a novelty, but it's clever... Top 10 commands used:
Sample output:
|
||||||
|
25
|
^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. |
||||||||||||||||||
|
20
|
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: Eg:
Cool, isn't it? |
|||
17
|
Here's a couple of configuration tweaks:
This works the same as (I use Gnome Terminal, you may need to change the escape codes for other terminal emulators.) Bash completion is also incredibly useful, but it's a far more subtle addition. In
This will enable per-program tab-completion (e.g. attempting tab completion when the command line starts with Works nicely with this also in
|
|||
17
|
I use the following a lot: The
Will print the last command so you can check that it's correct before running it again. Just enter In a similar vein:
Will search your history for the most recent command that contained the string 'foo' and print it. If you don't need to print,
does the search and executes it straight away. |
||||||
|
16
|
I have got a secret weapon : shell-fu. There are thousand of smart tips, cool tricks and efficient recipes that most of the time fit on a single line. One that I love (but I cheat a bit since I use the fact that Python is installed on most Unix system now) :
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. And of course the classic fork bomb that is completely useless but still a lot of fun :
Don't try that in a production server... |
||||||
|
12
|
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.
|
||||||
|
11
|
in order to find all the places where PROG is available, usually somewhere in ~/bin rather than the one in /usr/bin/PROG that might have been expected. |
||||||
|
11
|
I like to construct commands with echo and pipe them to the shell:
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. |
||||||||||||
|
10
|
When downloading a large file I quite often do:
And then just ctrl+c when I'm done (or if Another useful tool is netcat, or
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 |
||||||
|
10
|
|
|||
|
10
|
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 cd -- to see a list of recent directories, with a numerical menu cd -2 to go to the second-most recent directory. Very easy to use, very handy. Here's the code:
|
|||
|
10
|
Expand complicated lines before hitting the dreaded enter
$ &c. |
|||
9
|
I've always been partial to:
I also use
then I can change to that directory by |
||||||
|
8
|
This copies to the Mac system clipboard. You can pipe commands to it...try:
|
|||
7
|
|
|||
7
|
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. |
|||
|
7
|
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) eg:
|
||||||
|
7
|
String multiple commands together using the && command:
or
|
||||||||||||
|
Not the answer you're looking for? Browse other questions tagged bash command-line or ask your own question.
asked |
7 years ago |
viewed |
110460 times |
active |
Linked
Related
Hot Network Questions
- Why would alien vehicles be tripods?
- As Easy as One-Two-Three
- In long-lived races, how far should one track one's own family tree for relationship purposes?
- Which map is the correct one?
- Trouble installing new bolts into rusted welded nuts
- How can I prevent nohup from creating the file ~/nohup.out?
- Is ‘hallöchen’ appropriate between a professor and a student?
- Hate/love conundrum
- 5V DC over Coax
- Tyre pressures on a roadbike
- Clinton's “wonky”
- How to find a more precise value of integral?
- What does % do in Linux shell strings?
- Can there be an injective function that its derivative is equivalent to its inverse function?
- Is it ethical for a PI to tell university to pay out intern's salary for an extra month after internship ends, and keep the money?
- Am I obligated to continue to work on a paper I contributed significantly to?
- A Riddle Within a Riddle
- Are design patterns frowned upon in agile development?
- Is roof-hopping through a city realistic?
- usermod to change user password is not working
- How to check if a hook is hooked or not?
- Requesting time off on behalf of spouse for surprise vacation
- Can I accrue miles from award ticket awarded from another airlines
- Will increased precipitation in Antarctica prevent sea level rise?
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|