LinuxQuestions.org
Review your favorite Linux distribution.
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 02-26-2016, 01:39 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Rep: Reputation: 131Reputation: 131
How to count certain files in a directory?


I'm trying to figure out if I have all of the .sql files in a directory, if they are in a .tar file that I created.

If I run:
Code:
ls -al | grep *.sql | wc -l
This always returns 0, however I need it to match:

Code:
tar -tf archive.of.some.project.tar.gz | wc -l
25

Is there a way to do this either with ls or find?

thanks

Last edited by JockVSJock; 02-26-2016 at 01:47 PM.
 
Old 02-26-2016, 01:46 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
The grep is unnecessary and not doing what you are intending it to do: it is looking for regular expression "*.sql" in the output of ls -al, not matching the files *.sql, instead you can try
Code:
ls -al *.sql | wc -l

Last edited by thesnow; 02-26-2016 at 01:52 PM. Reason: added regular expression clarification
 
1 members found this post helpful.
Old 02-26-2016, 03:05 PM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,166
Blog Entries: 4

Original Poster
Rep: Reputation: 131Reputation: 131
Thanks for the help.


So what is the difference between *.sql and "*.sql". Does the quote marks give it a regular expression that can't be piped?
 
Old 02-26-2016, 03:32 PM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
If you use "set -x", you can get an idea of what it's doing. Unquoted, it will be treated as a glob, quoted as a regular expression or literal.

Code:
user@host [-] ~ $ cat file
+ cat file
abc
123
abc 123
*
* abc

user@host [-] ~ $ grep * file
+ grep Desktop dev Documents Downloads file firefox linuxquestions Music Pictures Public samba temp Templates Videos file
grep: dev: Is a directory
grep: Documents: Is a directory
grep: Downloads: Is a directory
grep: firefox: Is a directory
grep: linuxquestions: Is a directory
grep: Music: Is a directory
grep: Pictures: Is a directory
grep: Public: Is a directory
grep: samba: Is a directory
grep: temp: Is a directory
grep: Templates: Is a directory
grep: Videos: Is a directory

user@host [-] ~ $ grep "*" file
+ grep '*' file
*
* abc


user@host [-] ~ $ grep "*." file
+ grep '*.' file
* abc

user@host [-] ~ $ grep ".*" file
+ grep '.*' file
abc
123
abc 123
*
* abc
If file contained something like "123.abc", it would not be matched by *.abc nor "*.abc" since the * is being treated literally, not as a wildcard, and the . is matching one or more characters (expression).

Last edited by thesnow; 02-26-2016 at 03:35 PM. Reason: sloppy/paste
 
  


Reply

Tags
find, ls, tar, wc


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] Script count files in the directory but keep loop not exit untill see 10 files. dotran Linux - Newbie 9 12-22-2014 05:34 AM
C programming question Word Count for all files in a directory AlexGonzalez Programming 1 04-07-2011 09:02 PM
write a program to count files within a directory bio71 Programming 1 10-03-2008 06:36 PM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 07:22 AM
how to get count of files in a directory? hongxing Linux - Software 2 11-10-2005 10:06 PM


All times are GMT -5. The time now is 10:57 PM.

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