sos12
sos12

Reputation: 719

List all files with file count as one of the output columns in $BASH?

Is there a way to show files similarly to ls -al that would also also show the file count of the directories listed? Sort of like an ls -al with ls -1 | wc -l as the final column? I've tried switching arguments out, and have pretty much given up on a pipe because I hit syntax errors whenever I try to manipulate the results much. Separately, they're golden, so I feel like I'm missing something obvious. A way to modify ls so it would also show file count of directories that it lists seems like it should be, at least. Does anyone know of a way to get this to work?

Upvotes: 0

Views: 73

Answers (2)

Reinstate Monica Please
Reinstate Monica Please

Reputation: 11603

I think something like this would be closer to what you want

> mkdir testdir && cd testdir && touch a && ln -s a b && mkdir c && touch c/{1..10}
> shopt -s dotglob; for i in *; do [[ -d $i ]] && paste <(ls -ld "$i") <(find "$i" -mindepth 1 | wc -l) || ls -l "$i"; done 
-rw-rw-r-- 1 user user 0 Jul  8 00:04 a
lrwxrwxrwx 1 user user 1 Jul  8 00:04 b -> a
drwxrwxr-x 2 user user 4096 Jul  8 00:04 c/ 10

Upvotes: 0

user3442743
user3442743

Reputation:

Directories

ls -al | awk '/^d/{d++}{print}END{print "Directories: "d}'

All files

ls -al | awk '{print}END{print "Files:" NR}'

Upvotes: 2

Related Questions