LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2013, 08:08 AM   #1
anandg111
Member
 
Registered: Jan 2012
Posts: 56

Rep: Reputation: Disabled
performing arithmatic operations on output of `wc -l`


Hi
I want to perform arithmatic operations on output of `wc -l`.
for example
Code:
user046@sshell ~ $ ls -l
total 0
where "total 0" will increase one line in wc -l
Code:
filecount=`ls -l | wc -l`
here $filecount will be 1 but is should be 0
how to get rid of it ?
 
Old 06-27-2013, 08:10 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Code:
$ mktemp -d
/tmp/tmp.Ogy7kjo9jn
$ ls -l /tmp/tmp.Ogy7kjo9jn/
total 0
$ ls -1 /tmp/tmp.Ogy7kjo9jn
$ ls -1 /tmp/tmp.Ogy7kjo9jn | wc -l
0
 
Old 06-27-2013, 08:13 AM   #3
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
is it for counting files in the directory? then it can be fooled:
Code:
$ mktemp -d
$ touch /tmp/tmp.F1Si9FgHCI/a
$ touch /tmp/tmp.F1Si9FgHCI/$'new\nline'
$ ls -1 /tmp/tmp.F1Si9FgHCI | wc -l
3
 
Old 06-27-2013, 08:24 AM   #4
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 512

Rep: Reputation: 139Reputation: 139
Code:
filecount=$(ls -1 | wc -l)
Counting Files in the Current Directory

Last edited by mddnix; 06-27-2013 at 08:25 AM.
 
Old 06-27-2013, 08:45 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
A simple answer would be, do not process the output of ls ... http://mywiki.wooledge.org/ParsingLs

Instead of correcting the wrong way, maybe identify what you actually want to do and use a better method
 
Old 06-27-2013, 10:41 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by Ygrex View Post
is it for counting files in the directory? then it can be fooled:
Code:
$ mktemp -d
$ touch /tmp/tmp.F1Si9FgHCI/a
$ touch /tmp/tmp.F1Si9FgHCI/$'new\nline'
$ ls -1 /tmp/tmp.F1Si9FgHCI | wc -l
3
Then use " -lC1b".

The C1 is one column, and drops the "Total..." line. The "b" causes special characters in a file name to be escaped.
 
Old 06-28-2013, 11:04 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The proper way to count files in a directory is generally with globbing and an array, at least for simple matches.

Code:
shopt -s dotglob nullglob
files=( * )
echo "There are ${files[@]} files in this directory."
In order to catch hidden files you need to set the dotglob shell option first. Setting nullglob is recommended too, if the pattern matches nothing.

However, what this actually counts is all entries in the directory, including subdirectories. If you actually want simple files only, or some other sub-category, it requires a bit of extra effort to filter them out. Usually you have to loop through them and check each one.

Code:
shopt -s dotglob nullglob
unset f files

for f in *; do
    [[ -f $f ]] && files+=( "$f" )
    [[ -f $f ]] && (( files++ ))    #alternate that just increments a counter
done

echo "There are ${files[@]} files in this directory."  #or just "$files" for the alternate version.
The final option is to use find, and count it's output in some way. You'll probably only want to do this if you need very precise matching ability, however.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] confused pointer arithmatic atlantis43 Programming 13 06-08-2013 11:40 AM
[SOLVED] Arithmatic calculation in shell script shivaa Linux - Newbie 14 10-21-2012 07:30 PM
Accessing and performing operations on Base level kernel structure ankur0921 Programming 5 01-26-2011 05:58 AM
Bash for loop and arithmatic scub@ Programming 1 11-25-2009 03:27 AM
Performing root operations in X such as moving files... pujolasdf Linux - Software 3 03-26-2005 12:55 PM


All times are GMT -5. The time now is 07:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration