Two Most Useful Commands, If You Live On The Linux/MacOS Terminal



Do you use the linux/Mac OS X terminal a lot? If yes, you must have a few most useful commands. The commands that help you move around terminal fast and gives you the super power to look up your history real fast will be handy on every day use. Calling them as the “Most Useful Ever” may be a bit of exaggeration. But, If you did not know about these linux commands before you will be thankful for these commands.

How to Get Back to Previous Working Directory or How to Toggle between Two Directories?

Moving between directories is one of the frequent operations that one will be doing on the terminal. Often, you will end up with real deep directory structure and want to go back to the previous working directory. If the previous directory that worked was your home directory, you can just type “cd” to get back to your home directory irrespective of where you are now.

However, often you may want to go back to other directories. Naive way to do is retype all the long path to go where you want. Terminal also offers the best solution to go back to the previous working directory. And the command to do that is just

“cd -“.

Is not that short and very sweet?

How to Look Up Your History Real Quick and Execute Commands from History?

If you spend a lot time on a Linux/MacOS X terminal, frequently you will be executing commands that you used before. The Linux shell that you use may offer simple solution like using up arrow keys to go back to the command you used before. If you know little bit about history, you might even cut down the number of keystrokes by using exclamation “!” and the first few letters of the command that you used before. However, there may be many commands that look similar or you may have to exact remember what you typed.

Possibly the most convenient way to look up your history is to use the “reverse-i-search”. Type

Ctrl and r

in the terminal to invoke the “reverse-i-search”. After typing “Ctrl – r”, you will see

(reverse-i-search)`’:

Then you type parts of the command that you wanted to reuse to look up in your history. As you type, “Reverse-i-search” will display a command that contains what you typed. The best part is, if the command it returns is not the one you want, type Ctrl-r and it will search back in the history and give another command that matched your query.

For example, if you look for a “scp” command that you used before, type “Ctrl-r” and “scp” to get the most recently used command containing scp. If the first result isn’t the one that you wanted, type Ctrl r to keep get another command that matahces your query in your history.