LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need bash command help (https://www.linuxquestions.org/questions/linux-newbie-8/need-bash-command-help-4175411480/)

gnlogic 06-14-2012 01:33 PM

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

MensaWater 06-14-2012 01:53 PM

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


gnlogic 06-14-2012 04:28 PM

Quote:

Originally Posted by MensaWater (Post 4703359)
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

MensaWater 06-15-2012 08:49 AM

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.


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