Command Line Tricks

Albina Sitdikova
5 min readMar 26, 2021

During 12-week Metis Data Science Bootcamp I did my investigation about Command Line Tricks. Learning about command line tricks can make your work more productive.

The command line is a text interface for your computer. It’s a program that takes in commands, which it passes on to the computer’s operating system to run. From the command line, you can navigate through files and folders on your computer.

I’m gonna tell in this article about 8 of tricks I found the most useful:

  1. TAB (keyboard shortcut)
  2. Creating aliases
  3. Customize your prompt
  4. Defaults
  5. History
  6. KillAll
  7. Change flags
  8. Bonus

TAB (keyboard shortcut)

TAB is keyboard shortcut to complete file/directory names.

To use it, we are open any directory in terminal, then write begging of name of the directory. For example, write “Pa” (there’s only one match: “Pairs” in example) and press Tab.

After pressing TAB, terminal autocomplete name of directory if there’s only one match:

If there’s few matches. Like for directories start with P(there’s 5 of them in example) you can press tab 2 times and terminal will give you a list of directories beginning of which are matching:

Example of having few folders start with ‘P’ and pressing TAB

Creating aliases

Aliases - custom shortcuts, it all keeps in .bash_profile

How to create aliases:

  1. Navigate to your home directory:

cd ~

2. Open up .bash_profile using vi:

vi .bash_profile

3. Add an alias (press i):

alias $preferredAlias=“$commandToAlias”

4. Save the file (press Escape, type :wq, and hit Enter)

5. Restart Terminal

Aliases starts with ‘alias’ before, name of new command, ‘=’ , any command or list of commands in quotes.

Example:

I added 2 aliases: one change directory to ‘metis’ directory (metis_dir), another one change directory and make git pull and git push requests in ‘metis’ directory(metis_git):

So, now if you type name of alias in terminal it’s gonna run all command you wrote in this alias:

It works!

Customize your prompt

You can customize Bash Prompt with your preferences:

  1. Navigate to your home directory:

cd ~

2. Open up .bash_profile using vi:

vi .bash_profile

3. Add the following line (press i):

export PS1=” $ ”

4. Save the file(press Escape, type :wq, and hit Enter)

5. Restart Terminal

Modify prompt by changing export “PS1= ”

In my example I did:

export PS1= “[\\u@\\H \\W \\@]\\$”

after
before

By default the command prompt is set to [\u@\h \W]\$. The backslash-escaped special characters are decoded as follows:

  • \u: Display the current username .
  • \h: Display the hostname
  • \W: Print the base of current working directory.
  • \$: Display # (indicates root user) if the effective UID is 0, otherwise display a $.

There’s another many special characters that can help to modify prompt.

Defaults

This command is used to tweak applications and system settings via their preference files. You can use it to do things like disable transparency in the menu bar, always show your scroll bars, change trackpad behavior, and much much more.

This command start with “defaults write” and changes you wanna make.

Changed screenshot default location

History

If you print word “history” terminal will show you the list of 500 last commands you used:

KillAll

You can use KILLALL command to force a Mac application to quit if “Force Quit” doesn’t work.

Caution: killall should be used sparingly to avoid accidentally terminating the wrong processes. There is no confirmation prompt to ask if you really do wish to kill the processes, so check carefully beforehand.

Just print “killall [process]” in terminal:

Forcing to quit “QuickTime Player” application

Change flags

This commands lets you view and change the flags on a file or folder. For most people, the only flags you’re going to care about are “hidden” and “nohidden”, which hide and unhide documents, respectively.

Just type in terminal: “chflags [flag:hidden/nohidden] [path to the folder]”

Command to make folder is hidden
Now folder “Is_hidden” is hidden

Bonus: Watch STAR WARS in your terminal

Just a fun thing you can do in your terminal: to watch Star Wars

First of all you need to instal telnet (if not installed yet):

  1. Install Homebrew on Mac OS
  2. Open up .bash_profile using vi:

brew install telnet

3. Hit Return and let Homebrew download and install Telnet to the Mac

And then to watch to see Star Wars IV you can run:

telnet towel.blinkenlights.nl

Enjoy! ;)

I hope my article was useful for you.

Please visit my GitHub to see my projects. Also feel free to reach out me on LinkedIn.

--

--