10 Time-saving Terminal Tips for UNIX Beginners

By Shaumik Daityari

The terminal is the preferred choice of working space for many web developers these days. And why not? With the right set of commands, the terminal can definitely get things done faster. However, beginners make certain time-consuming mistakes. Time is money and in this post, we’ll focus on a few areas that can help you complete your actions quickly and efficiently.

1. Use Tab Completion

This is perhaps the most common mistake I have seen beginners make. Suppose you need to open a file named “my_long_file_name.py” in a text editor. You type the whole name of the file and hit enter — only to realize you misspelled it. So you start typing it again. That’s not how it should work.

Once you have typed 2-3 characters, hit tab and the rest of the name would be competed for you. If you have similar file names (like “my_long_file_name.py” and “my_long_file_name.py.bkp”), the common part would be completed for you and you would be shown both options, as shown below.


The same applies for commands too. Type in part of the command and hit tab to complete the command. If there are similar commands, you would be shown the option just like in case of the files.

There is no downsides of over tabbing, so go ahead and start using it frequently, if you haven’t already.

2. Use !! to prefix to the last command

In a previous article, I mentioned sudo !! runs the previous command with administrator privileges. What I didn’t mention is that all !! does is replace itself with the previous command.

To perform the same task, hit the ‘up’ arrow, followed by the home key (or Fn + <-) to reach the beginning of the command. It really comes down to your personal choice.

In addition, !:n selects the nth argument of the last command (but not the rest of the command), whereas !$ selects the last argument of the last command.


3. Cut and paste lines

If you are typing something long and realized it needs to be put somewhere else, you could cut whatever is there in your current line by pressing Ctrl/Cmd+U. You could then paste it somewhere else by pressing Ctrl/Cmd+Y. They are useful shortcuts, if you don’t want to get messy by selecting text to perform these actions.

4. Clear screen

If you want to clear the output of the previous commands from the terminal’s view, you can run —


There is a shortcut for this, however. Just hit Ctrl/Cmd+L and all the text above your current line is cleared. This shortcut can be used when you are halfway writing a command, without losing it, unlike the clear command.

5. cd to home directory

No matter what your working directory is (run pwd to get your absolute working directory), you can switch back to your home directory (/home/username/) any time by running the following —


Alternately, you can run the following too:

cd ~

Which brings us to your next tip…

6. Relative path to your home directory

You probably know that your home directory is /home/username/, but while working in the terminal, you can use ~ as a relative path to your home directory. For instance:

cd ~

points to:

cd /home/username/

Here’s a look at some other examples.

vi ~/.bash_history
cd ~/git.slides/
scp -r ./ donny@

The commands listed above are equivalent to the following:

vi /home/username/.bash_history
cd /home/username/git.slides/
scp -r ./ donny@

7. Using an alias

I’m sure you must have run long commands that are essential to your work. Commands like the following:

python manage.py runserver --settings=project.development

To avoid typing such long commands every time you need them, you can use an alias.

alias start-dj="python manage.py runserver --settings=project.development"

Next time you run start-dj, what actually gets executed is this — python manage.py runserver --settings=project.development.

8. Use the .bashrc

Using an alias is great, but what’s sad is that they get removed from the memory once you close the terminal. To save them, you need to store them in your .bashrc file. By default, it is stored in your home directory (/home/username/.bashrc or ~/.bashrc). Just paste the alias commands in the .bashrc file and restart the terminal for them to take effect.

If you don’t want to restart the terminal, save the .bashrc file and run the following for the new aliases to take effect.

. ~/.bashrc

Developers can get pretty innovative when customizing commands using aliases. There is a wide variety of custom commands to make your work easier, but I wouldn’t mention any of them because they vary greatly from person to person. Perhaps, you will develop your own shortcuts in due time.

9. Create a file on the go

You can dynamically create a file and with the contents as the output of a function pretty easily.

[command] > sample_file

The file sample_file is created and is replaced if it already exists (without any confirmation). Therefore, you must be careful while doing this.

This command is great for taking database dumps. For example:

mysqldump -u [username] -p -D [database] > db_dump

A consequence of this is that you can easily create empty files by giving an empty command (which of course, has an empty output).

> sample_file

The command above will create an empty file with the name sample_file.

10. Use file contents as arguments to a command

Just like we can store the output of a command into a file, we can use the contents of a file as the input for a command. For example, the following will run the command with the inputs of sample_file as arguments.

[command] < file

A use case of this is to restore an SQL dump.

mysql -u [username] -p -D [database_name] < db_dump

This will first login into MySQL and then run each line in db_dump in the MySQL shell.


Now that you have completed your work on the terminal, you can type exit and hit enter, or you could just press Ctrl/Cmd + D to do the same — your choice.

With this, we come to the end of our list of time savers. Do you have any interesting UNIX tips or aliases you want to share? Feel free to comment below.

  • cyber ghost

    this list is pretty helpful, I would like to add “ctrl l ” which is equivalent to “clear” command.

  • Majid

    Very helpful. Thanks for time and effort.

    • http://dada.theblogbowl.in/ Shaumik Daityari

      Hi Majid,

      Glad you liked it!

  • http://dada.theblogbowl.in/ Shaumik Daityari

    Hi Cyber Ghost,

    I have already added that! Thanks for the suggestion though :)

  • sebastiaan hilbers

    very nice!

  • rahilwazir

    The command clear doesn’t clear the terminal buffer instead we can use reset command

    • http://dada.theblogbowl.in/ Shaumik Daityari


      I mentioned that it clears your screen, nothing more. Although Ctrl + L comes in very handy!

  • sincarne

    An addition to #6 that I use all the time: cd – will return you to your previous directory. I also recommend looking into the z shell script: https://github.com/rupa/z . It uses frequency + recentness to smart cd into folders you use a lot.

  • http://samar.techgaun.com Samar Dhwoj Acharya

    Some more:
    $ !-n where n is any integer will run nth command from last.

    ctrl+shift+c, ctrl+shift+v perform copy and paste respectively

    ctrl+u clears whole line of command, ctrl+w clears single word

    ctrl+a takes you in the beginning of your command and ctrl+e to the end

    ^sometext^replaced^ will run last command with ‘sometext’ replaced with ‘replaced’ eg. ^cat^ls^

    !sometext will run last ‘sometext’ command eg. !find

    !?sometext will run last command containing string ‘sometext’

    These are what came in my mind right now. Sorry if some are already discussed on the article (I just skimmed).

    • http://dada.theblogbowl.in/ Shaumik Daityari

      Thanks for the list. Although I covered most of them, some are still new to me!

  • http://asdf404.net/ asdf404

    Press Ctrl+R and beginning of command for search in history. Press Ctrl+R again for find next.

    • Pete

      That’s a great one asdf404! Thanks.
      This one’s very basic, but useful: if you’re halfway through a command you’ve mucked up, you don’t want return and risk running it, your want ‘ctrl-C’.

  • http://dada.theblogbowl.in/ Shaumik Daityari


    Actually I mentioned “cd -” in my last tutorial! And yes, the z script looks interesting.

  • Milan

    Great tips!
    I also found the following to be very very useful:

  • RanM

    …or you could simply use a decent GUI and and equally decent development environment… ;-)

  • Andrei Barbos

    A few more shortcuts which I stumbled upon and which might come in handy are:

    – CTRL+Z which suspends the execution of the process; to bring it back to life one can use either “fg” or “bg” command

    – use the &(ampersand) sign at the end of a command to make it run in the background

    – use the tee command to split the output of a program so that it can be both displayed and saved in a file ( http://en.wikipedia.org/wiki/Tee_(command) )

    – touch can be used to create an empty file, as an alternate to the ” > sample_file” construction, e.g. touch sample_file.

    – use “command >> sample_file” to append at the end of the sample_file; if sample_file already exists, then using “>” will rewrite the sample_file.

    Hope others will find them useful.




Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Instant Website Review

Use Woorank to analyze and optimize your website to improve your website to improve your ranking!

Run a review to see how your site can improve across 70+ metrics!

Get the latest in Front-end, once a week, for free.