0

I often create small projects as separate directories in one big ~/code directory. However, with time I forget what are those directories about, and unfortunately, dirname isn't good enough to decipher the use case.

Is there a way to augment ls (or alias to another utility) which will also show short info about each project? I envision it to show a few lines from README.md in the directory of a project alongside with dir name.

1
  • You have a tool-box. You should be able to do it in a two line shell script (3 if you include #!/bin/bash, in the count). Dec 21, 2020 at 9:34

1 Answer 1

0

There is no such tool; but this is UNIX, and so you can easily create one. :-)


Not having any README.md files around, I'll use .gitignore instead, as the file under each subdirectory to print the first line from:

find . -type d -printf '%30f ==> ' -exec sed q {}/.gitignore \;

Sample output:

                   control-DBs ==> autogen.h
                         basic ==> flarp.exe
                    postgresql ==> libpgstore.a
                            ui ==> .DS_Store
                       plugins ==> *.*~

How it works

  • -type d : We are looking for subdirectories only.
  • printf '%30f ==> ' : The -printf accepts a bunch of codes, for printing various information about the file (technically speaking, a directory is a kind of file) being processed. There are enough codes to emulate output of ls(1) with the simpler options. See man find or https://man7.org/linux/man-pages/man1/find.1.html for the complete list; a few must-haves are
    • %f: basename
    • %s: size in bytes
    • %m: permission (a.k.a. mode bits)
  • -exec sed q {}/.gitignore \; : Here we give find(1) a custom command to execute for each subdirectory being processed.
    • {} stands for the file's path
    • sed q fff is one way to print the first line of file "fff"
    • \; tells find(1) where the custom command ends.

What if only some subdirectories have .gitignore in them?

find . -type d -exec [ -e {}/.gitignore ] \; -printf '%30f ==> ' -exec sed q {}/.gitignore \;

We again turn to -exec for help. Say the subdirectory being currently processed is called "ddd"; then [ -e ddd/.gitignore ] will evaluate to false if there is no .gitignore in ddd/. As soon as a test evaluates to false, find(1) gives up and moves on to the next subdirectory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .