DEV Community

loading...
Cover image for 10 simple Linux tips which save 50% of my time in the command line

10 simple Linux tips which save 50% of my time in the command line

javinpaul on August 18, 2019

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided i...
pic
Editor guide
Collapse
citizen428 profile image
Michael Kohl • Edited

Technically these are not "Linux commands", but builtins/features of the shell you're using. They'll work on other OS that run the same shells too and might not work in different shells even on Linux:

$ !!
/bin/ksh: !!: not found
$ pushd
ksh: pushd: not found
$ 
Enter fullscreen mode Exit fullscreen mode

Even switching Bash from emacs to vi mode has an effect on some of the things you mention here, i.e. CTRL-R for history:

$ bindkey|grep hist
"^N" down-line-or-history
"^O" accept-line-and-down-history
"^P" up-line-or-history
"^R" history-incremental-search-forward
"^S" history-incremental-search-forward
[snip]


$ set -o vi
$ bindkey|grep hist
"^[OA" up-line-or-history
"^[OB" down-line-or-history
"^[[A" up-line-or-history
"^[[B" down-line-or-history

Enter fullscreen mode Exit fullscreen mode

That's why the drawings by Julia Evans you're using are calling them "bash tricks". Granted, a lot of them will work across shells since they have the same builtins, but IMHO the distinction matters.

Collapse
javinpaul profile image
javinpaul Author

Yes, technically you are correct, most of them are bash built-in. You also mentioned a couple of good points which I didn't know before, thx for adding value.

Collapse
v6 profile image
🦄N B🛡 • Edited

the distinction matters

Bah! He's writing a blog article, not an ISO spec. The point of the title is that it's discoverable, not that it builds a precise reference taxonomy. Consider the neebs like me in the target audience. "What Linux command do I have to run?" is a common question I ask.

However, the distinction is correct.

Collapse
simbo1905 profile image
Simon Massey

Only I do ”set -o vi” in my .bashrc and use ”cntl+r” all the time.

Collapse
citizen428 profile image
Michael Kohl

Cool. Nobody is stopping you or your distribution from customizing your readline config, but on a lot of systems it won’t work out of the box.

Collapse
irobertson profile image
Ian Robertson • Edited

One key I love in Bash and Zsh is meta-dot (or alt-period or escape-period). It is a bit like up arrow, but rather than replacing the curren line, it inserts the last word of the previous line. Pressing it multiple times cycles through multiple preceding lines.

For example, you just ran

somecommand > longfilename

and want to open the resulting file in vi, you can type

vi meta-dot

and longfilename will be inserted on your command line after vi.

Collapse
5422m4n profile image
Sven Assmann

Whether you are in bash or zsh, you can use the ! operator quite flexible:

If we take: echo a b c d as an example

!$          # the last argument: d
!:*         # all the arguments: a b c d (can be shorten !*)
!:1         #  the first argument: a (same as !^)
!:1-3       # arguments from first to third: a b c
!:2-$       # arguments from the second to the last one: b c d
Collapse
javinpaul profile image
javinpaul Author

Wow, nice to know that, how do you type meta-dot? just literal?

Collapse
irobertson profile image
Ian Robertson

Depends on your keyboard layout; most likely your meta key is "alt", so typing a period while holding down the alt key would do the trick. You can also type the escape key, followed by a period.

Collapse
devloco profile image
devloco • Edited
# Did you forget to use "sudo" with a command?
# Use this alias to auto add sudo and re-run it.
#
# usage example:
# > apt update
# Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
# > oops


alias oops='sudo $(fc -ln -1)'
Collapse
hseritt profile image
Harlin Seritt

Those are some great ones. I like the "!" and history # as in ...

hseritt@hseritt-ThinkPad-X1-Carbon-5th:~/dev/python/top/project$ history

...

2177  git add --all
2178  git commit -m "Fixed CMS-12"
2179  git push origin CMS-12-CreateOrganizationsApp 
2180  git checkout master 
2181  git merge CMS-12-CreateOrganizationsApp 
2182  git push origin master 
2183  history
hseritt@hseritt-ThinkPad-X1-Carbon-5th:~/dev/python/top/project$ !2177

This will execute 2177 which is

git add --all

Collapse
simbo1905 profile image
Simon Massey

This is a massive one. You can:

history | grep git

To see all the things you were doing then:

!2177

it's a bit more keystrokes than ”ctrl+r all” to find a command but helps you reread what you did. So i mix these two often.

Collapse
hseritt profile image
Harlin Seritt • Edited

Yeah the point was just using history for clarity for the readers ... you can of course, grep any output you need to. That should be a given. Thanks!

Collapse
jingxue profile image
Jing Xue

Within vim, you can more quickly switch back and forth between two files - buffers, technically - by ctrl-6. If you have more than two files, you can still switch between them by ":bn" where n is a buffer number. That's not slower than ctrl-z then "fg n".

Collapse
javinpaul profile image
javinpaul Author

Thx @Jing Xue, awesome tip for vim users.

Collapse
bhansconnect profile image
Brendan Hansknecht

Nice article. Personally, I feel that a lot of people can get a huge boost in shell productivity by installing oh-my-zsh and some nice plugins.

I'm also pro cobalt2 color scheme(not sure it really helps that much with productivity though)

Collapse
javinpaul profile image
javinpaul Author

@brendan , looks like a nice idea, but most of the time you don't have the luxury of those plugins on highly secure corporate infrastructure like on a big investment banks.

Collapse
8ucik profile image
8ucik

JavinPaul is true about this. On such an infrastructure you can' t have plugins installed and use them all around. Although I have got the option to use zsh I love the idea of using Ctrl + R.

The best useful thing I have found is the option to set aliases. You can have a cd into folder then run a program do a copy of the result and then return to the home folder. Just in one command.

This is a deal breaker for me that when I switch to a Windows environment I can't handle it.

Collapse
stealthmusic profile image
Jan Wedel

Hi, and thanks for the article! I love those shell short cuts. It makes working in it extremely fast and fun!

One meta point: You’ve added those equal signs to underline the headings, this actually looks very bad on mobile browsers or the dev.to app. Just open it on your phone. Would you mind removing them?

Thanks!

Collapse
javinpaul profile image
javinpaul Author

Thx @jan for pointing it out. I have removed them.

Collapse
alvarezgarcia profile image
Alvarez García

Hey nice list.. thanks for sharing it and encourage programmers to use terminal.
I want to share one that I use commonly:

$ grep -nHri 'string'

It searches case insensitive recursively on current dir and shows number of line and file of each occurence.

Collapse
peterwitham profile image
Peter Witham

Great collection of tips, I use the history grep a lot for not only finding what I need but also checking what I did to make sure that in the past I got it right :)

8 is something I still forget to do, which I blame on using those 'other' systems making me actually CD all the time.

Thanks for this.

Collapse
javinpaul profile image
javinpaul Author

@peter , history| grep is the holy grail, I learned a lot by doing just that and learning about what find, grep, and lsof options other teammates are using :-)

Collapse
masinick profile image
Brian Masinick

I keep a script that can run (with very minor changes, if any) across several of the "sh" based shells, including the original AT&T sh, ksh, zsh, and bash, though the newer shells now incorporate quite a bit of the functionality, so I've used them less frequently recently:

directory stack functions

declare -i DNUM=0
DLIST[DNUM]=pwd

function g # go to a directory
{
if builtin cd "$@" >/dev/null && [ ${DLIST[DNUM]} != "$PWD" ]
then
DNUM=DNUM+1
DLIST[DNUM]=$PWD
fi

if DISPLAY exists, then set titlebar and icon.

if [ -n "$DISPLAY" ]; then
    titlebar="$USER @ $HOST : $PWD"
fi
pwd

}

function gb # go back
{
if (( $DNUM > 0 ))
then
DNUM=DNUM-1
fi
g ${DLIST[DNUM]}
}

function gn # go to selected (nth) dir
{
select DIR in echo ${DLIST[*]} | tr " " "\012" | sort -u -y0
do
if [ "$DIR" ]
then
g $DIR
else
g $REPLY
fi
break
done
}

function up # go up n levels
{
declare -i levels

levels=${1}

if [ -z "${1}" ] && [ ${PWD} != "/" ]
then
  g ..
  return $?
fi

while [ ${levels} -gt 0 ] && [ ${PWD} != "/" ]
do
  g ..
  levels=levels-1
done

}

function addpath
{
PATH=$PATH:$1
echo $PATH
}

function todo
{
echo date '+%D %H:%M' >> ~/todo
echo "$*" >> ~/todo
echo >> ~/todo
}

Main # <--- Main

Set the default directory and file protection mask. By default, do

not mask any protection on my ownership, but remove default write

access for the group, and do not give "world" any default access.

(I add or subtract various things, adding functions or procedures

that I am frequently using, and remove them when inappropriate

at a particular assignment. Sometimes I can send portions of a

script by Email; other times sending anything in is frowned upon

so I have to create bits and pieces of my personal tools manually).

export SHELL=~/bin/bash # usually my default shell
umask 022

OS=$(uname)
myterm=$(who am i)
myterm=$(echo $myterm | awk -F" " '{ print $2 }')
echo "The current HOST is $HOST"
echo "The current terminal is $myterm"
echo "The current shell is $SHELL"

return # get out of any functions I may be in

Finished .bashrc

Collapse
jususc profile image
Jusus

I don't know if it's just me but this article read really badly. It's not entirely clear what the actual commands are and your affiliate links all the way through really detract from the important stuff.

Collapse
osninja_io profile image
The OpenShift Ninja

Great article! If someone hasn't also suggested it, I suggest you look at fzf, which makes control-r a much better experience.

github.com/junegunn/fzf

I would also suggest oh-my-zsh which has a lot of plugins and tools that make your command line life easier.

github.com/robbyrussell/oh-my-zsh

Collapse
rlml8 profile image
• Edited

May sound captious but at the first glance, I thought that it would be perfect with the below

Have you ever been* amazed to see …

Collapse
javinpaul profile image
javinpaul Author

thx for suggestion included. And, no you are not captious, keep on suggesting :-)

Collapse
larden profile image
Jakub L. • Edited

sudo !! is one of my favorite tricks in bash. It saves a lot of time!

Collapse
filipesperandio profile image
Filipe Esperandio

Not long ago I started using shell param expansion more and more and it is definitely a booster:
mv path/to/some/file/that/is/long/and/hard/to/type/{file,new_file_name}.txt

Collapse
simbo1905 profile image
Simon Massey • Edited

As bash and vi is preinstalled on corporate linux and modern cloud hosts i have “set -o vi” in my profile to practice using vi keybindings day to day. I stick to bash as i cannot install other shells onto production hosts when firefighting outages. Using vi shortcuts in bash I can up arrow or ctrl+r to a line, hit esc to exit edit mode, and use vi keys to jump around and edit the line super fast. Then in an outage I can open configuration files in vi and know how to edited them very fast.

I think thats a good tip for new folks that being a rock star on your laptop using other shells and extensions that aren’t installed in production isn’t a transferable skill to “cattle not pets” cloud infra. Being a rockstar on your own laptop using exotic shells will wow your friends. Being a rockstar on a cloud vm during an outage using ancient tools and an empty profile will wow your team mates and the CTO.

I posted how to install git in bash inside visual studio code on windows which is how i stay sharp and ready to be a commandline ninja when trapped on a mandatory corporate windows laptop for day to day work.

Collapse
v6 profile image
🦄N B🛡 • Edited

Being a rockstar on a cloud vm during an outage using ancient tools and an empty profile will wow your team mates and the CTO.

I have lived an extreme example of this, when I had to fix a production outage by texting someone Linux commands.

Collapse
dalgibbard profile image
Darren Gibbard • Edited

FYI; I noticed you switch to egrep when splitting multiple matches with pipe; you can escape the pipe and stick with regular grep too:

mycommand | grep "string1\|string2"

Collapse
javinpaul profile image
javinpaul Author

Thx, I'll try thins, somehow I never used grep like that :-)

Collapse
dmorehead profile image
dmorehead

Try tcsh ..used it on everything from nextstep to irix to linux to Darwin today ..has the most creature comforts of any command line shell ..including many if not more of the ones mentioned :)

Collapse
osninja_io profile image
The OpenShift Ninja

I used tcsh back in the day... zsh is better :)

Collapse
forsakenharmony profile image
Leah

Try fish shell and don't worry about 1-4 🙃

Collapse
javinpaul profile image
javinpaul Author

Is that a real shell? never heard about that.

Collapse
bobbyiliev profile image
Bobby Iliev

Great article! 🙌

You should check out this Open-Source Introduction to Bash Scripting Ebook on GitHub as well!

Collapse
theloneking profile image
Dineshkumar Gnanaprakasam

Cool Tips! I especially like tips # 1-3 & 8. Thanks for sharing!