|Almost every UNIX command can use to find a file or directory. There are times you'll need part of a pathname - the head (everything before the last slash) or the tail (the name after the last slash). The utilities basename and dirname, available on most UNIX systems (and on the CD-ROM) handle that.|
basename /usr/bin/gigiplotgigiplot %
basename can also strip a suffix from a filename. For example:
basename /home/mikel/bin/bvurns.sh .shbvurns
dirname /usr/bin/screenblank/usr/bin %
If you give dirname a "pure" filename (i.e., a filename with no
path, as in the second example), it tells you that the directory is
. (the current directory).
basename 0.foo .foo
If the answer is
0, your basename implementation is good. If the answer is
0.foo, the implementation is bad. If basename doesn't work, dirname won't, either.
Here's an example of basename and dirname.
There's a directory tree with some very large files - over 100,000
You want to find those files, run
on them, and add huge. to the start of the original filename.
By default, split names the file chunks xaa, xab,
so on; you want to use the original filename and a dot (
instead of x:
for path in `find /home/you -type f -size +100000c -print` do cd `dirname $path` || exit filename=`basename $path` split $filename $filename. mv -i $filename huge.$filename done
(The absolute pathnames are important here. The cd would fail on the second pass of the loop if you use relative pathnames.) In the loop, the cd command uses dirname to go to the directory where the file is. The filename variable, with the output of basename, is used several places - twice on the split command line.
If that gives the error
command line too long, replace the first
lines with the two lines below.
This makes a
find /home/you -type f -size +100000c -print | while read path