Home » Infrastructure » Unix » Not able to include wc -l inside expr
Not able to include wc -l inside expr [message #178891] Fri, 23 June 2006 05:41 Go to next message
janetwilson
Messages: 48
Registered: June 2006
Location: St.Louis
Member
Hi,

I'm not able to include wc -l inside expr

I want to display the number of files & directories inside a directory.

ls -l | wc -l

In my unix machine ls -l display a total.

The result of ls -l may look like this

total 28
-rw-rw-r--    1 oracle   oracle        380 Jun 16 17:44 fifth.sh
-rw-rw-r--    1 oracle   oracle        186 Jun 13 16:38 first.sh
-rw-rw-r--    1 oracle   oracle        222 Jun 15 18:43 fourth.sh
drwxrwxr-x    2 oracle   oracle       4096 Jun 23 15:39 one
-rw-rw-r--    1 oracle   oracle       2275 Jun 13 17:59 second.sh
-rw-rw-r--    1 oracle   oracle        488 Jun 22 17:04 sixth.sh
-rw-rw-r--    1 oracle   oracle        154 Jun 15 15:21 third.sh


I want to avoid the first line (total 28).

ls -l | wc -l shows 8. actually there are 6 files and one directory. A total of 7.

so i changed my code like

ls -l | expr `wc -l -1`

but it is giving error.

Can you please rectify the error.


Thanks & Regards
Janet Wilson
Re: Not able to include wc -l inside expr [message #178896 is a reply to message #178891] Fri, 23 June 2006 05:56 Go to previous messageGo to next message
Frank Naude
Messages: 4565
Registered: April 1998
Senior Member
Try this:

$ ls -l | wc -l | awk '{print ($1 - 1)}'
Re: Not able to include wc -l inside expr [message #178897 is a reply to message #178891] Fri, 23 June 2006 05:57 Go to previous messageGo to next message
raom
Messages: 22
Registered: January 2006
Location: india
Junior Member
see it may work for u

ls -l |wc -l| awk '{print $1-1}'

ooops same answer though

ls -l | grep -v "total" | wc -l

[Updated on: Fri, 23 June 2006 06:02]

Report message to a moderator

Re: Not able to include wc -l inside expr [message #178988 is a reply to message #178897] Fri, 23 June 2006 15:05 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
hp-ux11>>ls -l
total 0
drwxrwxr-x   2 scott     users           96 Jun 23 12:49 dummy_dir
-rw-rw-r--   1 scott     users            0 Jun 23 12:45 file
-rw-rw-r--   1 scott     users            0 Jun 23 12:46 file1
-rw-rw-r--   1 scott     users            0 Jun 23 12:45 file2
-rw-rw-r--   1 scott     users            0 Jun 23 12:46 file3
lrwxrwxr-x   1 scott     users            5 Jun 23 12:46 file3_lnk -> file3

hp-ux11>>ls -l | grep ^-
-rw-rw-r--   1 scott     users            0 Jun 23 12:45 file
-rw-rw-r--   1 scott     users            0 Jun 23 12:46 file1
-rw-rw-r--   1 scott     users            0 Jun 23 12:45 file2
-rw-rw-r--   1 scott     users            0 Jun 23 12:46 file3

hp-ux11>>ls -l | grep ^- | wc -l
4
Re: Not able to include wc -l inside expr [message #179548 is a reply to message #178988] Tue, 27 June 2006 22:29 Go to previous message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
Am I missing something here? If you don't like the TOTAL line at the beginning of 'ls -l', then why do you use the '-l' switch?
ls | wc -l


Ross Leishman
Previous Topic: file checking script
Next Topic: sqlplus from Unix
Goto Forum:
  


Current Time: Mon Nov 30 11:44:37 CST 2020