Section 2.7. I Can t Copy from the Command Line

2.7. I Can't Copy from the Command Line

Long commands can be annoying. It's easy to forget the full directory path to desired commands. It's easy to forget which switches you used, and the sequence of switches and other arguments you need to use.

I address this annoyance in three ways. First, I show how you can use the history of previous commands to help you run a long command. Second, I show you how you can add a directory to your PATH, which can minimize the length of more common commands. Finally, I show you how you can configure a long command in a dedicated script that's easier to run from the command line. It helps if you add these scripts to directories in your PATH.

As this annoyance is related to the command-line interface, the annoyance is described only from the point of view of a Linux administrator. Any Linux user who is comfortable with the command-line interface can also use the lessons from this annoyance.

2.7.1. Using Your History

When you run a long command for the first time, it takes work. You may have read through the associated manpages to find the right arguments. You may have searched through your system to find the directory with the command you need.

You may not run a command like the following very often:

 /home/michael/Desktop/scripts/dbmanage -opcg /home/michael/Desktop/process/logwriter 

If you have to rerun the command again, you may not remember the function of the -o, -p, -c, and -g switches. You might not remember the directory with the hypothetical dbmanage command or the logwriter file.

If you've run a command before, the easiest way to run it again is to use your history of previous commands. By default, the last 1,000 commands are available in the ~/.bash_history file. The number of commands in this file is associated with the HISTSIZE directive in /etc/profile.

You can scroll through the list of previous commands with the up and down arrows on the keyboard. With the Page Up and Page Down keys, you can move to the top and bottom of the history. Once you find the command you need, just press Enter.

If you've run the desired command on another system, you can use the middle mouse button to copy the command (see the next section).

But scrolling through 1,000 commands can be a drag. As with any other Linux command, you can use grep to search through the output. For example, to find the find commands in your history, use the following command:

 history | grep find 

As a Linux geek, you probably use the find command often, so you may see a number of lines such as:

 387  find | xargs grep -3 -s 'bashrc' | more 526  find . -atime +7 -name users -print | exec grep -l linux {} 

Notice how each command is associated with a number, which represents the sequence in your command history. For example, if you want to run command number 387 again on the local computer, you don't have to retype the command; just "bang" out the number:


The "bang" in Linux is an exclamation point. In this case, it allows you to specify the command number from your history that you want to run. (Within many commands, it allows you to specify "anything but" the term you use.) In other words, you can tell your friends that "bang" is actually an important technical term.

If you don't remember the number, bang out the first few letters. For example, if you've run a complex iptables command recently and need to run it again, enter the following at your shell prompt:


But be careful; if there is more than one iptables command in your history, you may need more information, as !iptables runs the most recent iptables command in your history.

2.7.2. Copying with the Middle Mouse Button

It's often not enough to search through the history of previous commands. You may want to apply the command of your choice on different computers. If you create an encrypted password at the command line, you may want to transfer it to a configuration file. These situations are when the middle mouse button can be most helpful.

If you already have a middle mouse button, great! If you see only two buttons, you should still be able to access the middle button's functionality. If you have a scroll wheel, press on it. If it clicks, it may already be configured as a middle mouse button. If your mouse has only two buttons, and no scroll wheel, click on both together. Linux often configures this action as the click of a middle mouse button.

If your middle mouse button doesn't work using the instructions described here, see "My Mouse Doesn't Do What I Want" in Chapter 1.

Now to see if the middle mouse button works for copying commands, take the following steps:

  1. Open a command-line console in the GUI or from a command-line interface.

    If your mouse is properly enabled, you should be able to left-click, drag, and highlight the text of your choice in the console. If you're in the text console and your mouse doesn't work, try it in a GUI-based console, such as konsole, gnome-terminal, or xterm.

  2. Run a command, or find one in the output of the history command.

  3. Highlight the command.

  4. With the text still highlighted, open another text console.

  5. Click the middle mouse button, or use one of the related techniques described earlier in this section. If your middle mouse button is properly enabled, the command you highlighted in the first console will be pasted into the second.

The middle mouse button is also good for other purposes, such as transferring encrypted passwords. For example, to add a password to the GRUB configuration file, the best course is to use GRUB's own command for creating an encrypted password, grub-md5-crypt, and then to paste it into the GRUB configuration file in a text editor.

For example, if you want to add an encrypted password to the /boot/grub/menu.lst configuration file, take the following steps:

  1. Run the grub-md5-crypt command. Its location varies depending on your distribution.

  2. When prompted, enter the password of your choice and confirm.

  3. You'll see an encrypted password in the output, such as:


    You certainly wouldn't want to have to retype this password. Any error in retyping the 32 characters in this encrypted password would change the password.

  4. Highlight the encrypted password.

  5. Open another console, and open the /boot/grub/menu.lst configuration file in a text editor.

  6. Insert the following command before the stanzas associated with individual boot options:

     password --md5 

  7. Stay in insert mode. Add a space after the command. Click the middle mouse button. This action should insert the encrypted password into the GRUB configuration file.

  8. Save and close the GRUB configuration file. The next time you boot using GRUB, you'll notice that a password is required before you can modify GRUB, such as if you want to boot into Single User Mode.

2.7.3. Adding a Directory to Your Path

If you haven't run a complex command before, you can still create a shortcut by taking advantage of your PATH . While the command completion feature associated with the bash shell can help you complete long paths to a specific command, it's still easier if you add directories with key scripts and commands to your PATHor modify your PATH to reflect the scripts you need. Defining your current PATH

But first, you need to figure out the directories in your current PATH. Then you can see what directories you need to add. To find the directories in your PATH, run the following command:

 echo $PATH 

The output includes the directories in your PATH. Linux distributions define different values for PATH. If you work with different distributions, the variations can be annoying. For example, my systems have the following values for PATH:





Red Hat Enterprise Linux


Fedora Linux


If you've installed software from source code on downloaded tarballs, key files may get unpacked to the same directory on all distributions. You may want to make sure that directory is copied to your PATH on your systems.

When you run a command, your shell searches files in your PATH from left to right. In other words, if there were an ls command in the /usr/local/bin directory, Linux would run that command instead of the standard /bin/ls.

In fact, the PATH is one way a cracker can wreck havoc. For example, if a cracker found a way to modify the su command to send the root password to another computer, he could break right into your system with administrative privileges. All he would need to do is copy this command to a higher-level directory in your PATH, such as /usr/local/bin. Adding to your PATH

If you're going to run a number of commands from a directory not in your PATH, you should add it. If you want to add it for all users, modify your /etc/profile. The best way to modify this file varies by distribution.

Some distributions, including SUSE Linux, suggest that you use another file, such as /etc/profile.local, for edits you make from the command-line interface. Otherwise, be sure to back up any key configuration file before editing it.

SUSE and Debian specify the directories associated with the PATH directive separately for the root and for other users. In their versions of /etc/profile, the root user's PATH is associated with User ID 0. For example, in SUSE Linux, the following directive in /etc/profile specifies directories for User ID 0:

 test "$UID" = 0 && PATH=/sbin:/usr/sbin:/usr/local/sbin:$PATH 

But the objective here is to add the ~/sbin directory to the PATH for regular users. If you're running SUSE Linux, add the following directive to the /etc/profile.local configuration file:


If you're running Debian Linux, edit the following stanza in /etc/profile:

 if [ "`id -u`" -eq 0 ]; then   PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11" else   PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games" fi 

The conditional specifies a PATH for the root user; to modify the PATH for all regular users, you would modify the directive associated with ELSE. Therefore, to add the ~/sbin directory to the path for all regular users, you would modify this directive to read:


Red Hat and Fedora Linux configure a pathmunge directive in their versions of /etc/profile, which adds the noted directories one by one to the default PATH for the root user, in the following stanza:

 if [ `id -u` = 0 ]; then         pathmunge /sbin         pathmunge /usr/sbin         pathmunge /usr/local/sbin fi 

You can add the directories of your choice to this stanza. For example, if you want to add the $HOME/sbin directory for regular users, add the following directives in that stanza:

                      else         pathmunge $HOME/sbin 

The next time you log in to one of these distributions as a regular user, run the echo $PATH command again. You'll see the desired directory in your PATH.

Linux Annoyances for Geeks
Linux Annoyances for Geeks: Getting the Most Flexible System in the World Just the Way You Want It
ISBN: 0596008015
EAN: 2147483647
Year: 2004
Pages: 144
Authors: Michael Jang

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: