Pinterest Stumbleupon Whatsapp
Ads by Google

The terminal can be an intimidating place, but there’s a good chance you will open one during your Linux experience. If you spend quite a bit of time there, you may find that it’s actually not so bad. I’ve come to prefer using the terminal for some tasks, such as installing and updating software How to Overcome Problems with the Ubuntu Update Manager How to Overcome Problems with the Ubuntu Update Manager Virtually every user has run into problems with the Ubuntu Update Manager, either with missing repositories or due to unusual errors indicating Internet connectivity issues. We'll show you how they can be addressed. Read More .

Whether you’re a terminal newbie or someone who got over the fear long ago, there are ways you can make things go more smoothly. Here are 20 shortcuts that can help you overcome some of the command line’s awkwardness.

LinuxTerminalShortcuts-Echo

Moving the Cursor

At first, even moving around the terminal seems more difficult. Fortunately, it’s not harder, just different. Remove your hand from your touchpad, take a deep breath, and smile. The keyboard is about to become your best friend.

1. Jump Between Parts of a Line

Use Ctrl + Left and Ctrl + Right to move between the various parts of line. Let’s say you’ve typed out the following line.

dnf install kde-desktop-environment

Tapping Ctrl + Left moves you to the beginning of environment, desktop, kde, install, and dnf, in that order.

Ads by Google

2. Hop to the Beginning or End of a Line

Home or Ctrl + A move the cursor to the beginning of the line. End or Ctrl + E moves you back to the end.

Which keys you use depends on your keyboard layout. Not all PCs have Home and End keys. MacBooks don’t. Either way, Ctrl + A/E should work.

3. Clear Parts, or All, of a Line

Ctrl + K takes everything from your current position to the end of the line and gives it the boot. Ctrl + W goes after only the word immediately before the cursor. Ctrl + U erases from the beginning of the line up to the current position of the cursor

4. Autocomplete a Command or Directory

Pressing tab completes the name of the current command or directory.

Let’s imagine you’re navigating to your downloads folder using this line:

cd /home/user/Downloads

You can hit tab once you’re at cd /home/user/Dow to automatically finish the word.

Say you want to install a bunch of apps using apt-get install. You can type apt-get ins and hit tab.

When the terminal can’t predict what you’re trying to say, it typically lets you know with a beep.

Recycling

Those of us who grew up in the era of Windows and Mac have grown accustomed to clicking on icons to get stuff done. There’s none of that in the terminal. This means we need to type out every command. Fortunately, we have ways to avoid re-typing things over and over and over again.

5. Switch Between Recently Used Lines

Press the Up arrow to get to the last command you used. Tap it again to retrieve the one before that. Press the Down arrow if you’ve gone too far.

The terminal saves your history even when you close the window, so you can do this to recall commands you used days ago.

6. Copy the Previous Line

Let’s say you’re looking to repeat a command you just issued, but with root privileges. Chances are you forgot to begin the line with sudo (more on this later). In that case, rather than retyping the entire command, you can simply enter:

sudo !!

The double exclamation points tell the terminal that you want to re-enter the previous line.

7. Copy the Previous Line Containing a Specific Command

What if the command you want to repeat isn’t the last one you used? The command illustrated above, !!, won’t work anymore. Instead, you’ll want to use a single exclamation point combined with the beginning of the command you wish to retrieve.

For example, you could use !apt-get, !dnf, or !pacman to try checking for updates again. The technique also works with cd, man, and other essential commands An A-Z of Linux - 40 Essential Commands You Should Know An A-Z of Linux - 40 Essential Commands You Should Know Linux is the oft-ignored third wheel to Windows and Mac. Yes, over the past decade, the open source operating system has gained a lot of traction, but it’s still a far cry from being considered... Read More .

LinuxTerminalShortcuts-Repeat-Command

8. Copy the Previous Argument

After copying and pasting a folder to a new location, you may want to start doing other things in that target directory. Try this:

cd !$

The !$ indicates the last argument used. Keep in mind, arguments consist of any information you give a command to complete a task, so directories are hardly the only things you can recycle using !$.

9. Fix Typos

Sometimes typos happen. In an article, they can lead to embarrassment. In the terminal, they stop commands from working. Fortunately, there’s an easy fix.

Let’s say you want to look up all the things you can do with Fedora’s package manager, DNF. To do so, you fire up your terminal and type:

man dfn

Well, that isn’t right. The terminal’s telling you that there is “No manual entry for dfn.”

You know this. To fix the issue, you type:

^dfn^dnf

When you hit enter, the terminal will repeat the command using the new spelling.

Navigating Directories

Launching applications is hardly the only thing you do on your computer, and the same is true once you dive into the terminal. Sometimes you simply have to move files around and dig through folders.

This comes with a learning curve, but with these basics memorized, you will get the hang of it in no time.

10. Navigate to a Specific File or Folder

As I was saying, manipulating files can be a real chore. You have to remember a file’s location and know how to type  out the path explicitly. Who does that?

An easier approach is to drag a file or folder into the terminal. This will append the path to the end of your current command.

LinuxTerminalShortcuts-Drag-Drop

11. Navigate Between Folders

Once you’ve already started managing folders inside the terminal, dragging and dropping can start to feel like, well, a drag. At that point, these two dots will become your best friend. When you’re using cd to move around, follow that command with ..

No, that’s not an unfinished ellipses. Here, let’s try this.

cd ..

cd followed by two periods will move you into the parent directory.

To switch between folders within the parent directory, use cd .. followed by the name. Pretend you’re managing files in your /Music/Black_Eyed_Peas folder, you finish the job, and now you want to hop over to /Music/Lindsey_Stirling. Simply enter:

cd ../Lindsey_Stirling

12. Return to the Previous Directory

Halfway through editing files in /Lindsey_Stirling, you realize you forgot to remove one of the unnecessary album art images cluttering up your /Black_Eyed_Peas folder. To get back instantly, use a dash. As in –

cd -

13. Return to the Home Directory

When you’re ready to go home, so to speak, just enter cd. That’s it.

14. See Your Current Directory

Okay, you’ve been navigating for a while, and you no longer know where you are. It happens. Enter pwd to see the path to your location.

15. Go to the Root Directory

Oh, so you’re legit. If you’re looking to navigate straight to your root directory and start cd-ing around in the terminal, you clearly know your way around Linux. To get straight to the meat and potatoes of your operating system, use:

cd /

16. Keep Root Access

Yes, I could have placed this one anywhere on the list, but to manipulate any of the files in the root directory, you need administrator access. The best way to do this can vary depending on your distribution. One option is to place sudo at the beginning of your command.

Another approach, assuming you have the root password, is to use su instead. This will sign you in as the administrator and let you perform any changes you wish without worrying about re-entering your password. It can save time if you plan on making a bunch of modifications at the root level. But don’t do this if you’re even the slightest bit unsure of what you’re doing What Is SU & Why Is It Important to Using Linux Effectively? What Is SU & Why Is It Important to Using Linux Effectively? The Linux SU or root user account is a powerful tool that can be helpful when used correctly or devastating if used recklessly. Let's look at why you should be responsible when using SU. Read More .

Multitasking

You’ve learned the basics. Now, are you ready to take your terminal-fu to the next level?

17. Run Multiple Commands

To do this, you’re going to need a couple ampersands. You run a command, enter &&, and follow up with a second task that will only begin if the first one doesn’t fail. Here’s an (admittedly silly) example.

echo "Where's Lindsey Stirling?" && echo "Here she is!" && cd /home/user/Music/Lindsey_Stirling

The terminal will display the text “Where’s Lindsey Stirling?” Then it will then say “Here she is!” before taking you to the appropriate music folder.

You can think of && as and. As in, you’re telling the terminal to do this and this and this.

18. Run a Command with a Backup Plan

The double ampersands signal to run the subsequent command only if the previous one succeeded. Maybe you want the opposite, to run a command only if the first one fails. In that case, swap && for ||.

19. Run a Command in the Background

Place a single & at the end of a line. The terminal will show you a process number and then, so long as nothing went wrong, return back to normal as though nothing were going on.

LinuxTerminalShortcuts-Command-Background

To see which processes are currently running, enter the jobs command. You can use kill followed by the process number provided to force a task to stop running. Alternatively, you can bring the operation back to the foreground using fg.

20. Run Multiple Commands in the Background

As you might expect, the & does this too. After you end your first command with an &, follow up with another one.

rmdir /home/user/Public/ & killall chromium-browser & sudo apt-get update &

All of these tasks will run in the background. Each will activate regardless of whether the others succeed or fail.

Are You Ready for the Terminal?

By the time you’ve worked through all of these shortcuts, that mysterious black window with white text should start feeling familiar. You might even find yourself keeping a terminal window open somewhere on your desktop at all times.

In that case, it’s time to get acquainted with the fun stuff 9 Quirky Linux Commands You Need to Know (And Will Love) 9 Quirky Linux Commands You Need to Know (And Will Love) Make ASCII art, talk to your computer and play text adventures. Your Linux command line isn't just for work: it can be weirdly entertaining, if you know the right commands. Read More . If you’re feeling confident, you can advance to managing partitions Take Control of Linux Disk Partitions with These 10 fdisk Commands Take Control of Linux Disk Partitions with These 10 fdisk Commands While desktop disk management tools are also available for Linux distros (such as the Disk Utility in Ubuntu), using the fdisk command line tool offers more options. Read More ? Alternative, you may want to spend more time learning the basics A Quick Guide To Get Started With The Linux Command Line A Quick Guide To Get Started With The Linux Command Line You can do lots of amazing stuff with commands in Linux and it's really not difficult to learn. Read More . Whatever you do, there are certain commands you should never run 9 Lethal Linux Commands You Should Never Run 9 Lethal Linux Commands You Should Never Run You should never run a Linux command unless you know exactly what it does. Here are some of the deadliest Linux commands that you'll, for the most part, want to avoid. Read More .

What are your favorite terminal shortcuts? Are there any commands you would recommend learners commit to memory? Share your knowledge in the comments!

  1. basheer
    April 2, 2016 at 12:39 pm

    or switch to oh-my-zsh

  2. jaeger1957
    April 2, 2016 at 6:26 am

    For running multiple commands sequentially without any conditions, use a semicolon to separate them. They will be executed in sequence regardless of the success of the preceding commands.

    • Bertel King, Jr.
      April 2, 2016 at 12:06 pm

      Syntax can be a wonderful thing.

  3. jaeger1957
    April 2, 2016 at 6:24 am

    Also, for #19, the 'jobs' command will show you the job number, not the process ID, so you don't want to kill using that number, as it will kill the wrong process. You should use 'kill %' to kill the job.

  4. jaeger1957
    April 2, 2016 at 6:21 am

    Instead of using 'su' to gain continued root privilege, which doesn't work for most distributions, and for which you'd need the root password, instead, use 'sudo bash' which will give you a root shell until you exit it.

    • Bertel King, Jr.
      April 2, 2016 at 12:03 pm

      Good advice! I'm a Fedora guy, so I'm accustomed to using 'su.' I will keep this in mind for future recommendations.

  5. rhino
    March 31, 2016 at 9:40 am

    Better still - convert BASH to vi mode and you're set !
    Proper cursor movement.
    ESC and v will then let you edit the line in your configured editor !

    • jaeger1957
      April 2, 2016 at 6:31 am

      Agreed! I only use vi mode for my command line. Simply issue the 'set -o vi' command and from that point you can use vi keyboard sequences to recall previous commands, move and edit within the line, and even to search for previous commands. I haven't found whether there's a command completion keystroke, but other than that, I don't find any reason to use the regular mode.

  6. Sudeepto Dutta
    March 30, 2016 at 5:08 pm

    Very nice article Bertel. I knew most of them, but 7, 8 and 9 were new to me.

    You can also add the "reverse-i-search" command, and how to cycle through them. It helped me lot for finding recently executed commands.

  7. bergercookie
    March 30, 2016 at 9:00 am

    Ctrl-U clears *from the current cursor position* to the beginning of the line (not the entire line necessarily).

    • Bertel King, Jr.
      March 30, 2016 at 9:10 pm

      Thanks. We've made the change. Sorry about that.

  8. Colin
    March 30, 2016 at 12:41 am

    I enjoy using the fish shell instead of bash. It auto completes commands for you.

  9. Shikhanshu Agarwal
    March 29, 2016 at 7:59 pm

    These tips are pretty awesome! Thank you for sharing!

    • Bertel King, Jr.
      March 29, 2016 at 11:49 pm

      Glad you can make use of them!

Leave a Reply

Your email address will not be published. Required fields are marked *