EDIT: Thanks for the help on this. I was hoping to run it as a for loop so I could pass the extension in as an argument later eg: for file in (find . -type f -name '*.'$1); do
etc... Could anyone add the best approach to adapting one of the solutions to work in the loop context after my do
?
Original post:
I'm embarrassed at how long I've been working at this one.
I'm trying to append filename with last 3 characters of the file's parent directory in Bash.
For example, my directories look like this:
192.168.1.101
192.168.1.102 etc...
and the files within each directory look something like this:
120000.JPG
120137.JPG
and I would like to rename the files to look like so:
101_120000.JPG
I've tried everthing from sed, to awk and haven't quite got there yet. I'm trying to use a shell script with a for loop. I've managed to print the last 3 of the parent directory using:
for f in `find . -name '*.'$1`
do
echo $(dirname $f) | (tail -c 4 $t)
but I haven't been able to pass that off as a variable to use in the mv command in the for loop. If I try:
for f in `find . -name '*.'$1`
do
lastThree=$(dirname $f) | (tail -c 4 $t)
I get an error "tail: error reading './192.168.1.101': Is a directory"
Curious if I'm approaching the solution the right way. If anyone has suggestions on how to solve, I'd be much appreciated.