.. _unix2: .. highlight:: tcsh ********************************* Unix Basics 2 ********************************* Hopefully you are becoming familiar with the terminal environment in Unix. Today we will examine more basics for working in Unix. ============= Wildcards ============= When entering file and directory names, Unix gives us a number of special ways to refer to filenames, knowing as "globbing" using "wildcard" characters. Wildcard characters that have a special meaning in the terminal include ``*``, ``\``, ``?``, ``^`` and ``[ ]`` and can be used to designate various patterns. One example is the ``*`` character, which stands for any number of characters or any type. This is often used to find files with a certain extension, as ``*.txt`` will match any file with the extension ``.txt``. You can also use multiple wildcards in the same expression; for instance if you want list all files containing the word "data" that ends in ".txt" you could use ``ls *data*.txt`` and the shell would show you all of the files matching that pattern. Other useful wildcards include: * ``?``, which can be used to represent a single unknown character. For instance, ``?at`` matches ``Bat``, ``bat``, ``Cat``, ``cat``, and many others, but not ``at`` alone. * ``[]`` can be used to represent any one of the characters included in the square brackets. As an example, ``[CB]at`` matches ``Bat`` and ``Cat``, but not ``bat`` or ``cat``. You can also specify a range of characters such as [A-Z] for any capital letter, [a-z] for any lowercase letter, and [0-9] for any numeric character, or [A-Za-z0-9] for any alphanumeric character. * ``^`` negates whatever is entered. For example, ``[^]`` matches any single character *except* those included in the square brackets. ``[^C]at`` will match ``Bat``, ``bat``, ``cat``, and many other strings, but not ``Cat``. What if your expression contains one of the special wildcard characters? Like we saw with having a space in the file name, you can enter an "escape character" by preceding the character with a backslash ``\``. You will also need to escape wildcards in some other situations, like when using wildcard to denote a file name when using ``find`` (see below). Thus if you want to match a sequence that includes a question mark, use, for example, ``hello\?.txt``. There are more sophisticated pattern matching techniques called "regular expressions" which we will use more extensively when we talk about AWK. Getting used to using these wildcards in the terminal will make regular expressions a bit easier to understand, so be sure to spend some time experimenting with them. =================== Finding Files =================== To make queries of files in the filesystem, use the ``find`` command, which has a number of sophisticated options. The syntax for ``find`` is ``find ``, where ```` are the directories that you wish to search (including all subdirectories) and ```` are the specific options that you want to search for (these are specified by command options). Command options for ``find`` that I use frequently include: * ``-name `` finds files whose name matches the given pattern. You are allowed to use wildcards in name patterns, but you must put a backslash in front of any special wildcard characters (otherwise, the shell expands the wildcards *before* searching the files, while we want the wildcards to remain until ``find`` does its search). * ``-iname `` same as ``-name``, but is case insensitive * ``-type ``, target is of a specific type, common examples include ``f`` regular file, ``d`` directory * ``-atime