UNIX In-Class Tutorial Three 3.1 Wildcards The characters ls

advertisement
ITSC 1358 – UNIX System Administration
Prof. Michael P. Harris
_____________________
Date: _____________________
Name:
UNIX In-Class Tutorial Three
3.1 Wildcards
The characters * and ?
The character * (star) is called a wildcard, and will match against none or more character(s)
in a file (or directory) name. For example, in your unixstuff directory, type:
$ ls back*
This will list all files in the current directory starting with back...
Try typing:
$ ls -a *ile
This will list all files in the current directory ending with ...ile
The wildcard character ? will match exactly one character. So… ls ?ouse will match
files like house and mouse, but not grouse.
Try typing:
$ ls ?agic
The ‘choose from a list’ wildcard characters are [ ]. They will match any character, or
range of characters, in the list. Lists can be ordinal (in-order) lists of many types. So…
ls [A-N]* will only match filenames whose first letter is a capital A through N.
This pattern, ls [drs]ing will match filenames; ding, ring, and sing.
Try typing:
$ ls /usr/bin/[ef]gr*
$ ls /usr/bin/lp[rsq]*
$ ls /etc/[a-g]*
Page 1 of 3
3.2 Filename conventions (expansion)
We should note here that a directory is merely a special type of file. So the rules and
conventions for naming files apply also to directories.
In naming files, characters with special meanings such as - / * & % , should be avoided.
Also, avoid using spaces within names. The safest way to name a file or directory is to use
only alphanumeric characters, that is, letters and numbers, together with _ (underscore) and
. (dot).
Filenames conventionally start with a lower-case letter, and may end with a dot followed by a
group of letters indicating the contents of the file. For example, all files consisting of C
code may be named with the ending .c, for example, prog1.c . Then in order to list all
files containing C code in your home directory, you need only type: ls *.c in that
directory.
$ ls /usr/bin/*.sh
/bin/*.sh
/etc/*.sh
Beware: some applications give the same name to all the output files they generate.
For example, some compilers, unless given the appropriate option, produce compiled files
named a.out. Should you forget to use that option, you are advised to rename the compiled
file immediately, otherwise the next such file will overwrite it and it will be lost.
3.3 Getting Help
On-line Manuals
There are on-line manuals (man pages) which give information about most commands. The
man pages tell you which options a particular command can take, and how each option
modifies the behavior of the command. Type: man command to read the manual (manpage) for a particular command.
For example, to find out more about the wc (word count) command, type:
$ man wc
When you are not sure of the exact name of a command, you can type,
$ apropos keyword
apropos will give you the commands with keyword in their man page header. It is the
same as the command; man –k keyword …For a keyword man-page search, Try typing:
Page 2 of 3
$ apropos copy
When you are looking for a short description of a command, you can type,
$ whatis keyword
whatis searches a set of database files and gives a one-line description of the command,
but omits any information about options etc. Only complete word matches are displayed. It is
the same as the command man –f keyword …For example, try this:
$ whatis whatis
The whatis database is created using the command: /usr/sbin/makewhatis
When you want to know what ‘type’ of command or file a particular file is, you can type,
$ file filename
file tests and attempts to classify each files’ file-type. A data block from the file is read
and is compared against entries in the magic file to determine its file-type. Try typing:
$ file ~/unixstuff/backups/science.bak
Summary
*
?
match any number of characters
[list,range]
match a character from the list
or match a character from the ordinal range
man command
apropos keyword
whatis command
file command
command --help
read the online manual page for a command
match one character
match commands with keyword in their man pages
brief description of a command
brief description of a the file type
command-line syntax help for a command
Page 3 of 3
Download