After completing this section, you should be able to save time running commands from a shell prompt using Bash shortcuts.
The GNU Bourne-Again Shell (bash) is a program that interprets commands typed in by the user. Each string typed into the shell can have up to three parts: the command, options (which usually begin with a - or --), and arguments. Each word typed into the shell is separated from each other with spaces. Commands are the names of programs that are installed on the system. Each command has its own options and arguments.
When you are ready to execute a command, press the Enter key. Type each command on a separate line. The command output is displayed before the next shell prompt appears.
[user@host]$whoamiuser[user@host]$
If you want to type more than one command on a single line, use the semicolon (;) as a command separator. A semicolon is a member of a class of characters called metacharacters that have special meanings for bash. In this case the output of both commands will be displayed before the next shell prompt appears.
The following example shows how to combine two commands (command1 and command2) on the command line.
[user@host]$command1;command2
The date command displays the current date and time. It can also be used by the superuser to set the system clock. An argument that begins with a plus sign (+) specifies a format string for the date command.
[user@host ~]$dateSat Jan 26 08:13:50 IST 2019[user@host ~]$date +%R08:13[user@host ~]$date +%x01/26/2019
The passwd command changes a user's own password. The original password for the account must be specified before a change is allowed. By default, passwd is configured to require a strong password, consisting of lowercase letters, uppercase letters, numbers, and symbols, and is not based on a dictionary word. The superuser can use the passwd command to change other users' passwords.
[user@host ~]$passwdChanging password for user user. Current password:New password:old_passwordRetype new password:new_passwordpasswd: all authentication tokens updated successfully.new_password
Linux does not require file name extensions to classify files by type. The file command scans the beginning of a file's contents and displays what type it is. The files to be classified are passed as arguments to the command.
[user@host ~]$file /etc/passwd/etc/passwd: ASCII text[user@host ~]$file /bin/passwd/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=a3637110e27e9a48dced9f38b4ae43388d32d0e4, stripped[user@host ~]$file /home/home: directory
One of the most simple and frequently used commands in Linux is cat. The cat command allows you to create single or multiple files, view the contents of files, concatenate the contents from multiple files, and redirect contents of the file to a terminal or files.
The example shows how to view the contents of the /etc/passwd file.
[user@host ~]$cat /etc/passwdroot:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ...output omitted...
Use the following command to display the contents of multiple files.
[user@host ~]$cat file1 file2Hello World!! Introduction to Linux commands.
Some files are very long and can take up more room to display than that provided by the terminal. The cat command does not display the contents of a file as pages. The less command displays one page of a file at a time and lets you scroll at your leisure.
The less command allows you to page forward and backward through files that are longer than can fit on one terminal window. Use the UpArrow key and the DownArrow key to scroll up and down. Press q to exit the command.
The head and tail commands display the beginning and end of a file, respectively.
By default these commands display 10 lines of the file, but they both have a -n option that allows a different number of lines to be specified.
The file to display is passed as an argument to these commands.
[user@host ~]$head /etc/passwdroot:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin[user@host ~]$tail -n 3 /etc/passwdgdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:977:977::/run/gnome-initial-setup/:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
The wc command counts lines, words, and characters in a file.
It takes a -l, -w, or -c option to display only the number of lines, words, or characters, respectively.
[user@host ~]$wc /etc/passwd45 102 2480 /etc/passwd[user@host ~]$wc -l /etc/passwd ; wc -l /etc/group45 /etc/passwd 70 /etc/group[user@host ~]$wc -c /etc/group /etc/hosts966 /etc/group 516 /etc/hosts 1482 total
Tab completion allows a user to quickly complete commands or file names after they have typed enough at the prompt to make it unique. If the characters typed are not unique, pressing the Tab key twice displays all commands that begin with the characters already typed.
[user@host ~]$paspasswd paste pasuspenderTab+Tab
[user@host ~]$passTab
[user@host ~]$passwdChanging password for user user. Current password:
Tab completion can be used to complete file names when typing them as arguments to commands. When Tab is pressed, it completes as much of the file name as possible. Pressing Tab a second time causes the shell to list all of the files that are matched by the current pattern. Type additional characters until the name is unique, then use tab completion to complete the command.
[user@host ~]$ls /etc/pasTab
[user@host ~]$ls /etc/passwdpasswd passwd-Tab
Arguments and options can be matched with tab completion for many commands.
The useradd command is used by the superuser, root, to create additional users on the system.
It has many options that can be used to control how that command behaves.
Tab completion following a partial option can be used to complete the option without a lot of typing.
[root@host ~]#useradd ----base-dir --groups --no-log-init --shell --comment --help --non-unique --skel --create-home --home-dir --no-user-group --system --defaults --inactive --password --uid --expiredate --key --root --user-group --gid --no-create-home --selinux-userTab+Tab
[root@host ~]#useradd --
Commands with many options and arguments can quickly become long and are automatically wrapped by the command window when the the cursor reaches the right margin. Instead, to make command readability easier, you can type a long command using more than one line.
To do this, you will use a backslash character (\), referred to as the escape character, to ignore the meaning of the character immediately following the backslash. You have learned that entering a newline character, by pressing the Enter key, tells the shell that command entry is complete and to execute the command. By escaping the newline character, the shell is told to move to a new command line without performing command execution. The shell acknowledges the request by displaying a continuation prompt, referred to as the secondary prompt, using the greater-than character (>) by default, on an empty new line. Commands may be continued over many lines.
[user@host]$head -n 3 \>/usr/share/dict/words \>/usr/share/dict/linux.words==> /usr/share/dict/words <== 1080 10-point 10th ==> /usr/share/dict/linux.words <== 1080 10-point 10th[user@host ~]$
The previous screen example displays how a continued command appears to a typical user. However, protraying this realism in learning materials, such as this coursebook, commonly causes confusion. New learners might mistakenly insert the extra greater-than character as part of the typed command. The shell interprets a typed greater-than character as process redirection, which the user did not intend. Process redirection is discussed in an upcoming chapter.
To avoid this confusion, this coursebook will not show secondary prompts in screen outputs. A user still sees the secondary prompt in their shell window, but the course material intentionally displays only characters to be typed, as demonstrated in the example below. Compare with the previous screen example.
[user@host]$head -n 3 \/usr/share/dict/words \/usr/share/dict/linux.words==> /usr/share/dict/words <== 1080 10-point 10th ==> /usr/share/dict/linux.words <== 1080 10-point 10th[user@host ~]$
The history command displays a list of previously executed commands prefixed with a command number.
The exclamation point character (!) is a metacharacter that is used to expand previous commands without having to retype them.
The !number command expands to the command matching the number specified.
The !string command expands to the most recent command that begins with the string specified.
[user@host ~]$history...output omitted... 23 clear 24 who 25 pwd 26 ls /etc 27 uptime 28 ls -l 29 date 30 history[user@host ~]$!lsls -l total 0 drwxr-xr-x. 2 user user 6 Mar 29 21:16 Desktop ...output omitted...[user@host ~]$!26ls /etc abrt hosts pulse adjtime hosts.allow purple aliases hosts.deny qemu-ga ...output omitted...
The arrow keys can be used to navigate through previous commands in the shell's history. UpArrow edits the previous command in the history list. DownArrow edits the next command in the history list. LeftArrow and RightArrow move the cursor left and right in the current command from the history list, so that you can edit it before running it.
You can use either the Esc+. or Alt+. key combination to insert the last word of the previous command at the cursor's current location. Repeated use of the key combination will replace that text with the last word of even earlier commands in the history. The Alt+. key combination is particularly convenient because you can hold down Alt and press . repeatedly to easily go through earlier and earlier commands.
When used interactively, bash has a command-line editing feature. This allows the user to use text editor commands to move around within and modify the current command being typed. Using the arrow keys to move within the current command and to step through the command history was introduced earlier in this session. More powerful editing commands are introduced in the following table.
Table 2.1. Useful Command-line Editing Shortcuts
| Shortcut | Description |
|---|---|
| Ctrl+A | Jump to the beginning of the command line. |
| Ctrl+E | Jump to the end of the command line. |
| Ctrl+U | Clear from the cursor to the beginning of the command line. |
| Ctrl+K | Clear from the cursor to the end of the command line. |
| Ctrl+LeftArrow | Jump to the beginning of the previous word on the command line. |
| Ctrl+RightArrow | Jump to the end of the next word on the command line. |
| Ctrl+R | Search the history list of commands for a pattern. |
There are several other command-line editing commands available, but these are the most useful commands for new users. The other commands can be found in the bash(1) man page.
bash(1), date(1), file(1), cat(1), more(1), less(1), head(1), passwd(1), tail(1), and wc(1) man pages