0

I'm in a directory where running tree command produces something like this:

├── directory1
│   └── image_sequence
│       ├── image.0001.jpg
│       ├── image.0002.jpg
│       ├── image.0003.jpg
│       ├── image.0004.jpg
│       ├── image.0005.jpg
│       └── image.0006.jpg
│ 
└── directory2
    ├── somefile.ext
    └── someanotherfile.ext2

The image sequence inside image_sequence produces a large listing that I want to trim. My desired output is something like below:

├── directory1
│   └── image_sequence
│       └── image.####.jpg
│ 
└── directory2
    ├── somefile.ext
    └── someanotherfile.ext2

Can the output of tree command somehow be modified?

Santosh Kumar
  • 3,753
  • 6
  • 28
  • 36
  • I'm confused about whether you want to use the `tree` command or not. – Jeff Schaller May 28 '19 at 13:00
  • https://unix.stackexchange.com/q/334438/117549 may be the answer to this question; see also https://unix.stackexchange.com/q/368884/117549 and https://unix.stackexchange.com/q/47805/117549 – Jeff Schaller May 28 '19 at 13:02
  • Tree command can be used, except that the whole sequence listing needs to converted to `image.####.jpg` format. Then use Python to modify image sequence output. The links you gave suggested the flags which will completely omit the output of image sequence. There's also an option called `--filelimit` which is close to what I want, but again it completely omits the image sequence output. I fear that using Python completely will not produce the identical output (the symbols and all). – Santosh Kumar May 28 '19 at 13:45

2 Answers2

1

Try this:

tree | sed '/\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 image\.[0-9]\+\.jpg/d; s/\(\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 image\.\)[0-9]\+\(\.jpg\)/\1####\2/'
  • The first /.../d; deletes all lines containing ├── image.[0-9]+.jpg (pseudo-pattern) entries
  • The second s/.../\1####\2/ replaces the last line └── image.[0-9]+.jpg

Output:

$ tree | sed  '/\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 image\.[0-9]\+\.jpg/d; s/\(\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 ima
ge\.\)[0-9]\+\(\.jpg\)/\1####\2/'
.
├── directory1
│   └── image_sequence
│       └── image.####.jpg
└── directory2
    ├── someanotherfile.ext
    └── somefile.ext

3 directories, 8 files

This will of course only work if all files in image_sequence match the image pattern and will modify filenames in other directories matching the patterns. If the last file in image_sequence for example is readme.txt, then you will remove all image entries instead.

Freddy
  • 25,172
  • 1
  • 21
  • 60
  • What are those hex values? – Santosh Kumar May 28 '19 at 14:02
  • `├──` and `└──`. This is why it only works if all files match those patterns. So I wouldn't use this solution if you have other files inside `image_sequence` or other `image.xxxx.jpg` files in other directories. – Freddy May 28 '19 at 14:03
  • One last doubt. I want to replace `├──` and `└──` to something simpler. Say `|-` and `L`. Because my shell is not printing it well. How can I do that? – Santosh Kumar May 29 '19 at 06:07
  • Well, I see there's an option for `--charset` which take _ascii_ or _unicode_. Can you please modify your answer with that? – Santosh Kumar May 29 '19 at 06:14
  • With the ascii option it's `tree --charset=ascii | sed '/|-- image\.[0-9]\+\.jpg/d; s/\(\`-- image\.\)[0-9]\+\(\.jpg\)/\1####\2/'` – Freddy May 29 '19 at 07:48
  • Nope! That prints out all the images. Not even `####`ing. – Santosh Kumar May 29 '19 at 09:01
  • Works for me using `tree v1.7.0`. With ascii I get prefixes `|--` and `\`--` which are matched by the above `sed`. If it doesn't match it will of course change nothing. – Freddy May 29 '19 at 09:35
1

You could replace the numerical parts of the sequential filenames with #s, using a sed expression (similar to the second one in Freddy's answer). uniq can then remove the duplicate lines:

tree | sed 's/\.[0-9]\+\.jpg/.####.jpg/g' | uniq

This will still leave two entries for the images (because the final line uses a different symbol in the tree-drawing part), but it has still trimmed the list down to a manageable length:

.
|-- directory1
|   `-- image_sequence
|       |-- image.####.jpg
|       `-- image.####.jpg
`-- directory2
    |-- someanotherfile.ext2
    `-- somefile.ext
JigglyNaga
  • 7,706
  • 1
  • 21
  • 47