LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux commandline add map structure as prefix to files. (https://www.linuxquestions.org/questions/linux-newbie-8/linux-commandline-add-map-structure-as-prefix-to-files-4175507423/)

Boomhenk 06-08-2014 12:15 PM

Linux commandline add map structure as prefix to files.
 
Hello,

I am trying to get the grasp of linux commandline, but this problem I just can't solve.

I would like to merge multiple folders, possibly containing files that may cause duplicate names, into one. To prevent dupes, I would like to have part of the map structure prepended to the file names. At least, that seemed the most obvious solution. So I would like:

./map1/a.dat ~> ./map1_a.dat
./map1/b.dat ~> ./map1_b.dat
./map2/a.dat ~> ./map2_a.dat (no duplicate filename :))
and trivially: ./a.dat -> ./a.dat

and so on, for all maps within ./ .
How should i go about this?

~

Boomhenk

jpollard 06-09-2014 09:22 AM

I will direct you to the "find" command first. This will list the files (as a path name). Be sure to only look for regular files.

Next, the output of that will be files like ./map/a.dat. (you can avoid the leading ./ if you specify map as the directory to process).

This can be piped to a loop to process each name - replacing the "/" with a "_", you now have the original name, and the replacement; at which point you can use the mv <orginalname> <replacementname>...

If you use the mv command, the directory tree processed will be empty of anything except other directories.


All times are GMT -5. The time now is 10:05 PM.