0

I have several directories:

├── folder1.x
│   ├── dsferwe
│   ├── ewrwr342
│   ├── analysis
│   ├── fdsds
│   ├── vcxvcxz
│   ├── 324322
│   ├── export.asc
│   ├── f_random_subfolder.xf
│   │   ├── -.method
│   │   ├── desktop.ini
│   │   ├── lock.file
│   │   └── submethods.xml
│   └── Sync
├── random_folder4.x
│   ├── dsferwe
│   ├── ewrwr342
│   ├── analysis
│   ├── fdsds
│   ├── vcxvcxz
│   ├── 324322
│   ├── export.asc
│   ├── 22random_subfolder.xf
│   │   ├── -.method
│   │   ├── desktop.ini
│   │   ├── lock.file
│   │   └── submethods.xml
│   └── Sync
├── X93430.x
│   ├── dsferwe
│   ├── ewrwr342
│   ├── analysis
│   ├── fdsds
│   ├── vcxvcxz
│   ├── 324322
│   ├── export.asc
│   ├── random_subfolder222.xf
│   │   ├── -.method
│   │   ├── desktop.ini
│   │   ├── lock.file
│   │   └── submethods.xml
│   └── Sync

Each directory contains an export.asc file.

I want to copy all export.asc files to a new directory while renaming to have the same name as their parent directory. Therefore, in the end according to the example above I will have these files in a new folder:

folder1.x.asc
random_folder4.x.asc
X93430.x.asc

How can I achieve this?

RELATED topics I have already followed but haven't had any success

2

You can do it with find -exec sh -c ...

find . -name export.asc \
-exec sh -c 'cp "$1" /path/to/new/folder/"$(basename "$(dirname "$1")")".asc' sh {} \;
  • "$(basename "$(dirname "$1")")" will be the name of the folder the file resides in.

Add -i to cp if you want to make sure that no files are overwritten without prior interaction.

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.