This article should only take you a couple of minutes to read, and you'll walk away with a handful of the bash tricks I constantly use.
1. Make a directory and navigate to it
mkdir /foo/bar && cd $_
In bash, $_
is a reference to the last parameter that was used. It can come in handy all over the place. Generally, the best use for $_
is to avoid repeating yourself in cases like the above example.
2. Backup a file without typing it twice
cp /some/path/to/file.txt{,.bak}
This feature of bash is called brace expansion, it has a ton of useful applications. This is just one I sometimes use when I want to create a copy of a file quickly.
3. Backup every file in a folder
for file in * ; do cp "$file" "$file".bak; done
In this oneliner, we are taking advantage of bash's ability to loop over the files in a directory.
Frequently, I find myself using this syntax to avoid using multiple lines.
4. Find a command by its description
apropos "some description"
apropos
is a command that looks through the commands on your machine for one that has a description like the one you provided.
It can be extremely useful when you remember what a command does, but not its name.
5. Repeat the last command with sudo
sudo !!
!!
is a quick way to repeat the last command. I do this a ton.
Feel free to add your own useful bash tricks in the comments. 🤠
Top comments (22)
Nice list.
Please please please please please use double quotes around variables, unless you explicitly desire the shell to interpret the variable value. A simple example to illustrate the issue:
See this Q&A on unix.stackexchange and wooledge: Quotes for more details.
You can also use
man -k
instead ofapropos
For testing shell globs, you can use
echo
Cool! Thanks for the feedback, that is good advice. 🤠
I definitly recommand ${variable}, much better to look and feel more like you can't be wrong about it :)
To piggyback on the last one there, if you
history
:$ history
the output should be a numerical list of commands you've entered in the past, like so:
If I wanted to run
git status
again (ie, repeat command 10016 from history), I could do something like this:$ !10016
That's a great trick! Thanks!
You could also
pipe
history
too! Which could allow you togrep
(and more) within your history:$ history | grep "status"
:)
ctrl-r status will walk back up your history looking at each time the command line contained status - just hit return when you find the command you need.
My favorite piece of knowledge:
If you choose to make an alias for sudo, you should ensure there is a space at the end, or else the shell will not expand any more aliases within the command.
So if I wanted to use please instead of sudo, I would need to do:
I am always tempted to put spaces around the equals sign, probably my #1 scripting mistake.
Some of my regular bash commands:
A simple way to monitor network connectivity and latency -- ping a highly-available domain like google.com:
ping google.com | while read pong; do echo "$(date): $pong"; done
I also like to use alias to shorten frequently-used commands and parameters. Here are some examples from my .bashrc:
alias l="ls -aF"
alias ll="ls -laF"
alias c="cd"
alias m="more"
alias e="env | sort"
alias h='history'
alias p="pwd"
alias g="go run"
alias cd..='cd ..'
alias c..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
Print each PATH entry on a separate line:
alias path='echo -e ${PATH//:/\n}'
Open the current directory in macOS Finder:
alias o="open ."
Really cool! Just learned about $_.
If anyone wants, I just created a function to perform the first item on the list in a single command.
This should work with bash and zsh, and should be added to their config files(.bashrc and .zshrc, respectively).
Just a heads up, globbing in your "backup every file" example will miss all dotfiles. You could do a dual expansion in this case
for i in * .*; do echo "${i}"; done
but this also includes.
and..
in this glob. YMMV depending on what you're doing with the list.Thanks for the heads up!
ctrl+r to search back through your command history to rerun a command is something that I find surprises people
vim folks will also love setting vi as the keyboard shortcuts to edit lines in Bash with
set -o v
within their profile. you can then up arrow (or ctrl+r) to find a previous command, hit Esc to exit insert mode to activate vi shortcuts then to edit the line. For example command0
(zero) takes you to the beginning of the line,w
jumps over words,r
will let you overwrite,a
to start appending,d3w
will delete three words. Using vi shortcuts can save a serious amount of typing.Folks on windows who like bash and who use visual studio code might like my post on setting Git Bash to be the built in terminal within VS Code.
Thanks for these, sometimes it's the little things that you forget and need to read to be reminded. The create folder and change to it is priceless, I turned my one into a function so I can use it generically.
When using mkdir to create /foo/bar adding -p makes sure that /foo is also created if is does not exist yet.
In number 3, is moving every file in a folder just an example to illustrate using a for loop? Or is there a reason why you wouldn't do it like this:
Haha, no.
I updated it to a more legitimate use. I didn't actually want to use the example of making backups twice in a row, but the moving example didn't make sense. 🙃