Copying files from multiple directories into a single destination directory

前端 未结 4 2009
挽巷
挽巷 2021-01-24 09:14

There are multiple directories which contain a file with the same name:

direct_afaap/file.txt
direct_fgrdw/file.txt
direct_sardf/file.txt
...

N

4条回答
  •  失恋的感觉
    2021-01-24 10:00

    It may not be quite what you want, but it will do the job. Use cp --backup=numbered :

    $ find . -name test.sh
    ./ansible/test/integration/roles/test_command_shell/files/test.sh
    ./ansible/test/integration/roles/test_script/files/test.sh
    ./Documents/CGI/Code/ec-scripts/work/bin/test.sh
    ./Documents/CGI/Code/ec-scripts/trunk/bin/test.sh
    ./Test/test.sh
    ./bin/test.sh
    ./test.sh
    
    $ mkdir BACKUPS
    
    $ find . -name test.sh -exec cp --backup=numbered {} BACKUPS \;
    cp: './BACKUPS/test.sh' and 'BACKUPS/test.sh' are the same file
    
    $ ls -l BACKUPS
    total 28
    -rwxrwxr-x. 1 jack jack 121 Jun  9 10:29 test.sh
    -rwxrwxr-x. 1 jack jack  34 Jun  9 10:29 test.sh.~1~
    -rwxrwxr-x. 1 jack jack  34 Jun  9 10:29 test.sh.~2~
    -rwxrwxr-x. 1 jack jack 388 Jun  9 10:29 test.sh.~3~
    -rwxrwxr-x. 1 jack jack 388 Jun  9 10:29 test.sh.~4~
    -rwxrwxr-x. 1 jack jack  20 Jun  9 10:29 test.sh.~5~
    -rwxrwxr-x. 1 jack jack 157 Jun  9 10:29 test.sh.~6~
    

    If you really want to put part of the folder name in, you have to decide exactly what part you want. You could, of course, just replace the directory separator character with some other character, and put the whole path into the filename.

提交回复
热议问题