Useful Shell&Command Line Commands

#Commands #index #shell #СD

No matter what technology you use when developing software, you use the command line to increase your productivity. In this article, we have prepared a list of shell commands and tricks that are to boost your efficiency.

Disclaimer: the commands listed in the article are used on macOS with Z Shell in Iterm2. The commands may be used for Unix/bash-like shells, too. If you use Windows, it’s better to find other options.

cd-

You have probably already used cd to go to a certain directory. But have you known that you can add the dash - as an argument, and go back to the previous directory?

$ cd /home
~> home
$ cd /my_dir
~> /my_dir
$ cd -
~> /home

You can also use the dash argument - with git checkout. For instance, you can use this to switch between branches quickly.

Shell history

Press the up arrow to have your last commands in the shell history selected.

You can also use ctrl + r combination to search through your shell history in reverse.

Typing history directly will show the entire shell history in the terminal. Typing ![number] will select a command at the number position in the history. For instance, typing !7 will show the seventh command used.

Negative numbers can be also used to identify the number-th last command, e.g.:

$ echo second
$ echo last
$ !-2
-> echo second

!!

If you require the very last command used, include !! in the current command. You will retrieve the last executed command:

$ apt-get install unicorn-factory
> [...] Permission denied
$ sudo !!
-> sudo apt-get install unicorn-factory

!:[index]

You can also select just some parts of a command. To do that, include !:[index] in the command. The [index] should include the argument of the previous in it.

$ echo hello world 
> hello world 
$ echo !:1
> hello

You can select ranges with [index]-[index]. For instance,

$ echo live long and prosper
> live long and prosper
$ echo !:3-4
> and prosper

Current line editing

Editing the current line may be useful in cases when you have selected command from the history, but still need to minorly edit it.

Move your cursor to the beginning of the line by pressing ctrl + a combination, or move it to the line’s end by pressing ctrl + e.

Aside from these, there are other useful button combinations:

  1. ctrl + w cuts the word positioned left to the current cursor state;
  2. alt + d cuts the word positioned right to the cursor;
  3. ctrl + k cuts everything positioned right to the cursor;
  4. ctrl + u cuts everything positioned left to the cursor;
  5. ctrl + y reverts the last cut, placing back things you have just cut.

ctrl + x + e

Imagine you write a long string, and it probably includes cycles and branches, and then you find out you have to edit something in the middle of the string. In such cases, press ctrl + x + e to open VIM (or Nano. VI, etc.), where you can continue editing your multi-string command.

Pasting a modified command from history

You can complete retrieving a command and modifying it in one step instead of two.

To do that, use ^x^y. This retrieves the previous command with x replaced by y.

For instance:

$ gti status
> Command 'gti' not found,
$ ^gti^git
> git status

Processing multiple files with a single command

You have probably used commands cp and mv for files processing. However, there is a useful command {} that expands parameters.

After using {}, the shell is instructed to expand each value inside the curly brackets:

$ mv hello_world.{js,html} static

The command moves both hello_world.js and hello_world.html files while not requiring typing those twice.

Ranges can be also used here. For instance, here the command moves file1.pngfile2.pngfile3.pngfile4.png, and file5.png to the backup/ directory:

$ mv file{1..5}.png backup/

Conclusion

There are much more commands to help you improve your productivity and efficiency. However, these will be certainly useful on a daily basis.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Previous Topic
How to Become a Developer: a Complete Beginner's Guide
Next Topic
Things to Know about SASS
We Love to Hear From You
For any support, sales, careers, or security inquiry please contact us!

    * - marked fields are required.