Get number of elements in a list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get number of elements in a list
# 1  
Old 07-06-2011
Get number of elements in a list

Hi all I would like to know the number of elements in a list. $list=`ls xyz*` I want to get the number of files xyz* in the folder. Anybody please help!!!
# 2  
Old 07-06-2011
Code:
ls xyz* | wc -l

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 07-06-2011
Or:
Code:
echo "$list"| wc -l


Last edited by Franklin52; 07-06-2011 at 05:52 AM.. Reason: Adding quotes
# 4  
Old 07-06-2011
Quote:
Originally Posted by Franklin52
Or:
Code:
echo $list| wc -l

@franklin,

the $list needs to be double quoted right ?

Code:
 
$ list=`ls *.mk`
 
$ echo $list | wc -l
       1
 
$ echo "$list" | wc -l
       2
 
$ ls *.mk
abc.mk  efg.mk
 
$ echo "$list"
abc.mk
efg.mk
 
$ echo $list
abc.mk efg.mk

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 07-06-2011
Just to ensure that files are listed one per line
Code:
ls -1 xyz* | wc -l

Note that the ls command return a display that is ascii sorted by default

In some case, when you have numerous files, you may want to skip that sorting operation (in order to speed up processing) and diplay files as soon as they are encountered in the directory structure (use the -f option of ls)

Code:
ls -f | wc -l


Last edited by ctsgnb; 07-06-2011 at 05:58 AM..
# 6  
Old 07-06-2011
Quote:
Originally Posted by itkamaraj
@franklin,

the $list needs to be double quoted right ?
Right, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

Issue with the incorrect number of array elements

Hello , I have a file : RestartSession.txt with the below contents : Backup p203pcrw01_OS_Weekly Failed full 10/11/2015 10:00:07 PM 1444572007 10/11/2015 10:26:23 PM 1444573583 0:00 0:26 18.76 1 08 0 0 0 2 2 180668 ... (4 Replies)
Discussion started by: rahul2662
4 Replies

3. Shell Programming and Scripting

Number of elements, average value, min & max from a list of numbers using awk

Hi all, I have a list of numbers. I need an awk command to find out the numbers of elements (number of numbers, sort to speak), the average value the min and max value. Reading the list only once, with awk. Any ideas? Thanks! (5 Replies)
Discussion started by: black_fender
5 Replies

4. UNIX for Dummies Questions & Answers

make ls retrive ordered list of elements

Hello friends!! I have a question regarding the use of ls in unix. I have a folder with files: t1.txt t2.txt t3.txt t4.txt ... t10.txt When I make an ls I always get: t10.txt t1.txt t2.txt t3.txt .. t9.txt (2 Replies)
Discussion started by: SaktiPhoenix
2 Replies

5. Shell Programming and Scripting

To get Port number alone from the list

~]#netstat -vatn | grep LISTEN tcp 0 0 0.0.0.0:34895 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN ... (7 Replies)
Discussion started by: linuxadmin
7 Replies

6. Shell Programming and Scripting

Number of elements in Word list

Hello everyone, can anyone let me know if there is a way to get the count of elements in a word list that I use for a for loop in the way: for single_result in $results ; do ....... I know I can increment a counter in my for loop, but would there be a way to know the total number of elements in... (4 Replies)
Discussion started by: gio001
4 Replies

7. Shell Programming and Scripting

eval, array, number of elements - couldn't get it

I try to get a number of elements in an array, using dynamic array name. I need the array name to be dynamic. The array name is constructed as 'inf_ln_$nmb', where $nmb is a file line number So, say I have the arr 'inf_ln_4': > for (( el=0; el<${#inf_ln_4}; el++ )); do > echo "$el:... (1 Reply)
Discussion started by: alex_5161
1 Replies

8. Shell Programming and Scripting

How to extract elements in a field using a number

Hi, I face difficulty where the number which I grep before I would like to use it as number to grep again in another file. For example in file 1, I extract the second field and assign to variable "char" in a while loop. And then, I grep again this char to get i and j. char=`echo "${LINE}"|... (17 Replies)
Discussion started by: ahjiefreak
17 Replies

9. AIX

How to list mirrored elements? (pv, vg, lv)

Hello, How can I list mirrored elements, such as PV, VG or LV? (and others if applicable). On AIX 4.3.3 and 5.3.0. I tried the commands: lscfg|grep hd lspv lsvg lsvg -l (of each vg) lslv (and some options) But could not find what am I looking for. I saw a "number of copies", but I am... (4 Replies)
Discussion started by: cactux
4 Replies

10. Shell Programming and Scripting

a list of number

Hi, I wrote a shell program and I need to only accept a list of numbers from 01 to 24 in my argument 1 ($1) before the program can continue. Does anyone know a better way to do it beside use the case command with 24 lines of condition? Thanks in advance! (6 Replies)
Discussion started by: whatisthis
6 Replies
Login or Register to Ask a Question