45.18 Using basename and dirname
45.18.1 Introduction to basename and dirnameThe basename command strips any "path" name components from a filename, leaving you with a "pure" filename. For example:
% basename can also strip a suffix from a filename. For example:
% The dirname 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
45.18.2 Use with Loops
Here's an example of
basename
and
dirname
.
There's a directory tree with some very large files - over 100,000
characters.
You want to find those files, run
split
(
35.9
)
on them, and add
huge.
to the start of the original filename.
By default,
split
names the file chunks
xaa
,
xab
,
xac
, and
so on; you want to use the original filename and a dot (
The find command will output pathnames like these:
/home/you/somefile /home/you/subdir/anotherfile (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
find /home/you -type f -size +100000c -print | while read path - , |
|