5 Helpful Linux Shell Tricks You May Not Know About

These are just 5 helpful Linux tricks I’ve picked up in my career and thought would be nice to share, in case there are others that don’t know about them!

  • Did you know that you can use cd without any arguments to go back to your home directory?
  • You can use cd - to go back to the previous directory you were in! If you do it again after the first time, you can switch back and forth.
  • On a similar note, there is actually a directory stack. You can use pushd instead of cd to go to another directory and add it on to your stack. You can see your stack with dirs and use popd to take the topmost directory off the stack and go back to the directory before it.
  • You can use history to see your command history. It should give you a list of numbers and corresponding commands. Then you can use !(the number) to repeat a command. For example, in the following scenario:
    $ history
    101 ls
    102 ssh errietta@errietta-laptop
    103 cd ~/Downloads
    

    In this scenario, you’d use !103 to repeat the command cd ~/Downloads.

  • You can use CTRL-R to search backwards in your command history. I’ve found this incredibly helpful! You could, for example, press CTRL-R followed by the word git to find the previous git push command in your history instead of typing the whole command in again. Save seconds from your day!

I’ve also made a video if you want to see those tricks in action! Enjoy!

2 Comments

  1. Note that most of these tricks are bash-specific. If using, say, dash or ash: `cd -` and `cd` work, `history` exists but !number does not, Ctrl+R works, but the rest does not.

    More bash tips:
    – The previous command is: !!
    – The previous command beginning with “word” is !word
    – ^alpha^beta would take the previous command, replace the first occurrence of “alpha” with “beta”, and run it.
    – If you have a job running in the background, to kill it you can do $ kill %number # where the number is the job number.
    – To bring a job to the foreground: fg
    – To launch a command in the background, add an & after the end.
    – To stop an already running command temporarily and send it to the background: Ctrl+z
    – To run a stopped job in the background: bg
    – To view all running background jobs: jobs

    And there are probably tons more that I can’t think of. It turns out, bash does a lot of stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.