0

The structure of my files is as follows:

My_Files:
        |-- Folder_1: 
        |           |-- a.txt
        |           |-- b.txt
        |           |-- c.txt
        |
        |-- Folder_2:
                    |-- a.txt
                    |-- b.txt

I have about 200 folders like this.

What I Want:

I want to copy my files and rename files of the same name into one folder such as this:

Final_Folder: 
            |-- a.txt
            |-- b.txt
            |-- c.txt
            |-- a1.txt (Renamed)
            |-- b1.txt (Renamed)

What I did:

I used from this command but no result found:

find My_Files/ -type f -print0 | xargs -0 cp -t Final_Folder/

and the output is as follow:

cp: will not overwrite just-created 'Folder_1/a.txt' with 'Final_Folder/a.txt'

cp: will not overwrite just-created 'Folder_1/b.txt' with 'Final_Folder/b.txt'

cp: will not overwrite just-created 'Folder_1/c.txt' with 'Final_Folder/c.txt'

cp: will not overwrite just-created 'Folder_2/a.txt' with 'Final_Folder/a.txt'

cp: will not overwrite just-created 'Folder_2/b.txt' with 'Final_Folder/b.txt'

How can I reach my goal?

Thanks in advance.

  • How fixed is the naming scheme? Does find My_Files/ -type f -exec cp -t Final_Folder --backups=numbered {} + do something close enough to what you want? – icarus Nov 9 '19 at 19:07
0

Given your scheme, and assuming Folder 1, Folder 2, Folder 3 etc. actually do hold some meaning (otherwise you can just append the current timestamp to every filename you have to avoid the problem you are getting), I would go with the following command (make sure the folder Final_Folder exists beforehand) :


find . -path ./Final_Folder -prune -o -type f -exec sh -c 'fn=$(basename -- {}) ; suffix=$(dirname {} | tr / _  ) ; cp {} ./Final_Folder/${fn%.*}$suffix.${fn##*.};   ' \;


the file-naming scheme will follow a scheme like this:

[basename filename]._[basepath folder 1]_[basepath folder2]_[basepath folder[3..n].[extension]

or in other words, the Final_Folder contents will be:

a._Folder_1.txt
a._Folder_2.txt
a._Folder_3.txt
b._Folder_1.txt
b._Folder_2.txt
b._Folder_3.txt
c._Folder_1.txt
c._Folder_2.txt
c._Folder_3.txt

The nice thing about this scheme is that if your hierarchy for example resembles:

[YEAR]/[MONTH]/[DAY]/[filename a,b,c].txt

as is often done for data collection

if you run the above command from the top level directory your filenames from today copied into the Final_Folder would be named:"

a._2019_11_10.txt
b._2019_11_10.txt
c._2019_11_10.txt
.
.
.
[a,b,c]._2019_11_10.[WITH OTHER EXTENSIONS]

and sorting, pulling or analyzing files of specific type, or name, or from a given year,month, or day and so on would be a very simple thing to do.

| improve this answer | |
  • Thanks, Good answer :) – Saeed Nov 10 '19 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.