Solved Bash Script to collect data or use a Phyton script?

January 16, 2014 at 05:39:12
Specs: Suse 11
Need to write a script to collect data from a filesystem directory and subdirectorys and have a count of all "wc -l" and a break down of files, ie "name of file", "count of files", "total count of size and then break down to "the count of size *4/60". I was told to use phyton, rather than a bash script (which I am having syntax issues with). And you help?

See More: Bash Script to collect data or use a Phyton script?

Report •

✔ Best Answer
January 17, 2014 at 20:53:08
First, you're find syntax is not quite right. Use the -type f option says to find only the file objects. If any directory objects exist, they are ignored.

Second, use a while loop instead of the for loop. The for loop fails if there is more than one object per line in file /tmp/list1. Plus the while loop is more efficent:

 

#!/bin/bash

targetDir="yourdir"
find $targetDir -type f |wc -l

while read i
do
   find $targetDir/*.$i -type f |xargs ls -l|awk ' { printf "%d %s\n", $5, $9 } '
done < /tmp/list1
# end script

Let me know if you have any questions

message edited by nails



#1
January 16, 2014 at 07:19:57
You can use just about any language to accomplish this task; shell (bash, sh, ksh, etc), python, perl, php, ruby, etc.

Which language do you know best?


Report •

#2
January 16, 2014 at 12:32:33
Would really prefer bash. I have used csh for years. Loved using the foreach statements, but with bash, it has been a learning curve. I want a script that I can run and get the quantity of files but also the total amount of sized used (MG).

my script is something like this:

# Total amount of files
$targetDir=/directory
echo Total number of files in $targetDir
find $targetDir | wc -l

# Variable of i is a list of files extensions I am trying to find. ($2) is the size ($1) is the
# number of files
for i in ‘cat /tmp/list1’
echo Total for $i
find /directory -type f ‘*${i}*’ |xargs -ld ; awk -F “ “ {print $2} {print $9}; sum /n


Report •

#3
January 16, 2014 at 12:54:13
Sorry, I don't don't do shell scripting so I'll defer to others that are knowledgeable in that area.

message edited by FishMonger


Report •

Related Solutions

#4
January 17, 2014 at 20:53:08
✔ Best Answer
First, you're find syntax is not quite right. Use the -type f option says to find only the file objects. If any directory objects exist, they are ignored.

Second, use a while loop instead of the for loop. The for loop fails if there is more than one object per line in file /tmp/list1. Plus the while loop is more efficent:

 

#!/bin/bash

targetDir="yourdir"
find $targetDir -type f |wc -l

while read i
do
   find $targetDir/*.$i -type f |xargs ls -l|awk ' { printf "%d %s\n", $5, $9 } '
done < /tmp/list1
# end script

Let me know if you have any questions

message edited by nails


Report •

Ask Question