Adding directory name to filename

Nils Kassube kassube at gmx.net
Fri Mar 5 19:10:45 UTC 2010


hippie dream wrote:
> for f in A/*/*.txt;do
>   fn=$(basename "$f")
>   d=$(dirname "$f")
>   dn=$(basename "$d")
>   n="$d/$dn$fn"
>   test -e "$n" && echo "not renaming $f - $n exists" || mv "$f" "$n"
> done

> Could "f" be any letter or does that specifically refer to a file? In
>  fact for the whole scripts do you just assignment with variable
>  names or are they arbitrary? Thanks so much!

The "f" as well as "fn", "d", "dn" and "n" are arbitrary variable names 
without special meaning. The script is mostly a sequence of assignments 
to separate the various parts of each file name. "f" is the original 
file name, "fn" is the file name without path, "d" is the directory 
tree, "dn" is the name of the last directory and "n" is the new file 
name including path.


Nils




More information about the ubuntu-users mailing list