Score:0

How to copy files that have same name as the folder they are in?

cn flag

There are files that have a pattern like name/name.ext inside subdirectories. There are many files with the same extension inside the subdirectories but I want to copy only the files which have pattern mentioned. what is the command to achieve this?

Score:2
hr flag

There may be a clever way to do this using a shell like zsh that has more extensive filename generation (globbing) capabilities, but if you can't do that, then the GNU find command supports regular expression matching with backreferences for at least some of the regextypes. Ex. given

$ tree .
.
├── bar
│   ├── 1.xyz
│   ├── 2.xyz
│   └── 3.xyz
├── baz
│   ├── 1.xyz
│   ├── 2.xyz
│   ├── 3.xyz
│   └── baz.xyz
└── foo
    ├── 1.xyz
    ├── 2.xyz
    ├── 3.xyz
    └── foo.xyz

3 directories, 11 files

then

$ find . -regextype posix-extended -regex '.*/([^/]+)/\1\.xyz$'
./baz/baz.xyz
./foo/foo.xyz

which you can modify to copy like

find . -regextype posix-extended -regex '.*/([^/]+)/\1\.xyz$' -exec cp -n -t path/to/newdir/ {} +
Score:0
iq flag

The following will copy ./name/name.txt to . (your currernt directory)

cp name/name.txt .
papabiceps avatar
cn flag
name/name.ext is a pattern. `name` can be anything. its a placeholder.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.