Miscellaneous Basic Commands
Using other commands in the Linux OS
* The cat command is short for concatenate; in its simplest form - 'cat file_name', it displays the entire contents of file_name onto the screen. More than one argument causes cat to display the concatenated or combined contents of all its arguments; the second file is appended to the first file and so on.
[ LinuxUser ] ~$ cat newfile.txt #!/bin/sh # Directories to backup SOURCES="/home /var/mail /etc" # Directory to backup to. # Filesystem Reference to mounted usb drive TARGET="/mnt/usb-harddrive/" [ LinuxUser ] ~$ cat myScript #!/bin/bash echo Current Date: date #!done [ LinuxUser ] ~$ cat newfile.txt myScript #!/bin/sh # Directories to backup SOURCES="/home /var/mail /etc" # Directory to backup to. # Filesystem Reference to mounted usb drive TARGET="/mnt/usb-harddrive/" #!/bin/bash echo Current Date: date #!done [ LinuxUser ] ~$
* The exit command terminates the current shell and is often used to log users out of the system.
* The grep, egrep, and fgrep commands are powerful file filters that may be used to search text files for patterns and return lines that match the specified patterns. We will examine these commands in greater detail later in the chapter.
* The head command displays the first few lines of a text file. By default, the command shows ten lines; a minus option may be used to set the number of lines returned.
[ LinuxUser ] ~$ head -2 newfile.txt #!/bin/sh # Directories to backup [ LinuxUser ] ~$
* The history command returns a numbered list of all commands the user has issued in the current shell.
[ LinuxUser ] ~$ history
1 rmdir Java
2 mkdir Java
3 mv lsinfo.txt Java/
4 find lsinfo.txt Java/
5 cp myScr myfile.txt
6 cp -i myScr xxx.txt
7 pwd
* The less scrolls through a text file one page at a time. In addition to moving forward, the less command allows the user to scroll upwards and navigate through the file.
* The more command allows the user to view one output page at a time and scroll downwards through a file or output.
* The tail command displays the last few lines of a text file. By default, the command returns ten lines; a minus option may be used to set the number of lines returned.
* The tree command follows the syntax 'tree path' and returns a graphical display of the structure of path complete with sub-directories, files, and links.
[ LinuxUser ] ~$ tree /home/NewUser /home/NewUser |--images
|--music | |--favorite.wav | |--pop.wav | `--berlois.wav |--java | |--myProgram.java | |--slideApp.java | `--swingSample.java `--settings 4 directories, 4 files
* The whoami command without any arguments may be used to obtain the name of the current user.
Using Wildcards
A wildcard may be used while performing file and directory operations. The wildcard causes the system to search for all files or directories that match a pattern rather than an exact filename. The most commonly used wildcard is the asterisk (*). Anything (zero or more characters) matches the '*' wild card. For example, '*.txt' will return all txt files while '*.*' will return all files of form filename.extension. Remember that Linux is case sensitive; '*.txt' will not match a file named 'myfile.TXT' as the extension of this file is in uppercase.
Two other commonly used wildcards are '?' (question mark) and '[]' (square brackets). The '?' wildcard stands for exactly one character. Suppose a program outputs up to twenty seven text files named outputA.txt, outputB.txt and so on all the way to outputZ.txt, issuing the command 'ls output?.txt' would display all the files generated by the program. The '?' here would match the characters 'A' through 'Z' - in fact, it would match any single character after the string 'output' and before the string '.txt'.
Another way of issuing the same command would be to specify 'ls output[A-Z].txt'. In this case, [A-Z] matches any of the characters specified explicitly within the square brackets or all characters in the interval A-Z. This is more exact; the '?' wildcard matches any character but [A-Z] only matches uppercase alphabets. So, 'ls output[A-Z].txt' would not return output1.txt, output$.txt and so on unlike 'ls output?.txt'. The following wildcard - [A-Z0-9_%] would match upper case alphabets A to Z, numbers 0-9, an underscore (_), or percentage symbol. If the first character within a pair of square brackets is the '^' (caret) or '!' (exclamation point), the search is inverted. In other words, any character not in the list is matched. For example [^a-z] will match any character other than a lower case alphabet.
The shell's command line interpreter itself translates patterns with wildcards into matching filenames and passes these filenames to the invoked command or program. If no matching filenames are found, the pattern with the wildcard is sent to the invoked program 'as is' (final example in the following table).
Command Translation by the shell
|
Issued by User
|
Interpreted by shell
|
| ls | ls |
| ls a* | ls alluvial.zip alpha.exe amoeba.txt |
| ls a?? | ls all and ans ate |
| ls zygwig* | ls zygwig* |
| Directory contents: all, alluvial.zip, alpha.exe, amoeba.txt, and, ans | |
Note: To find hidden files using wildcards, the pattern should start with a period; 'ls *' will not show hidden files. 'ls .*' will display all hidden files.
Keyboard Options
A Linux user's interaction with the CLI may be greatly simplified through keyboard shortcuts. The following are some commonly used shortcuts:
- The up and down arrow keys bring up previously typed commands.
- Pressing Ctrl-R and typing a sequence of characters will bring up the last command that started with the sequence. For example, holding Ctrl-R and typing 'cd' will bring up the last used 'cd' command.
- The Tab key will complete a partially entered file or directory name if a filename or directory name is typed provided that such a file exists and is the only one that starts with the supplied prefix.