I have a collection of directories that all contain a particular file that is named the same in all the directories.
dir1/dirA/file.txt
dir1/dirB/file.txt
dir1/dirC/file.txt
....
dir4/dirX/file.txt
dir4/dirY/file.txt
dir4/dirZ/file.txt
I want to copy these files into another directory, such that each file's name is its parent directory's name:
all_files/
dirA.txt
dirB.txt
dirC.txt
....
dirX.txt
dirY.txt
dirZ.txt
While I understand these examples could cause overlap, it's not an issue for me -- I know all the target directories have unique names.
I'm using bash in OS X. I tried doing several things like this (see here and here). My thought would be to split the string on the slashes, get the second to the last one, and use that, but I couldn't get it to work (based on this and others):
for name in `ls */*/file`
directory=${DIRS[${#DIRS[@]} - 2]}
mv name ../all_files/${directory}.txt
done
However, I couldn't get it to work.