DEV Community

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

Posted on • Updated on

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

Disclosure: This post includes affiliate links; I may receive compensation if you purchase products or services from the different links provided in this article.

Have you ever been amazed to see someone working very fast in UNIX terminal, firing commands, and doing things quickly?

Yes, I have seen that a couple of times, and It has always inspired me inspired to learn from those superstar developers.

In this article, or tutorial, or whatever you call it, I have shared some UNIX command practices I follow to work fast, quick, productive, or efficiently in Linux.

I work for Financial services industry, and my work involves development and support of online stock and futures trading application in Electronic trading, Derivatives, FX, Commodity, and other asset classes.

All our services run on Linux servers so it's essential for us to work efficiently and quickly in a Linux terminal and that's how I have learned these productivity tips in Linux.

This article is like my earlier articles about Top 10 basic networking Commands in UNIX and How does nslookup command work in Linux.

If you have not read those, you can see if you find them interesting and useful. In this UNIX command tutorial, I am going to share my experience on how to work quick, fast, and efficiently in UNIX.

Top 10 UNIX and Linux Productivity Tips

If your server also resides in a Linux machine and your day 2-day work involves a lot of searching and playing around UNIX commands then these tips will save a lot of your time.

Below tips is the result of my years of experience in UNIX terminal which I have summarized as 10 tips to work fast in UNIX.

Why I am sharing this?

Well, I love these kinds of productivity tips, which is easy to learn and remember but makes a huge difference in your day-to-day work. By sharing this, I am looking forward is to get some more tips from you guys to enhance my arsenal so please share how you work in UNIX, how you make most of powerful Linux commands and shell utilities provided by Linux and other UNIX operating systems like Fedora, Ubuntu, CentOS, etc.

Please share your experience by posting comments to make this post useful and get most of it and benefit from each other's expertise.

Anyway, let's start with these useful tips:

1) Use !{command} for executing the previous instance of command quickly

This has saved me 30% time on average. It always happens that you fire same UNIX command multiple times within a fraction of seconds,

before knowing this trick I used to use up and down arrow for finding my power and then executing them which takes a lot of my time, but, after knowing this trick, I just have to just remember the command name like !ls will execute your last "ls -lrt" , !vim will open your final file without typing full command.

Use this tip and experience it, It definitely saves loads of time, and it's also useful on shell other than bash shell (like csh or ksh) where up and down arrow generally doesn't give you previous commands.

For example, After doing ls -l stocks.txt if you want to open stocks.txt, you can use the vim editor as vi !$ (last argument).

2) Use !! for Executing the Last Command

This is the extension of the previous tip, which is used to execute the very last command you have completed. Since it just involves two keystrokes and that too for the same key, it's amazingly fast.

This will also work on the shells in which up and down arrow doesn't work like K shell and C shell. This is extremely useful if you are stopping or starting your server or Java application for debugging ging purpose frequently.

Btw, if you are not familiar with bash shell yet, I suggest you take a look at Bash Shell Scripting: Crash Course For Beginners, which will teach you the bash shell from the command line to shell script.

A very useful course for someone who wants to become a power user in Linux.

3) Use "CTRL+R" for Repeating the Last Matching Command

Best out of the lot if you remember your last command executed sometime back and just want to find that command with the same argument and execute.

This is the tip you need to remember.

Just press the "CRTL+R" and type words that you had in your last command and UNIX will find that command for you then just press enter.

All the above three tips will save a lot of your time if you execute commands frequently, and percentage of repetition is quite high. for me

I have saved almost 50--60% time by following the above three tips. Let me know how it works for you guys.

4) Using history Command to Get Some of the Most Frequently Used UNIX Command

Well, this was the first tip I learned when I started working on UNIX. This is your most helpful command in UNIX and Shell scripting.

In most of the cases, there is a certain command like starting, stopping, checking log files, making a build or doing release, etc.

These are the commands you often need to execute and if you don't remember exact command no need to worry, just do history | grep "keyword" and you will get that command from the history on your Linux machine.

There are certain environment variable, e.g. HISTSIZE which defines how many command UNIX history can store, so have it big

Enough to save your time and avoid referencing your command booklet every now and then.

Btw, if you are not familiar with basic Linux commands, then I suggest you go through Shell Scripting: Discover How to Automate Command Line Tasks to get yourself familiar with commands like this.

It will save you tons of your time by avoiding Google every now and then to search appropriate Linux commands for the task at hand.

5) Using Regular Expression in grep and find

The grep and find are the two best tools UNIX provide to us. Almost everybody needs to search something in UNIX, e.g. a file, a directory, certain words in a file, e.g. ERROR or Exception, and if you know how to use the grep and find with the regular expression you will save a lot of your time by typing fewer commands.

For example, by knowing about egrep you can fire egrep "ERROR|Exception" *.xml instead of firing two grep command for finding ERROR and Exception individually.

If you are interested in learning more about grep and find command, then you should check out my earlier articles 10 examples of grep command and 10 examples of find commands in Linux.

I have shared a lot of useful options for these two commands on those articles, which will help you to get more from these two powerful Linux commands.

best Linux tips

6) Using pipe Instead of firing Two Commands

As shown above this nice little tip I guess everybody knows.

If you don't, it's better to start with a nice Linux course like Linux Command Line Basics, as you might not know some other fundamentals as well.

From my personal experience, joining a nice course is better than finding essential information in bits and pieces.

And, if you don't mind learning from FREE resources, you can also check out my list of 5 FREE Linux courses for developers.

7) Using Aliases and Defining Them in Bash Profile or bashrc File

Have you seen some strange commands working in someone's machine and not yours, which might be aliased he would have set up in either his .bashrc or .profile file?

Always do such kind of setup for commonly used command. There are lots of usage of the .bashrc and .profile file, but one of the most important ones is setting up aliases, e.g., "l." which finds all hidden files. "ls" which includes all useful option, e.g. -lrtH to show all relevant information.

You can further see Linux Command Line Interface (CLI) Fundamentalsto learn more about how login works in Linux and what is the role of .login, .profile and .bashrc files in Linux and bash shell.

Btw, you would need a Pluralsight membership to access this course, which costs around $29 monthly or $299 annually (14% saving). I have one, and I also suggest all developers have that plan because Pluralsight is like NetFlix for Software developers.

It has more than 5000+ good quality courses on all the latest topics. Since we programmers have to learn new things every day, an investment of $299 USD is not bad at all.

Btw, it also offers a 10-day free trial without any obligation which allows you to watch 200 minutes of content. You can watch this course for free by signing for that trial.

8) Using pushd, popd, cd -, ~ to Move Across a Directory

Based on my experience, navigation in the UNIX shell takes almost 50% times of people, and if you are going to write a directory path every now and then just forget about working fast.

So, instead of typing full name use all the above tips and make the best use of pushd, popd, cd --- and cd ~ command. cd --- is best if your switching between two directory location in UNIX.

9) Minimize the Keystrokes or Increase the Speed of Typing

The less you type, the faster you work.

To make use of your last typed command, make use of tab in bash, so that let the UNIX bash shell complete your command.

Use Ctrl+R if the last command you have typed is very long and you want to change just a few lines.

10) Keep Learning New Linux Commands

Try to learn more commands and their options and use this will reduce thinking the time for a particular task and use ctrl+z and fg and bg to suspend a process.

It saves almost 10% time if you are viewing multiple files or log files so instead of every now and then executing vim commands just do Ctrl+Z to suspend it and fg 1 or fg 2 to bring it in the foreground.

I hope these examples, tips on UNIX command will help you to do more in less time and enhance your productivity and experience while working in UNIX.

And if you are looking for some resources then you can check out Learn Linux in 5 Days and Level Up Your Career

This list is by no means complete so please share how you are working in UNIX and of course how fast are you working in UNIX?

Further Learning
5 Free Linux Courses for Programmers
Linux Command Line Interface (CLI) Fundamentals
How Linux Works: What Every Superuser Should Know
10 examples of curl command in Linux
Top 10 Courses to learn Linux for Beginners
My favorite courses to learn AWS
10 examples of lsof command in Linux
Top 10 Python Courses to learn Coding
Learn Linux in 5 Days and Level Up Your Career
Shell Scripting: Discover How to Automate Command Line Tasks
Linux Administration Bootcamp: Go from Beginner to Advanced

Closing Notes

Thanks for reading this article so far. You might be thinking that these are very stuff and there is no point learning them, but you will be amazed how much time you will save by applying these tips in your work.

There is a good chance that you may already know most of the stuff, and there are also a lot of useful free resources which you can use, I have also linked them here and there along with best resources, which are certainly not free, but totally worth of money.

I particularly like Udemy courses as they are very affordable and provide a lot of values in very small amount, but you are free to choose the course you want.

At the end of the day, you should have enough knowledge and experience about the essential Linux commands and bash tricks.

Good luck with your Linux journey! It's certainly not going to be easy, but by following these tips, you will save a lot of time and become the Linux superuser you always wanted to be.

If you like this article, then please consider following me on twitter (javinpaul if you'd like to be notified for every new post and don't forget to followjavarevisited on Twitter!

P.S. --- If you just want to start with one course to learn Linux, I think the Linux Command Line Basics is the best one to start with.

Top comments (42)

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 Kanoldt

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

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
 
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

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

@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
 
javinpaul profile image
javinpaul

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
 
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
 
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

@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
 
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
 
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
 
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

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

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
 
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.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.