2.7. I Can't Copy from the Command LineLong 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.
2.7.1. Using Your HistoryWhen 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.
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: !387
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: !iptables 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 ButtonIt'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.
Now to see if the middle mouse button works for copying commands, take the following steps:
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:
2.7.3. Adding a Directory to Your PathIf 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. 2.7.3.1. Defining your current PATHBut 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:
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.
2.7.3.2. Adding to your PATHIf 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.
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: PATH=$PATH:$HOME/sbin 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: PATH="/usr/local/bin:$HOME/sbin:/usr/bin:/bin:/usr/bin/X11:/usr/games" 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. |