Linux Command Line - Tips and Tricks
Overview
I use a terminal pretty often in my daily job - either when logging in to servers or on my Macbook. In the spirit of learning, I decided to put together a document with some tips/tricks/shortcuts that I have learned over the years.
Working with your history
A bash shell keeps a history of every command you run in the terminal. Here are some commands I rely on daily:
You can view your history with history
:
$ history | grep git
12 git diff
31 cd git
70 git submodule update --init --recursive
72 git status
73 git add .
74 git status
77 vim .git/config
78 git status
79 rm -rf .git
80 git status
81 git init
Each line has a line number - this won’t change as long as your history is saved. If you want to run a command from your history, you can do something like: !$line_number
:
$ !80
git status
One of my favorite commands (and it saves me the most time) is the reverse history search. You can search the most recent commands you have ran by pressing ctrl + r
:
(reverse-i-search)`':
Then you can start typing. Say you want to find the last docker
command you ran, start typing ‘docker’:
(reverse-i-search)`doc': docker kill 80486e6e1927
You’ll see, after I type ‘doc’ the last docker command appears. You can now scroll through all matches by pressing ctrl + r
again. Once you find the command you want, you can hit enter to run it. You can also hit backspace if you typo a letter you don’t want to search.
Assorted tips and tricks
Forget to sudo a command? Instead of typing it again, or scrolling back and typing sudo, simply run sudo !!
:
(1:1002)$ ls /etc/sensu/
ls: cannot open directory /etc/sensu/: Permission denied
(1:1002)$ sudo !!
sudo ls /etc/sensu/
conf.d config.json config.json.example extensions handlers
This will run the last command again, but with sudo applied.
Go back to last directory you were in - did you cd
to a directory and want to go back to where you were? Simply run cd -
(1:1003)$ cd /opt/chef
(1:1004)$ cd /tmp/
(1:1005)$ cd -
/opt/chef
(1:1006)$ pwd
/opt/chef
Git
I also use the Git command line pretty often. Git can be very confusing - especially when starting out. These commands are very basic - but they help me out almost daily.
Need to create a new branch quickly? Just use checkout -b
:
git checkout -b new-branch
Make changes to the wrong branch, and need to switch branches without losing your changes? You can just use git stash
git diff
diff --git a/README.md b/README.md
index a058cd7..7ad7ca6 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
+Some code I don't want added on this branch.
Stashing saves the changes to your “stash”:
git stash
Saved working directory and index state WIP on new-changes: a7e7137 Merge pull request #102
Then you can switch branches, and apply the changes from your stash:
git checkout -b different-branch
Switched to a new branch 'different-branch'
f45c898eb299:website z077514$ git stash pop
On branch different-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
One thing to note - git stash pop
removes the code in your stash. If you want to apply the changes and keep your stash, just use git stash apply
. This way you could theoretically apply your stashed changes to more branches.