The goal is to prepend text to every file name in a specific directory using its path. I don't want this to apply to just the current directory, but would work no matter where you are in the file tree. This is what I have:
for f in `ls path/*`
do
mv "&f" "x$f"
done
Instead of moving it to the same path with a new filename with the x in front, it attempts to move it to x/path (which gives an error obviously), and I can't seem to find anything on how to get the x just in front of the filename to effectively rename the file with x in front.
You could rely on both basename
and dirname
commands:
for i in path/*; do
mv "$i" "$(dirname "$i")/x$(basename "$i")"
done
Or with bash parameter expansion:
for i in path/*; do
mv "$i" "${i%/*}/x${i##*/}"
done
Both commands add a x
before the filename.