I have a folder named
/home/user/temps which has 487 folders.
In each folder I have a file called thumb.png.
I want to copy all files named thumb.png to a separate folder and rename them based on the folder they came from.
migrated from stackoverflow.com Apr 20 '11 at 13:43
This question came from our site for professional and enthusiast programmers.
Here you go:
the canonical wisdom, by the way, is that using
for clarity, I'll break it down:
details on the
the quotes in the
Short helper code:
let's name it 'deslash.sh' and make it executable. Call it with:
It will fail, if a collision exists
but that's unavoidable.
This works for arbitrarily deep subdirectories:
The interessting part is the parameter expansion
Note: the result is dependent on the working directory and the path parameter for find. I executed the command from the parent directory of temps. Replace
I have added quoting in case any of your dir names have white-space chars embedded in them.
Also I have changed this so it will only print out the commands to be executed.
Examine the output of the script to be sure all files/path names look proper.
When you're sure there are no issues with the commands that will be executed, remove the
To copy you need the command cp, and to rename for linux is the same than moving the file, so you have to do it with mv command. In Linux you always have to specify the whole path, from the source, if you are in another folder, and to the destination folder, of course. I'd be something like this, for copy:
or to rename or move