Manipulating Files and Directories
Working With Unix
[Filenames
|Creating Directories
|Changing Directories
|Copying Files
|Moving Files
|Linking Files
|Searching Files
|Removing Directories
|Exercises
]
A filename is always used to locate a file. A filename can be just the name of a
file in the current directory, or a pathname to a file anywhere on the file
system. Take the following example:
larry% ls
hi mp.and.the.holy.grail.txt
larry% cat hi
Hi there. How are you?
larry% pwd
/home/schallee/tmp/unix/samplefiles
larry% cat /home/schallee/tmp/unix/samplefiles/hi
Hi there. How are you?
larry%
The filename hi and the filename
/home/schallee/tmp/unix/samplefiles/hi pointed to the same file.
You can also use the wildcard charachters "*" and "?." "*" matches zero or more
charachters, and "?" matches only one charachter. If there is more than one file
that fits a pattern, both are returned. So:
larry% cat *
would print out the contents of every file in the directory.
All of the following paterns would match at least the file
"mp.and.the.holy.grail.txt":
mp*
*.txt
*holy*
??.and.*holy*
*grail*
*grail????
To create a new directory, you use the make directory command
mkdir. mkdir takes a
filename from the command line, and creates a directory with that name. For
example, to create the directory "toast" you would:
larry% mkdir toast
larry% ls
hi toast
mp.and.the.holy.grail.txt
larry%
To change the current directory, you use the
cd command followed by the directory
that you wish to change to. If you don't supply cd with a directory, it
changes directories to your home directory.
larry% pwd
/home/schallee/tmp/unix/samplefiles
larry% cd
/home/schallee
larry% cd /home/schallee/tmp/unix/samplefiles
/home/schallee/tmp/unix/samplefiles
larry% cd toast
/home/schallee/tmp/unix/samplefiles/toast
larry%
The command for copying files is cp.
cp takes two arguments, first, the filename to copy from, and second, the
filename to copy to. cp takes the first file, and makes a duplicate copy
of it with a new name. If no second filename is provided, it copies the file
to a file with the same name in the current directory. If the second filename is
a directory, it copies the file to a file with the same name in that directory.
larry% pwd
/home/schallee/tmp/unix/samplefiles/toast
larry% cp ../hi hello
larry% ls
hello
larry% cp hello greetings
larry% ls
greetings hello
larry% cat greetings hello
Hi there. How are you?
Hi there. How are you?
larry%
Note: If the second file already exists, it will be completely written
over.
It is also useful to move, or rename files. To do this, you would use the
mv command. mv works about
the same way as cp. It takes two filenames as arguments, and moves
the first to the second. If the second exists, it over writes it. The first
is deleted in the process.
larry% ls
greetings hello
larry% mv hello goodbye
larry% ls
goodbye greetings
larry% cat goodbye
Hi there. How are you?
larry% mv goodbye greetings
larry% ls
gratings
larry% cat gratings
Hi there. How are you?
larry%
Linking files allows you to have a file with more than one name. For example,
if you have a friend who is working on a file that you also work on, you could
link that file to a different place. Each person could have a link to the file.
The command to do this is ln. It takes
two arguments. A file, and a new file name. It then links the new file name to
the file.
larry% ls
gratings
larry% ln gratings hello
larry% ls
gratings hello
larry% cat gratings hello
Hi there. How are you?
Hi there. How are you?
larry%
To search a file for a expression, you can use
grep. grep takes a
pattern to search for as the first argument. The following arguments are
files to search through. grep returns the lines that contain the
pattern.
larry% cd ..
/home/schallee/tmp/unix/samplefiles
larry% ls
hi toast/
mp.and.the.holy.grail.txt
larry% grep about mp.and.the.holy.grail.txt
ARTHUR: I did say sorry about the 'old woman,' but from the behind
DENNIS: That's what it's all about if only people would -
That's what I'm on about - did you see him repressing me, you saw it didn't
WOMAN: Oh, Dennis, forget about freedom. Now I've dropped my mud.
MINSTREL (singing): Yes Brave Sir Robin turned about
FATHER: Oh, don't worry about that.
bicker and argue about who killed who. We are here today to witness the union
FATHER: For, since her own father... who, when he seemed about to recover,
And chickening out and pissing about,
with it and lived! Bones of four fifty men lie strewn about its lair. So,
TIM: He's got huge, sharp - he can leap about - look at the bones!
BEDEMIR: How do know so much about swallows?
French folk with your silly knees-bent running about advancing behavior! I
larry%
Directories can be removed using the
rmdir command. rmdir
removes the directory named as the first argument if it does not contain any
files.
larry% ls
hi toast
mp.and.the.holy.grail.txt
larry% rmdir toast
rmdir: toast: Directory not empty
larry% cd toast
/home/schallee/tmp/unix/samplefiles/toast
larry% ls
greetings hello
larry% rm greetings hello
larry% ls
larry% cd ..
/home/schallee/tmp/unix/samplefiles
larry% rmdir toast
larry% ls
hi mp.and.the.holy.grail.txt
larry%
- Create a directory.
larry% mkdir toast
- Change the current directory to the new directory.
larry% cd toast
- Copy a file to the new directory.
larry% copy ../hi hello
- Move a file to the new directory.
larry% mv ../hi hi
- Link a file to a new name.
larry% ln ../mp.and.the.holy.grail.txt mp
- Search a file for some pattern.
larry% grep the mp
- Remove a directory.
larry% rm hello hi mp
larry% cd ..
larry% rmdir toast
[Back
|Contents
|Next
]
Edward B. Schaller
(schallee@earthlink.net)