5 Terminal commands every developer should know

Image taken from https://itnext.io/upgrading-bash-on-macos-7138bd1066ba

1. Source command

Sample

We often switch between the versions by changing the JAVA_HOME in ~/.bash_profile

~/.bash_profile pointing to Java 11
~/.bash_profile pointing to Java 8

If we edit the ~/.bash_profile file, we need to restart our terminal for the changes to reflect

But with source command, we can see the changes reflect in current terminal session itself. Just do

> source ~/.bash_profile

2. Tree command

tree command in action

But there can be a scenario where you may want to skip few folders as they have so many files in it. Best example is node_modules folder in Node based applications

So lets see how to ignore a folder in tree command

// For ignoring single folder
> tree -I Docker
// For ignoring folders which match the regex
> tree -I Dock*
// For ignoring multiple folders in single command
// Double quotes are mandatory for this

> tree -I "Dock*|Kubernetes"
tree command ignoring Docker folder

Refer to other interesting tree command flags here — https://www.computerhope.com/unix/tree.htm

3. Sudo Previous Command

But do we really need to edit the previous command and add the sudo prefix to it ? We don’t have to

You can do sudo !! command which will rerun the previous command with sudo prefix

4. History Command

history command list down all previous commands we run in terminal

// Sample output for history command
> history
540 clear541 sudo ~/.bash_profile542 vi ~/.bash_profile

Let’s say we want to rerun the command vi ~/.bash_profile from above result. We don’t have to copy paste the command again

Simply run

> !542

542 is the number given for vi ~/.bash_profile command

Note: All commands listed by history command are numbered

We can also search for a particular command among history by doing grep on history command

> history | grep bash_profile

5. Alias Command

Let’s say we often edit .bash_profile with sudo vi ~/.bash_profile

Instead of typing the command every time, we can create an alias for it

// Created alias called bedit
> alias bedit='sudo vi ~/.bash_profile'
// we can simply run bedit hereafter
> bedit
// If you want to remove the alias
> unalias bedit

Thats all folks !

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store