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?
2 Answers 2
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.
-
What are those hex values?Santosh Kumar– Santosh Kumar2019年05月28日 14:02:52 +00:00Commented May 28, 2019 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 insideimage_sequence
or otherimage.xxxx.jpg
files in other directories.Freddy– Freddy2019年05月28日 14:03:23 +00:00Commented May 28, 2019 at 14:03 -
One last doubt. I want to replace
├──
and└──
to something simpler. Say|-
andL
. Because my shell is not printing it well. How can I do that?Santosh Kumar– Santosh Kumar2019年05月29日 06:07:31 +00:00Commented May 29, 2019 at 6: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– Santosh Kumar2019年05月29日 06:14:21 +00:00Commented May 29, 2019 at 6:14 -
With the ascii option it's
tree --charset=ascii | sed '/|-- image\.[0-9]\+\.jpg/d; s/\(`-- image\.\)[0-9]\+\(\.jpg\)/1円####2円/'
Freddy– Freddy2019年05月29日 07:48:44 +00:00Commented May 29, 2019 at 7:48
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
-
bash: s/\.[0-9]\+\.jpg/.####.jpg/g: No such file or directory
Santosh Kumar– Santosh Kumar2019年05月28日 14:03:32 +00:00Commented May 28, 2019 at 14:03 -
It's important to quote the
s/.../.../g
part with single quotes ('
), or else bash will try to expand the[0-9]
part (and fail).JoostM– JoostM2019年05月28日 14:23:10 +00:00Commented May 28, 2019 at 14:23 -
I just copy pasted your snippet.Santosh Kumar– Santosh Kumar2019年05月28日 16:06:04 +00:00Commented May 28, 2019 at 16:06
-
@SantoshKumar I accidentally chopped the word
sed
out while editing. Could you try again?JigglyNaga– JigglyNaga2019年05月28日 16:13:52 +00:00Commented May 28, 2019 at 16:13
tree
command or not.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).