Terminal Tips, Tricks and Tweaks

If you love using the Terminal and understand the power behind it. Then you will most likely love this. Some of the things in this post will help improve our terminal experience and productivity. Other things will be just to have some fun.

Terminal Tips, Tricks and Tweaks

Fast Commands

On Unix based Kernels such as the Linux, BSD, Solaris, and even the Mac OSX terminals pushing the up and down keys will allow you to scroll threw commands you previously used. So if the last command you used was ls -al then pushing the up key will bring you to that command. So you wont need to type the entire command again. This becomes really useful when dealing with really long commands. Pushing the up key twice shows you the command you used before the last command and pushing the down key brings you back to the last command you where at before you pushed the up key.

Auto Complete

Unfortunately using the above tip can become time consuming if you have to scroll up 100 commands. Having an auto complete feature can save a ton of time for you. The bad news is the terminal doesn’t have such an auto complete feature built in. The good news is it is easy to create. Below is how you can create the auto complete feature.

In your prefered text editor add the following then save the file as .inputrc make sure to have the period in front of it.

"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on

Auto Complete Directories

Lets assume my directory structure looks like the following.

  • Pictures
  • Videos
  • Documents
  • Downloads
  • Music

Now lets say we want to move into the Pictures directory. Normally we would type the following.

cd Pictures

But the terminal on Unix based systems is very intelligent. If you type cd P followed by pressing the Tab key it will finish typing Pictures. But lets say type cd D followed by pressing the Tab key. You will notice nothing happens. This is because their are two directories that start with D. So if we type cd Dow then press the Tab key it will finish typing Downloads for us.

Name Files After A Date

Lets assume you have written a shell script to create files and directories. To ensure you don’t make a file or directory name that already exist you can utilize the date command in your shell script. Below is some examples of using the date command.

echo date

Echo’s the current date in the following format Fri Sep 23 00:15:21 EDT 2016

date '+%m'

Will output the the two digit month number. For January it will output 01 for October it will output 10. If you play around with date and try various letters with % in front of them you will get very great results. Below are some things to try when using the date command.

  • %d (Two digit number for the day of the month)
  • %y (Two digit year number)
  • %Y (Four digit year number)
  • %D (The date formatted like mm/dd/yy)
  • %F (The dated formatted like yyyy-mm-dd)
  • %H (24 hour hour time, at 12 midnight it will be 00 at 1AM it will be 01 and at 1PM it will be 13)
  • %h (Three letter month. September will display Sep)
  • %M (Two digit minute. If it is 12:01 it will display 01 if it is 12:30 it will display 30)

Video Making

If you make a lot of videos as well as love using the terminal. You can use FFMPEG to make video from inside of the terminal. Not only is this something to show off to friends, to brag about how powerful the terminal can be. But it also is much less resource intensive then your GUI video editors. This is because theirs no extra eye candy that hogs your ram. Below are some commands I use when making videos with FFMPEG.

Screen Recording With Sound

ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s XxX -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 $HOME/Videos/$(date '+%m-%d-%Y').mkv

  • Replace XxX with your screen resolution for example 1200×1640
  • Change 30 to increase or decrease the amount of frames per second
  • If you don’t want to record the entire screen and only a portion of the screen get the coordinates of the area you wish to record. In the above example I used :0.0 which records the entire screen.
  • pcm_s16le is the audio codec I use on my linux system. Use the one that you prefer.
  • libx264 is the video codec I use to make the lossless video which outputs to the mkv format.
  • Linux gui systems use a technolagy known as X11 so to record the screen we use X11grab
  • $HOME/Videos/$(date ‘+%m-%d-%Y’).mkv The $HOME/Videos is the directory the video will be saved to. And the name of the video is named after the date.

Screen Recording Without Sound

ffmpeg -f x11grab -r 30 -s XxX -i :0.0+0,24 -vcodec libx264 -vpre lossless_ultrafast -threads 0 $HOME/Videos/$(date '+%m-%d-%Y').mkv

  • Not much different then recording with sound. But we didn’t need to use the -acodec switch.

Convert To YouTube Ready Format

fmpeg -i video.mkv -vcodec libx264 -vpre hq -crf 22 -threads 0 video.mp4

Even though You Tube will try to convert it for you this will save time and ensure the quality is what you expect.

Convert Video Formats

ffmpeg -i video.mkv video.flv

Replace .flv with the format of your choice. If you want it to be converted to mp4 then replace .flv to .mp4

Shortening Commands With Aliases

As you can see with the above FFMPEG examples commands can become pretty lengthy. The great news is you can create aliases to make them shorter. To do this open up the .bashrc file on your system and add some aliases. Below are some example aliases I use.

alias recsound="ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 "

alias recscreen="ffmpeg -f x11grab -r 25 -s 1280x720 -i :0.0+0,24 -vcodec libx264 -vpre lossless_ultrafast -threads 0 "

alias youtube="ffmpeg -i video.mkv -vcodec libx264 -vpre hq -crf 22 -threads 0 "

alias install="sudo apt-get install -y "

alias update="sudo apt-get update"

So now when I go to install a program using apt-get I don’t need to type sudo apt-get install -y program-name Instead I just type install program name. When I want to record my screen with sound I can simply type recsound video.mkv and it will record my screen and capture my microphone and save the video as video.mkv or what ever I typed for the video name.

Terminal Based Text Editors

  • EMacs (Ships with Mac OSX not created by Apple though it is also on many Unix Systems. If your Unix system doesn’t have it you can easily install it threw your package manager)
  • vi (Ships with many Unix systems, doesn’t ship with Mac OSX if your Unix system isn’t a Mac you can install it if you don’t have it)
  • vim (Built from vi and ships with many Unix systems if your Unix system isn’t a Mac you can install it if you don’t have it)
  • Nano

Play Games With EMacs

If you use EMacs you may or may not know that you can play games inside of it. That says a lot about it’s power since it is supposed to be a text editor. To play games inside of EMacs first you need to start EMacs by typing EMacs into the terminal. Then press the ESC key and type one of the following commands.

  • :tetris (To play Tetris)
  • :snake (To play snake)
  • :pong (To play Ping Pong)
  • :doctor (Not much of a game but more of a psychiatrist that gives you some therapy. No I’m not joking you can try it yourself)
  • Many other games. Just consult the EMacs Wiki

Use Telnet to Watch Star Wars IV A New Hope

You most likely have used telnet a few times before. And since Telnet was the tool primarily used in the 60’s and 70’s to access servers in a time when a web browser was unheard of and GUI was something out of Sci-Fi. Well just like you can login to a server with telnet and you can send emails with telnet. You can also watch Star Wars with telnet. Simply type the following command.


Post by Son Nguyen · in 27-07-2019 · view: 1262