Linux command line tips - gHacks Tech News

Linux command line tips

I thought it would be useful to break away from all the GUI-goodness and offer up a few command line tips and tricks. Why? No matter how powerful, user-friendly, and modern the Linux desktop becomes, there may come a time when you want to step up your game and get down and dirty with the command line interface. When you do, you will want your command-fu to be strong. And whether you are brand new to the command line, or have any level of familiarity with this tool, there are tips and tricks out there to make your life easier. This article should at least offer one or two of those tips...no matter your level of skill.

Tab completion

This tip is for the novice. There are a lot of commands on the Linux operating system (in my /usr/bin directory there is 1736 commands). Hard to remember all of them. Or sometimes a command will have a number of variations (like in the case of beagle) And sometimes, it's just tiring typing all of those commands. Fortunately the Tab key is there to help you out. Open up your terminal window and do the following:

  1. Type beagle
  2. Hit the Tab key twice

You should see a complete listing of all commands that start with beagle. Say you want to run beagle-index-info. You can do this by typing beagle-ind and then hitting the Tab key one time.

Run second command with first commands arguments

Say you need to find out what the directory /home/jlwallen/.e16/themes contains, but when you run the command ls /home/jlwallen/.e16/themes you see that the directory doesn't exist. Looks like you will have to create that directory. Normally you would do this by typing mkdir /home/jlwallen/.e16/themes (or mkdir ~/.e16/themes). You can use a neat little trick to take the arguments from the previous command and add them to a new command like so:

mkdir !*

The above command would be the equivalent of (in our example above) mkdir /home/jlwallen/.e16/themes).

Search your bash history

If you can't remember how you ran a specific command, you can use your bash history to help you. If you hit <Ctrl>r you will be in a special bash search prompt that looks like:

(reverse-i-search)`':

From this prompt you can enter a portion of the command and immediately see a command that contains what you type. If the command is the one you are looking for, hit the Enter key to execute that command.

Another method of searching bash history

This one is simple. If you can't remember the last few commands you entered, just hit the up arrow on your keyboard to look through the list. When you find the command you want to run, hit Enter.

Create recursive directories with one command

Have you ever had to create entire directory trees and did so like:

mkdir ~/test

mkdir ~/test/test1

mkdir ~/test/test1/test2

mkdir ~/test/test1/test2/test3

You create all of these subdirectories with a single command:

mkdir -p ~/test/test1/test2/test3

Run a second command only if first command succeeds

You can string two commands together in such a way that the second of the two commands will work if (and only if) the first command succeeds. Let's say you are going to use the wget command to download a tar file and then you want to immediately unpack that file. You could do so with this:

wget http://ADDRESS_OF_SERVER/filename.tgz && tar xvzf filename.tgz

The above would unpack filename.tgz if (and only if) it is successfully downloaded.

Final thoughts

That should get you going for a bit. I'll revisit this topic in the future so you can keep your command-fu skills sharp. And, by all means, if you have a cool command-line tip you want to share, please do so.






  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Jim said on January 19, 2010 at 4:14 pm
      Reply

      Thanks for the clear explanation. This is great for a linux noob like me.

    2. Dotan Cohen said on January 19, 2010 at 10:35 pm
      Reply

      When using the arguments of the last command, how does “!*” differ from “!!”?

    3. JakeDTH said on January 21, 2010 at 7:16 am
      Reply

      You do know that BASH, the default shell (or “command-line) for Debian (and most of its variants including Ubuntu), Fedora, and many other distributions, is a GNU package, right?

      GNU BASH is not in anyway related to the Linux kernel. It is developed for the GNU operating system. And what you call “Linux” is actually the GNU operating system, the appropriate name for it is GNU/Linux.

      GNU BASH has no strict relation with the Linux kernel or even GNU. It is simply a free software Unix-like shell, for many Unix and Unix-like operating systems, like BSD or Solaris. I believe it’s even used on Mac OS X.

      There are other shells, but I believe all of them are developed simply as free or open source software, not as a “Linux command line.”

      Please give credit where credit is due, these are BASH or DASH shell tips. And most of them can be found with the simple command “man bash”

      1. edo248 said on January 22, 2010 at 6:35 pm
        Reply

        Completely agree with you!

        Thank you for reminding about credits. Some people still don’t want to understand that free and EVEN open-source software is not a public domain. It has it’s authors and if you enjoing using them, moreover without any charge, you MUST give them credits.

        Waiting for authors reply.

    4. JohnnyPerl said on March 24, 2011 at 8:34 pm
      Reply

      Does anyone have a tip for when you are in the middle of typing a long command, and then you decide to abandon that and start with a clear line without executing what you were typing — any easier way to do this than just hold down backspace or up to a recent shorter command (and then hold down backspace)?

      1. Patrik P. said on May 4, 2011 at 7:29 pm
        Reply

        CTRL + U to delete whole line or you might want to use CTRL + W to delete one word in the line.

    Leave a Reply