Is there a Unix utility to restrict a file or directory listing to given minimum and maximum number of levels deep?
I have a git command which displays a unique listing of the directories where files have changed,git diff --name-only HEAD~3 HEAD~0 | sed 's|/[^/]*$||' | uniq
and I want to limit the output further to those a certain number of levels deep. Is there a way to accomplish that?
Here is an example from a Drupal
installation. After I install or update modules the git command shows which modules were added or changed. The module directory is the 4th level so any deeper directories should be cut off and duplicates should be removed from the 4th level directoris
sites/all/modules/table_trash
sites/all/modules/table_trash/css
sites/all/modules/table_trash/drush
sites/all/modules/table_trash/js
sites/all/modules/table_trash/libraries
sites/all/modules/table_trash/libraries/variants/js
sites/all/modules/table_trash
sites/all/modules/video_filter
sites/all/modules/views_aggregator
sites/all/modules/views_aggregator/views
sites/all/modules/views_aggregator
sites/all/modules/views_aggregator/views_aggregator_more_functions
sites/all/modules/views_php
sites/all/modules/views_php/plugins/views
sites/all/modules/views_php
sites/all/modules/views_watchdog
sites/all/modules/views_watchdog/views/handlers
sites/all/modules/views_watchdog/views/plugins
sites/all/modules/views_watchdog/views/theme
sites/all/modules/views_watchdog/views
sites/all/modules/views_watchdog
It appears to be a general case of applying grep command which strips out all lines after the Nth appearance of a separator character, '/' in this instance, but I am looking for something that also removes duplicates from the output, which is why in this case I need to pass it through the uniq
command. In this case both the maximum and minimum number of levels should be 4.
mawk
being the defaultawk
on Ubuntu. If you have the ability to install GNUawk
(akagawk
), you might want to do that and give it a twirl – iruvar Feb 09 '15 at 14:22