Score:0

Comparing structure of files

lk flag

I got 2 structures of dirs:

dir1
├── 1
│   └── file1
├── 2
│   └── file1
├── 3
│   └── file1
├── 4
│   └── file1
├── 5
    └── file1
dir2
├── 1
├── 2
├── 5

I use xargs mkdir -p < dirs.txt to make file.txt with names of dirs. In this file i got only paths to dirs, without files in dirs. How take out paths of files only includes list of dirs in dirs.txt?

For example: I need take out from dir1 structure of dirs from dir2 with files from dir1. This should look like dir3:

dir3
├── 1
│   └── file1
├── 2
│   └── file1
├── 5
│   └── file1

On last step I will use: rsync -a /source/directory --files-from=/full/path/to/listfile /destination/directory

Score:0
cn flag

Difficulty with these tasks is usually when you need to match something in the middle of the path, in this case, it seems you can match the second level from the two directory lists. I also assume you do not have, e.g., newlines in any of the paths.

awk -F / -vOFS=/ -vf=2 'NR==FNR {a[$f]++; next} $f in a {$1=""; print $0}' \
      <(printf %s\\n dir2/*) dirs.txt | rsync -avx --files-from=- dir1/ dir3

Setting the delimiter -F to / basically makes fields out of the different path components, the path component after the starting-point will become the 2nd field. The first pattern action NR==FNR {} reads the first file into the array, which is the directory list of dir2. When the first file reaches the end, NR==FNR becomes false and the second file will be processed by the next pattern action. Before printing the path, starting-point has to be removed to account for the source/ destination of rsync.

Score:0
cn flag

Use the command find. It can be instructed using the -type flag to only find directories, and with the -path flag, only find items matching a path. For example,

find . -path './dir1/*' -type d

will only list all directories and subdirectories under dir1. You can then redirect that to a dirs.txt file, and edit that file the way you want.

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.