Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 2883  / 3 Years ago, tue, july 27, 2021, 1:02:53

I have bunch of directories containing the file with the same name. I want to move these files to another directory and at the same time renaming them with the directory name so that they are distinguished and are not over-written.



EDIT:
All the directories are in the same directory. Destination is one directory on the system which could be anything. We read directory and read file form it and rename it exactly as the directory name and put it to the destination.



An important constraint is that the name of the file is given which will be in all of the directories. Directories might contain other files bit also the one which is given



Thanks a lot


More From » scripts

 Answers
5

Assuming the filename of these files are thefilename, you've cded to the directory containing these directories, and you want to move them to /path/to/dest, the following shell loop should do the trick



for file in */thefilename; do
echo mv -iv "./$file" "/path/to/dest/${file%/*}"
done


You can run that directly in an interactive shell, or put it in a file and run it as a script.



I added an echo to make it only print the mv commands. If the output looks correct, remove the echo and run it again to have it actually do the moving.


[#39292] Tuesday, July 27, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iriousutur

Total Points: 321
Total Questions: 112
Total Answers: 106

Location: Sweden
Member since Mon, Dec 7, 2020
3 Years ago
iriousutur questions
Sun, Apr 9, 23, 02:48, 1 Year ago
Tue, May 23, 23, 00:41, 1 Year ago
Fri, Oct 21, 22, 02:47, 2 Years ago
Mon, May 9, 22, 00:01, 2 Years ago
;