How do I list files in a folder showing name, time and number of rows in a line with bash?

StackOverflow https://stackoverflow.com/questions/10439163

  •  05-06-2021
  •  | 
  •  

Domanda

How do I merge ls with wc -l to get the name of a file, modification time and number of rows in a file?

thanks!

È stato utile?

Soluzione

There are a number of ways you can approach this from the shell or your programming language of choice, but there's really no "right" way to do this, since you need to both stat and read each file in order to form your custom output. You can do this without pipelines inside a basic for-loop by using command substitution:

custom_ls () {
    for file in "$@"; do
        echo "$file, $(date -r "$file" '+%T'), $(wc -l < "$file")"
    done
}

This will generate output like this:

$ custom_ls .git*
.gitconfig, 14:02:56, 44
.gitignore, 17:07:13, 21

There are certainly other ways to do it, but command substitution allows the intent of the format string to remain short and clear, without complex pipelines or temporary variables.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top