command strips any "path" name components from a
filename, leaving you with a "pure" filename.
can also strip a suffix from a filename.
basename /home/mikel/bin/bvurns.sh .sh
command strips the filename itself, giving you the
"directory" part of the pathname:
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).
have a bug in many System V implementations.
They don't recognize the second argument as a filename suffix to strip.
Here's a good test:
basename 0.foo .foo
If the answer is
, your basename
implementation is good.
If the answer is
, the implementation is bad.
doesn't work, dirname
Here's an example of basename
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
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`
cd `dirname $path` || exit
split $filename $filename.
mv -i $filename huge.$filename
command will output pathnames like these:
(The absolute pathnames are important here.
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.
variable, with the output of basename
, is used
several places - twice on the split
If that gives the error
command line too long
, replace the first
lines with the two lines below.
This makes a
redirected-input loop (45.22
find /home/you -type f -size +100000c -print |
while read path