listing unique file prefixes in a directory Linux

Posted on 2016-09-28
Medium Priority
Last Modified: 2016-10-24
Hi guys,
I'm trying to achieve the following.
I have a folder with the following path: /home/service
in this folder I'll have many files all with the same format

and etc and the order can be randomized each time.
each of those files has a simple integer in them. and the prefix (user#) corresponds to a certain in user a DB.
basically what I need to do, is to read the current integer from the DB for the selected user (I already have the command for that, it's tested and working) and then. to add to it ALL the integers of files corresponding to that user and write the new total (meaning current + sum of all integers of said user) back into the DB (The writing is done by calling a python script that accepts the user name and the total as arguments)

how would I go about achieving the following?

The script I have at the moment isn't working so well.

files=$(find /home/service/ -type f |grep -v "bash" | sed 's#.*/##')
for file in $files
  	user=$(echo $file | awk -F'[.]' '{print $1}')
        curr_traffic=$(echo "select traffic from campaigns.Campaigns where UserName='$user';" | mysql -N -u ##### -p#####)
        let total=$total+$curr_traffic
        echo "pre DB write total is $total"
        ufiles=$(find /home/service/ -type f |grep -v "bash" |sed 's#.*/##' |grep $user)
        for ufile in $ufiles
          	traffic=$(cat /home/service/$ufile)
                let total=$total+$traffic
	echo "Updating the traffic for $user as $total"
        rm -f /home/service/$user.*
       	#./update_statistics.py $user $total

Open in new window

Question by:David Sankovsky
1 Comment

Accepted Solution

Yuri G earned 2000 total points
ID: 41819968
function a {
    let total=0
    for ffile in /home/service/$1.*
      if [ -f $ffile ]; then let total=total+$(cat $ffile); fi
      echo "Total: $total"
      echo "Running update MYSQL for user: $1 with traffic: $total"
      rm -f /home/service/$1.*
for files in /home/service/*
      username=$(echo $files | cut -f4 -d"/"| cut -f1 -d".")
      if [ -f $files ]; then a $username; fi

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension . This reminded me of questions that come up here at EE along the lines of, "How can I tell the type of file from its cont…
Can you run Linux on a Windows system?  Yep.  Here's how.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

586 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question