Monday, October 10, 2011

Counting lines from ls

I needed to count how many directories where in a directory and print the directory number in the output. It was easily done like this:

$ ls -1 | cat -n
1 CBMessageGenerator
2 CRFeedback
3 CRHelpProvider
4 CRhelp
5 CellDiscrepancyManager
6 ConformanceChecker
7 ControlRoomDeskTop_VFI
8 DDSEmailLog
9 DTOutputFileBuilder
10 DTReportServer
11 DatabaseInstallScripts
12 DynoDBLib
13 DynoIntelliShed
14 DynoMail
15 FTPPoll
16 FTPPusher
17 HeartBeat
18 IPBrokerConfig
19 ReportBuilder2.0
20 ReportBuilder2.1
21 ReportingFramework1.1
22 Scheduler_jqf
23 StaticReportGenerator
24 StaticReportNotificationsEmailGenerator
25 SwarmDDS
26 SwarmDDSProcessManager
27 SwarmDTChasis
28 SwarmDTChasis_VFI
29 SwarmDTLoader
30 SwarmDTProcessServer
31 libDynoMail

ls | cat -n
gives the same result.

2 comments:

Triffid Hunter said...

wc -l will give just the total if you prefer, handy for scripts

OuZo said...

I needed to print the project number next to the project name. Thanks