Solved

listing unique file prefixes in a directory Linux

Posted on 2016-09-28
1
36 Views
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
user1.node1
user1.node2
user2.node4
user1.node3

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
do
  	user=$(echo $file | awk -F'[.]' '{print $1}')
        total=0
        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
        do
          	traffic=$(cat /home/service/$ufile)
                let total=$total+$traffic
        done
	echo "Updating the traffic for $user as $total"
        rm -f /home/service/$user.*
       	#./update_statistics.py $user $total
done

Open in new window

0
Comment
Question by:David Sankovsky
1 Comment
 
LVL 1

Accepted Solution

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now