[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

File Management

Contents Previous Topic Next Topic

We will now examine routines for manipulating files. To begin, we will look at different ways of specifying files, either through their full names or through patterns. Following that, we will look at five Unix commands for file manipulation.

Filename Patterns (*, ?, and [...])

Suppose you have a directory with five files ending in .cpp. You could refer to these five files using the five file names, for example, file1.cpp file2.cpp file3.cpp file4.cpp file5.cpp, however, you can also use the * pattern, which can be used to represent anything. Thus, all files ending in .cpp may be described using *.cpp.

To replace just a single character, use ?. For example, D??? represents all four-letter file names which begin with the letter D (whereas D* would represent all file names which simply begin with the letter D.

To determine which patterns are being matched, you can use the echo command which simply prints the result to the screen:

echo *.cppPrint all files ending in .cpp
echo *.hPrint all files ending in .h
echo Single*Print all files starting with Single
echo */*.cppFor each sub-directory (the first *) of the working directory, print all files ending with .cpp

On occasion, you may wish to use alternation using [...] which matches any one character in the brackets. For example, ls [ab]* would list all files or directories which start with either an a or b. Ranges of letters may also be specified using a dash; ls [a-z]* lists all files starting with a lower-case character.

cat

The cat command concatenates the files you list and prints the result to screen. If only one file is provided, it is equivalent to printing to the screen.

{ecelinux:1} cat SingleList.h

The cat command can be used to quickly generate a text file:

{ecelinux:1} cat > tmpfile
Start typing here, and
keep typing until you have typed enough.
Once you're finished, hit Ctrl-c.  This
terminates the command, and all
this text will be stored in the file tmpfile.
Just to create a big file, why don't you cut-and-paste the entire page you're reading right now.
We will see more about > later...

more

more prints a single file to the screen, however, unlike cat pages through the file one screen at a time, prompting the user to press any key to continue to the next screen.

{ecelinux:1} cat tmpfile

If you find you are calling more on a very large file, you can terminate the program with Ctrl-c.

cp

If you are copying one file to a new name, you may use

{ecelinux:2} cp tmpfile tmpfile.bak

If you are specifying more than one file, the last argument must be a directory to which all other arguments (file names) will be copied.

For example, to copy all files in ece250 to a backup directory, you could use:

{ecelinux:3} ls
ece250
{ecelinux:4} mkdir backup
{ecelinux:5} cp ece250/* backup/
{ecelinux:6}

mv

The move command does the same as copy but it deletes the original copy.

rm

You may delete a file with the remove command.

This works like copy, however, you simply list the files you wish to delete. Note that there is no undelete command, so use this command sparingly.

Why doesn't cp *.cpp *.cpp.old work?

In MS-DOS, it was common to rename files using syntax such as

> copy *.c *.bak

Copy would then take each .c file and make a copy which has a .bak extension. This is not possible in Unix because it is the terminal itself which converts the *.c and *.bak into the appropriate names. In MS-DOS, the arguments "*.c" and "*.bak" were passed directly to copy, and therefore, copy could interpret them appropriately.

To do this in tcsh, use

{ecelinux:1} tcsh
{ecelinux:2} foreach i (`ls *.cpp`)
foreach? cp $i $i.old
foreach? end
{ecelinux:3}
Contents Previous Topic Next Topic

Copyright ©2005-2008 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]