Skip to content

Instantly share code, notes, and snippets.

@greymd
Last active March 13, 2017 07:42
Show Gist options
  • Select an option

  • Save greymd/cf707f310019cbc6dbbede1bc91eee3b to your computer and use it in GitHub Desktop.

Select an option

Save greymd/cf707f310019cbc6dbbede1bc91eee3b to your computer and use it in GitHub Desktop.
convert tree result to have full path.

tree -f でいいじゃんという話。

$ tree test | awk 'NF{depth=gsub("[│├└]","&",$0); if(last_depth < depth){dirs[depth]=last_dir;}else if(last_depth > depth){dirs[last_depth]="";} last_dir=$NF;last_depth=depth;pdir=".";for (i in dirs){pdir=pdir"/"dirs[i];} gsub("/$","",pdir);$NF=pdir"/"$NF;print}' | sed '$d'

Preparation

$ mkdir test/{aaa,bbb}
$ touch test/aaa/{aaa1,aaa2,aaa3}
$ touch test/bbb/{bbb1,bbb2}
$ touch test/{root1,root2}
$ tree test/
test/
├── aaa
│   ├── aaa1
│   ├── aaa2
│   └── aaa3
├── bbb
│   ├── bbb1
│   └── bbb2
├── root1
└── root2

2 directories, 7 files
$ tree test | awk 'NF{depth=gsub("[│├└]","&",$0); if(last_depth < depth){dirs[depth]=last_dir;}else if(last_depth > depth){dirs[last_depth]="";} last_dir=$NF;last_depth=depth;pdir=".";for (i in dirs){pdir=pdir"/"dirs[i];} gsub("/$","",pdir);$NF=pdir"/"$NF;print}' | sed '$d'
./test
├── ./test/aaa
│   ├── ./test/aaa/aaa1
│   ├── ./test/aaa/aaa2
│   └── ./test/aaa/aaa3
├── ./test/bbb
│   ├── ./test/bbb/bbb1
│   └── ./test/bbb/bbb2
├── ./test/root1
└── ./test/root2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment