LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-14-2012, 02:33 PM   #1
gnlogic
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
need bash command help


Hello everyone. I am new to bash shell scripting and I have this problem I need to solve

Basically I have around 50,000 or so files in a directory of the format <department id>-<date>-<time>-<file id>.<ext>

I need to write a command that will output a list of department ID's and their counts as well as a count of files without department ID's.

I am stuck how to make this as a one command that won't break. Any help is greatly appreciated
 
Old 06-14-2012, 02:53 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
With that many files using flags with ls becomes a problem.

However you can usually use ls itself.

Counts can be gotten with the wc command. "wc -l" shows number of lines. (Type "man wc" for more details of the command.)

Count of files irrespective of department ID is simple:

Code:
ls |wc -l
Count of department IDs and their files becomes a bit more problematical but you can do it with a loop:

Code:
for DEPID in $(ls -R |awk -F\- '{print $1}' |sort -u)
do DEPCNT=$(ls ${DEPID}-* |wc -l)
   echo Department ID $DEPID has $DEPCNT files
done
Putting it all together in one script:

Code:
#!/bin/bash
echo Total files is" $(ls |wc -l)
echo Break down by Department ID follows:
for DEPID in $(ls -R |awk -F\- '{print $1}' |sort -u)
do DEPCNT=$(ls ${DEPID}-* |wc -l)
   echo Department ID $DEPID has $DEPCNT files
done

Last edited by MensaWater; 06-14-2012 at 02:54 PM.
 
1 members found this post helpful.
Old 06-14-2012, 05:28 PM   #3
gnlogic
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MensaWater View Post
With that many files using flags with ls becomes a problem.

However you can usually use ls itself.

Counts can be gotten with the wc command. "wc -l" shows number of lines. (Type "man wc" for more details of the command.)

Count of files irrespective of department ID is simple:

Code:
ls |wc -l
Count of department IDs and their files becomes a bit more problematical but you can do it with a loop:

Code:
for DEPID in $(ls -R |awk -F\- '{print $1}' |sort -u)
do DEPCNT=$(ls ${DEPID}-* |wc -l)
   echo Department ID $DEPID has $DEPCNT files
done
Putting it all together in one script:

Code:
#!/bin/bash
echo Total files is" $(ls |wc -l)
echo Break down by Department ID follows:
for DEPID in $(ls -R |awk -F\- '{print $1}' |sort -u)
do DEPCNT=$(ls ${DEPID}-* |wc -l)
   echo Department ID $DEPID has $DEPCNT files
done
Worked out beautifully, thank-you very much for your help
 
Old 06-15-2012, 09:49 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Glad I could help. Please go to thread tools and mark this as Solved as it helps others find solutions more quickly when doing web searches in future.
 
  


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] How write a literal bash command in a bash file? xeon123 Linux - Newbie 6 11-29-2010 01:05 PM
Bash history delete command from bash itself ashishag Linux - Software 6 05-02-2010 04:39 AM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 12:21 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Automatically append another piped command to issued command in bash amateen Programming 1 05-07-2009 07:36 AM


All times are GMT -5. The time now is 08:20 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