UNIX Power User Tips

Wildcard Characters

Like wildcards in poker wildcard characters are special characters that can stand for any other character (or strings of characters) in a filename. Some of the wildcard characters are described below. To use them simply include them in place in the filename and the filename will be expanded to include all the matches to your specification.

*Matches any characters or strings of characters.

?Matches a single character.

[...]Matches one of the characters listed inside the brackets.

Examples:

test*Matches any file that starts with the string test.

test.txt | testy.img | testerosa.doc | testing.mpeg

*forMatches any file ending with the string .for.

test.for | carrefor | for | testing.rfor

te*imgMatches any file starting with te and ending in img.

test.img | tell_th_man.img | terror.img | tearanimg

te?t.txtMatches te[character]t.txt pattern.

test.txt | telt.txt | tert.txt | temt.txt

test.???Matches all files with test. followed by a 3-letter suffix.

test.txt | test.img | test.doc |test.mpeg

[lmt]est.txtMatches all files starting with l, m or t followed by est.txt.

lest.txt | mest.txt | test.txt

Filename Completion

This is a useful feature of the c-shell which will complete a filename automatically when the <ESC> key is pressed, so long as there is an unambiguous start to the filename. For instance the user can type te<ESC> to signify the file name test345_filt2030_lessthan20.img, which would otherwise be a pain in the butt to type. Filename completion will also complete the name up to the ambiguous portion. Experiment to see what is meant by this. Include the command below in your .cshrc file to set this feature permanently for your shell.

set filec

Sets filename completion feature.

I/O Redirection

Many commands can use standard input and/or standard output channels. Standard input means that instead of taking input from a file the command takes input from the user typing, standard output is to display on the screen. This standard input and output can be redirected in a number of ways. It may be useful for instance, to have a text file of the listing of a directory, or to feed the output from one command into the input of another command. Below are listed some redirection methods, but it should be noted that not all commands can use these methods.

[command] > [filename]Writes standard output to a file.

[command] >> [filename] Appends standard output to a file.

[command] < [filename]Takes the input for the command from a file.

[command1]|[command2]Uses the standard output from command1 as the input for command2, called a pipe or pipeline.

Examples:

ls —al > my_directory.txt

Lists all contents of the pwd and puts them in a file called my_directory.txt.

cat my_directory.txt >> all_directories.txt

Appends the contents of my_directory.txt to the end of all_directories.txt, if the file doesn’t exist it is created.

man csh | grep test

Pipes the manual page for csh to the command grep, which is a search function. This command will return the lines of the manual page that contain the word test.

History Substitution

The c-shell maintains a list of all the lines a user types for each session, called a history. The history is accessible and potentially useful to the user. If the user needs to type the same complex command multiple times or remember a complex command that they typed 50 lines ago, this can be easily accomplished with history substitution.

!!Executes the previous command typed.

!-[x]Executes the command typed x commands ago.

![n]Executes the nth command in the history list.

history

Lists each command in the history list, preceded by its number.

Job Management

The c-shell has access to a list of all of the commands running on the system at any particular time. Each command, or job, has a specific ID number that can be used as a reference handle.

ps

Returns a list of all currently running processes, or jobs, for the current user. The listing has the process ID (PID) or job number on the left and the name of the process (program) on the right.

ps —ef

ps —ef | grep [search string]

ps —ef | grep mcdermoj

Returns a list of all currently running processes for all users in long format. The second syntax listing gives a way of quickly searching this list for the search string using the command grep and a pipe. The example returns all the processes that were initiated by the user mcdermoj (technically, any listing containing the string mcdermoj). The search string can also be the name of a process, or part of a name, or the date of initiation (3 letter month and date separated by a space).

kill [PID]

kill 4503

Kills the process referred to by the PID (obtained using ps listing) causing termination of the program.

Command Scripts

Scripts can be used to automate procedures involving more than one step, and/or operating on more than one file. Scripts can be as simple as stringing together a number of command lines or can be quite complex by using the programming-like features of the c-shell. A script is a text file which has execute permissions (see chmod above), it is invoked by typing the full name of the script file on the command line. This will work if the script file is in the pwd or if the script file is in the csh search path. Below are several examples of script files, followed by explanations of how they work. To use any of these scripts copy them to a text file in UNIX, use chmod (chmod uga+x [script filename]) to add execution privileges for everyone for the file and then type the name of the script file from the command line to execute it.

Example 1: Simple Script, simple.com

!#/bin/csh —fx

# This is a simple script file.

# It lists the contents of the pwd in long format,

# then finds all the occurrences of files containing

# the word test and places them in a new file called

# simple.out, then prints the file to the screen.

echo ‘This is a simple script file, enjoy!’

ls —al

ls *test* > simple.out

cat simple.out

# end of the script file

The first line should always be in a script file. This tells UNIX that it should treat the remainder of the file as a csh script. The pound sign (#) at the start of the line means that csh will ignore the remainder of the line- this is called a comment and can be used to give information about the script. echo simply prints the following string to the screen. The remainder of the script is simply commands.

Example 2: File Input, finp.com

!#/bin/csh —fx

# This is a simple script file.

# It demonstrates how to use information

# typed on the command line by the user of

# the script. It should be invoked by:

# finp.com [some text file name]

#

echo ‘Contents of ’ $1 ‘ are:’

cat $1

# end of the script file

This script demonstrates how to use input arguments to the script file. Each input argument is given a different number according to the order they are entered. The number is preceded by a dollar sign ($) since these are considered variables and all variables in csh are referred to by $variable. This script takes only one input argument, but has two input variables. $1 refers to the first input variable, [some text file name], and $0 would refer to the name of the command script, finp.com.

Example 3: Multiple Input Arguments, minp.com

!#/bin/csh —fx

# This script takes the idea behind

# the wildly popular finp.com and allows

# the user to input multiple file names,

# and use filenames with wildcards.

# minp.com [file1] [file2] [file3] ... [fileN]

#minp.com [wildcard file specifier]

foreach file ( $argv )

echo ‘Contents of ’ $file ‘ are:’

cat $file

echo ‘--------------------------------------------‘

end

# end of the script file

The c-shell contains a number of built in commands which allow complex, program-like scripts to be written. This script demonstrates the use of one such command, the foreach command. The idea is that the command line can contain a variable number of arguments (file names). The script steps through each argument and performs several operations for each argument. The syntax of the foreach command is listed below. The variable is set to each member of the list in turn and the commands are performed. In the example given above the list is given as $argv which is the csh variable for the list of input arguments. The variable is referenced by the standard notation $[variable], in the case given above, $file. This technique works with wildcard file designations since the csh interpreter treats a wildcard designation which matches more than one file as a list (e.g. t* will give a list of all files starting in t).

foreach [variable] ( [list] )

[commands]

end