LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 01: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, 01:53 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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 01:54 PM.
 
1 members found this post helpful.
Old 06-14-2012, 04: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, 08:49 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 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
Open Source Consulting | Domain Registration