What is your favorite Linux terminal trick?

Everyone wants to increase their productivity at the command line. What's your favorite tip for the Linux terminal?
337 readers like this.
bash logo on green background

Opensource.com

Do you remember the first time you watched somebody who really knew their way around the Linux terminal hard at work?

It may have seemed like magic to you. Or, at the very least, it seemed like something out of the movie Hackers.

Of course, in reality, no one is sitting around typing in commands at sixty words per minute, watching screens of output fly by as they give their machine further instructions in a near constant rapidity. But when you get in the zone, sometimes, if only for a few minutes, it can feel that way.

What's your favorite trick for terminal productivity? Maybe it's a simple alias that you set up for a long string of options on a command you use frequently. Maybe it's a collection of short scripts you use to automate the boring part of your workflow. Perhaps it's your mastery of a terminal multiplexer like screen or tmux. Or maybe your memorization of all the Bash keyboard shortcuts is what finally made you feel like a command line hero.

Whatever your favorite trick, take a moment in the spirit of open source and share it with the community. What do you wish you knew when you were starting out at the terminal, and why? Let us know in the comments below.

User profile image.
Opensource.com publishes stories about creating, adopting, and sharing open source solutions. Follow us on Twitter @opensourceway.

80 Comments

Using rxvt-unicode, my favourite hacks add a colour scheme, and a tabbed interface:

$ cat ~/.Xdefaults

! to match gnome-terminal "Linux console" scheme
! foreground/background
URxvt*background: [90]#000000
URxvt*depth: 32
URxvt*foreground: #ffffff
! black
URxvt.color0 : #000000
URxvt.color8 : #555555
! red
URxvt.color1 : #AA0000
URxvt.color9 : #FF5555
! green
URxvt.color2 : #00AA00
URxvt.color10 : #55FF55
! yellow
URxvt.color3 : #AA5500
URxvt.color11 : #FFFF55
! blue
URxvt.color4 : #0000AA
URxvt.color12 : #5555FF
! magenta
URxvt.color5 : #AA00AA
URxvt.color13 : #FF55FF
! cyan
URxvt.color6 : #00AAAA
URxvt.color14 : #55FFFF
! white
URxvt.color7 : #AAAAAA
URxvt.color15 : #FFFFFF

URxvt*font: xft:Monospace:pixelsize=12

URxvt*boldFont: xft:Monospace:pixelsize=12
!URxvt*letterSpace: -1
URxvt.perl-ext-common : default,matcher
URxvt.urlLauncher : firefox
URxvt.matcher.button : 1

! scrollbar style - rxvt (default), plain (most compact), next, or xterm
URxvt.scrollstyle: rxvt
URxvt.scrollBar_right: True
URxvt.iconFile: /usr/share/icons/gnome/256x256/apps/terminal.png
URxvt.keysym.C-Delete: perl:matcher:last
URxvt.keysym.M-Delete: perl:matcher:list

URxvt.colorUL: #4682B4

!TABS
!Shift+ down new tab
!Shift+ left go to left tab
!Shift+ right go to right tab
!Ctrl+ left move tab to the left
!Ctrl+ right move tab to the right
!Ctrl+D: close tab
URxvt.perl-ext-common: default,tabbed
URxvt.tabbed.tabbar-fg: 2
URxvt.tabbed.tabbar-bg: 0
URxvt.tabbed.tab-fg: 3
URxvt.tabbed.tab-bg: 0

The use of <() and $()

Launching a python http server from the command line. Something like 'python3 -m http.server' will make the local folder available via http, which is often faster than nfs or sftp.

Otherwise, aliases. My favorite is alias "grin"="grep -rin", which I use all the time to find things in my code.

Aaand, locate, which is a super fast way to find files in Unix, because it's based on a cached database.

My favorite is sudo !! Whenever I forget to type sudo before a command. It'll run the previous command with sudo.

Hi folks,
I have been enjoying using Linux ubuntu 18.04 bionic beaver, it was only recently that I started seriously learning about linux environment and the terminal.

So, my trick that I would to share probably not really a trick is more like when I first started experimenting with installing a package or a piece of software using the terminal, I knew I am suppose to use the:
$> apt-get install [here is name of the software I would like to install]

However, I remember getting an error about unable to acess some file or location or something of that sort.

So I kept scratching my head, but then after a quick online search I realized I need to use the keyword "sudo" in the command, so i should have typed it as follows instead:
$> sudo apt-get install [name of software to install]

Once I clicked enter key on my keyboard, I get prompted to enter a password, this part was a bit confusing to me too, I kept entering my own linux user login password, and it kept failing prompting me that the password is incorrect, gosh!! And I kept doubting my own sanity at that moment, eventually I had remembered that during my linux installation experience that I did come across a screen asking me to input some sort of password for something called 'root". Anyhow, it turned out that the password I am suppose to enter into the terminal was the so called "root password".

So there you go. That was one of my earliest learning experiences with Linux terminal. It has been a joyride learning about doing stuff through the terminal .

Best wishes to everyone

~Sam~

Now you've confused me: the whole point of sudo is that you *do* enter your *own*password when it prompts you.

However, if you were asked for a root password on installation - you didn't say which distro - then you probably don't have the sudoers file set up and are expected to do system administration by running as root.

In reply to by Peaceful Salma…

In many installations, only users in a "wheel" or "admin" group have sudo access. So you may nee to add yourself to that group first.

In reply to by dgrb (not verified)

I often forget sudo as well. If it's a long command to retype, do:

sudo !!

That will rerun the last command with sudo

In reply to by Peaceful Salma…

Funny that you're still struggling unawares with the same problem. You're supposed to enter the current user's password when using sudo, not the root password. If you had typed su, then the root password, then run the command, you'd then have used the root password for running a privilged command.

In reply to by Peaceful Salma…

I love using set -o vi then all my command history is like using a vi editor I can k then search for a command by typing the first few characters. I can also edit Linnea without hitting the left / right arrows repeatedly just use regular vi syntax.

Not so much a shell trick, but I wanted to simulate typing at a command line while I narrated a video. I found a few "simulate typing" utilities, but decided to roll my own. Basically, it's very much like 'echo' except:

chdelay(ch);
putchar(ch);
fflush(stdout);

And chdelay() was a function I wrote that created a delay based on the type of character -- so you could have a longer delay for spaces, and an even longer delay before the end of the line.

With this, I wrote a simple shell script that echoed the command line using my 'simtype' program, then executed the same command line. That meant I didn't need to type my command line while talking about it.
https://www.youtube.com/watch?v=o09FLGmbdp4

As someone else commented...I think my favorite at the moment was aliasing. Although mine is related the ssh into multiple servers. I found typing long server names annoying and taking extra time but once the config file is set up (on the servers themselves as well) it really streamlined bouncing from one server to another.

My config file already had my Identity info... I simply added the following for all my regularly utilized server connections.

Host dev01
HostName somelgservername.businessname.com

Now I can connect from my machine directly to the server by simply typing:
ssh dev01

Adding it to the config on all the servers allows me to jump to another server simply by typing that cmd.

All this assumes one has gone through all the steps of adding your public and private keys as needed and is an extra setup step...but it sure has helped me save time.

I am currently using (and loving) Fedora 28. If I am using the terminal, I am often using vim. To make my terminal experience even more enjoyable, I like to create the file .vimrc in my home directory with the following:

syntax on
set number
colorscheme slate

The next time you use vim, you will have syntax highlighting, line numbers, and the slate colorscheme by default!

Copy a whole directory tree without changing file attributtes:

tax cf - | (cd destination_dir && tar xf - )

Isn't that cp -a ??

In reply to by Francisco Mardones (not verified)

`cp` does not copy hidden files and directories.

In reply to by WhaitWhat (not verified)

I don't understand what it is with these people misinforming so badly. Of course you can use cp to copy directories and hidden files.

In reply to by Shawn H Corey

The trick of cloning a directory with two instanced of tar worked decades ago when many of cp's options didn't exist. In particular, those that preserve user/group/permissions, those that copy symlinks (instead of following them) and other options necessary for making a backup-type clone.

Today, most of it can be done with cp's "-a" and "-R" options, but that's a relatively recent innovation.

In reply to by WhaitWhat (not verified)

The tar method, versus cp or others, is that tar keeps (if you use the right options) permissions AND softlinks. If you use cp, your softlinks go away and you get another copy of the file the softlink pointed to (and no longer a soft link). Tar is a much better method. It lacks though if you are doing /dev, in this case cpio is a better method, or dump and and interactive restore.

In reply to by Francisco Mardones (not verified)

rsync works well for that too.

In reply to by Francisco Mardones (not verified)

Install Yakuake terminal.

Then add it to programs to startup in settings, so it starts when I login.

Disable startup notification in Yakuake so it won't annoy me with a stupid startup message

Change settings so it does not open up on being started.

Change settings to insure it closes on loss of focus.

Adding "Meta + T" shortcut to open (dropdown) the terminal when I press "Meta + T".

This allows me to have a terminal instantly, in hides instantly, runs commands or scripts in the background without any fuss or worry as I can instantly open anything to check it, best of all I can customize the terminal to match my systems "look and feel" (aka theme) even make it translucent, or change the background and foreground colors to my liking.

Best terminal ever!

I love the efficiency of 'dirs, pushed, and popd'. Once mastered the concept it became very natural and a real time saver to traverse an move through directories. Over the years I've often been asked something to the effect of "how do you jump around like that? That's magic." Many times. Also, if anyone is interested, I have a project on GitHub that managed native bag 'dirs' as projects: it's called "dirp"

I work on a lot of projects, and some projects modify the shell environment, operate from deep directories etc.etc. So I use an 'rc' function in bash:
rc () {
source ~/rc/$1.rc
}
to grab my project in that specific file. Since I also use many machines, the rc file self-documents that aspect as well. A great reminder too, just to see the names of the files in the ~/rc directory.

I have a command "ec" that is my editor, it calls 'emacsclient --no-wait 'on the file, but also logs the date, directory and name of the file. The command "ecg" then greps on the file to help me remind myself where this file was, e.g.
ecg sky1
would return
2018-05-21_02:44:17 /home/teuben/QAC/ngVLA sky1.py
2018-05-28_22:21:03 /home/teuben/QAC/test sky1.py
2018-06-10_21:41:39 /home/teuben/QAC test/sky1.py
which is all I need on the screen to get back to something I did a while back.

Install 'pv'. It will give you progressbars on everything. Use it as a replacement for where you would use 'cat'. Pipe things through it. It's awesome.

1. bash prompt that shows hostname with PROMPT_COMMAND set to update terminal title with working dir.
2. bash prompt changes for python venv
3. using terminator to get multiple panes
4. .ssh/config setup to use best practice cipher specs for modern/intermediate/cisco depending on host
5. ansible to setup my environment everywhere
6. pageup/pagedown mapped to history search
7. .ssh/config Host lines contain aliases to avoid unnecessary ssh-keyscan notifications
8. use 'pv' for any apps (like dd) that don't show progress
9. XXX | sudo tee -a filename for adding to system files
10. gpg + gpg-agent for passphrases

Tab completion on the command line was magic to me the first time I saw it used.

I'm a simple guy, I enjoy Ctrl+R to allow searching for previously entered commands in the terminal.

exit -> means all work is done, I can go home

A very simple one: !!
or !tar

I have 'less -FsXR' set to a two letter alias and use that a lot. I don't however like it when less complains when I try to read a directory instead of a file.
I fixed that by putting this in my shell rc-file:

LESSOPEN='|dir=%s;test -d "$dir" && ls -lah --color "$dir"';export LESSOPEN

I use screen for almost all my terminal activity. I usually use screen. I can keep terminal sessions open and reconnect to them with screen -ls and screen -r

Screen enables me to travel between windows systems, Linux and Mac systems and still land back in my development state with emacs still intact.

❤️ screen

I also find 'watch' useful for my monitoring long running iterative processes.

This is fun! I'm using Debian with i3wm and my favorite bash "trick" is cut. It allows you to get a column from a long list defining the divisor character.

AND... touch {fileone.txt,file2.txt,file3.txt} I think is great!

I'm using Debian with i3wm. Loving it!

just to expand on how cool curly bracket file naming is:
touch file{one,2,3}.txt

does the same thing. You can use just a comma for nothing e,g.
touch {fileone.txt,file2.txt,file3.txt,file.txt}

same as:
touch file{one,2,3,}.txt

the last comma gives you the file name "file.txt"

In reply to by Ezequiel Lopez (not verified)

GNU Parallel, it is a little kitchen-sinky, but oh man the power. Take output of anything and operate on them in parallel.. replaces bash loops, xargs, and others.

sudo !! - does the previous command in sudo. Best when you forget to use sudo, and quicker then editing the previous command to sudo before it

oh-my-zsh
fzf
vim
solarized dark everything
Tiling window manager! xmonad

cp /path/to/file{,.bak}

It just makes me feel so clever.

I also recently came across tab completion in a class I took, which saves a lot of time; great for commands you only half remember or for entering long file names. Another neat command I find useful is the script command, which records all the commands that you type in the terminal; I use it for documenting what I have done, because I have to duplicate the work on other servers in the field.

"sudo !!" . Executes the previous command but with sudo at the begin.
Like "fdisk -lu"
permission denied.
"sudo !!"
"sudo fdisk -lu"

You can use !$ which means the last argument of the previous command.

-. (period) does the same and you can fetch any param from the previous cmd via -# (digit, starting at zero for the command itself) to copy and -y to yank it to the cursor.

In reply to by Agent766 (not verified)

Hmm, turned my meta-key names to html tags. That ALT-. (period), ALT-# (digit) to copy and CTRL-ALT-y to yank/paste the copied param

In reply to by afbach

I think someone else mentioned it, but yum update was a real eye-opener for me. Came over from Windows and to be able to use a simple command to do something that had taken many steps was like the heavens opening and Angels singing. I've since explored all the switches and ways to modify the command to make life even easier.

I'm sure I use more, but when I first took over our Red Hat environment and started learning Linux this was easily the most impactful to me.

I know it's lame, but I added a few shortcuts to my .bashrc file to make commands, well, shorter:

[jhibbets@jhibbets ~]$ cat .bashrc
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

alias q='exit'
alias h='cd ~/'
alias c='clear'

Not as lame as you think. lol These are in my profile.

alias ll='ls -lh $LS_OPTIONS'
alias llm='ls -alh $LS_OPTIONS | less'
alias lrt='ls -alhrt $LS_OPTIONS'
alias ls='ls $LS_OPTIONS'
alias lsd='ls -lhd */ 2>/dev/null $LS_OPTIONS'
alias lsnd='ls -lh $LS_OPTIONS |grep -v ^d'
alias ipcalc='ipcalc.pl -nb'
unalias rm
alias cp="cp"
alias mv="mv"
alias df="df -h"
alias vi="/usr/bin/vim"
alias ping="/bin/ping -n"

In reply to by jhibbets

1.) ls -lt lists files by date
2.) The ip commands which has sinced replaced ifconfig tool set.
3.) Reboot "sorry boss I dont know what happened, server should be back up in 10"
4.) Sudo rm -rf \*

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.