Coder's Cat

Awesome Command-line tips to 10x your productivity

2019-11-26

Manage your configuration like code

Suppose you need to work on another machine, saving your configuration in GitHub will save you much time when re-customize your working environment.

Many developers already do the same thing. Manage any configuration(.bashrc, .vimrc, .emacs, etc) with Git is a good habit you should develop from the beginning of working on Linux/Mac.

The benefits of managing configurations with Git is:

  • Reviewing your change history, which is useful for troubleshooting.
  • Accumulating the tips and configuration.
  • Use it on multiple machines.

If you are not ready to share your configuration with others, use a private GitHub repo for security.

Currently, we can use private repo in GitHub without pay any fees.

Remember to add sensitive files into .gitignore, and DO NOT push files which contain password or secrets into GitHub.

I saw many developers pushed sensitive files into GitHub, and got security issues. Don’t be a fool.

How to get help

Don’t be afraid of so many commands options in the command line working environment.

Developers don’t need to remember everything,

The manual and documents are organized in the same pattern and Google is your final lifebuoy.

Whenever you need help for any command, try these:

Use man to query the manuals

Command man used to display the user manual of any command that we can run on the terminal.

On Ubuntu/Debian, you need to install it from the terminal: sudo apt-get install man.

Except for querying the manual of command, you can also use man to read the manual and details of the function or files you are interested in.

man exec will display on screen:

file:img/2019_11_25_awesome-linux-commands.org_20191127_121607.png

Use command with --help/-h

Any command or script will come with a help message. Whenever you don’t know which option to use for your task, have a try on command --help. Linux/Unix help messages usually designed in a similar format, you need to adapt to it.

file:img/2019_11_25_awesome-linux-commands.org_20191127_122357.png

Google effectively

Learn how to google effectively as a developer, it needs time but rewarding for your development life.

You should know using which keywords to search for command line issues.

For Linux commands “Linux command #keyword#“ should be a good try.

Shell alias

Shell aliases are the most useful tips when you working on Linux/Mac.

Whenever you think one command line will be repeated later(especially the long one), you should add it to your configuration as a alias.

Part of my configuration includes:

alias nis="npm install --save "
alias www='python -m SimpleHTTPServer 8000'
alias svim='sudo vim'
alias mkcd='mkdir_with_name(){ mkdir -p "$1"; cd "$1" }; makedir_with_name '
alias install='sudo apt get install'
alias update='sudo apt-get update; sudo apt-get upgrade'
alias ..="cd .."
alias ...="cd ..; cd .."
alias sock5='ssh -D 8080 -q -C -N -f user@your.server'

Other handy aliases:

30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X

A few more of my favorite Bash aliases

Shell tips

go back to the previous working directory

Suppose you change current working directory into /tmp, finished some tasks and want to go back to the previous working directory, in this case, cd - will help you saving time.

execute the command in history

history will show the commands executed recently

file:img/2019_11_25_awesome-linux-commands.org_20191126_160158.png

From the output, !5 will re-execute command ‘pwd’ If you want to re-execute the last command, you could use !! to finish it.

free/top

free/top used to display the usage statistics of resources, like memory and CPU usage. A more friendly alternative is htop, which display all kind of statistics with visualization.

netstat

netstat is a command-line network utility that displays network connections for Transmission Control Protocol, routing tables, and several network interface and network protocol statistics.

More usage examples listed here.

Combine commands for your task

The best way to use the Linux/Unix is to use the commands like awk, sed, grep, xargs, find, sort, etc., combine them into the function you wanted. Just like what’s I have done for Git repo checking.

Most of the simple tasks like grep word from files, filter files with pattern can be finished with these tiny commands.

cat/head/tail : show the content of files, bat is a Rust clone of cat, which supports syntax highlighting for a large number of programming and markup languages.

grep : Search any line that contains the keyword in a file.

sort: sorts the contents of a text file, line by line.

uniq : Remove duplicated parts from string or files.

find : Find or filter files with patterns.

xargs : Allows tools or commands to accept standard input as arguments.

awk : A scripting language used for manipulating data and generating reports.

Some fancy tools which power your productivity

httpie

httpie is a fancy command line HTTP client with an intuitive UI, it also supports JSON format, with highlighting, and many plugins. Maybe you have used curl or wget, you will forget them when you have tried httpie.

tmux

tmux is a terminal multiplexer, it can manage windows and sessions in your terminal. You can split the screen into multiple sub-panels, you can also detach and re-attach sessions.

If you want to connect to remote servers frequently, this is an invaluable tool for you.

file:img/2019_11_25_awesome-linux-commands.org_20191127_223129.png

Which shell to use

Bash is the default shell on many Linux distributions today.

Zsh is a modern one with features of other Unix/GNU Linux shells such as bash, tcsh, and ksh.

Fish is a smart and user-friendly command-line shell for Linux, macOS. It supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure.

If you work on Mac: Zsh on iterm2 is my most recommended one.

Oh My Zsh is a delightful, open-source, community-driven framework for managing your Zsh configuration. With the configuration from the community, it saves much of your time.

References

tmux guide: https://www.hamvocke.com

Oh-my-zsh: https://ohmyz.sh/

Join my Email List for more insights, It's Free!😋

Tags: System