From the current directory I have multiple sub directories:
subdir1/ 001myfile001A.txt 002myfile002A.txt subdir2/ 001myfile001B.txt 002myfile002B.txt
where I want to strip every character from the filenames before
myfile so I end up with
subdir1/ myfile001A.txt myfile002A.txt subdir2/ myfile001B.txt myfile002B.txt
I have some code to do this...
#!/bin/bash for d in `find . -type d -maxdepth 1`; do cd "$d" for f in `find . "*.txt"`; do mv "$f" "$(echo "$f" | sed -r 's/^.*myfile/myfile/')" done done
however the newly renamed files end up in the parent directory
myfile001A.txt myfile002A.txt myfile001B.txt myfile002B.txt subdir1/ subdir2/
In which the sub-directories are now empty.
How do I alter my script to rename the files and keep them in their respective sub-directories? As you can see the first loop changes directory to the sub directory so not sure why the files end up getting sent up a directory...