Find the answer to your Linux question:
Results 1 to 6 of 6
Everyone, I'm looking for some suggestions. I've been searching for some software that will inventory USB drives and break down the contents by directory and owner. I'd like to be ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2013
    Posts
    3

    File inventory for multiple USB disks


    Everyone,

    I'm looking for some suggestions. I've been searching for some software that will inventory USB drives and break down the contents by directory and owner. I'd like to be able to search by file type/owner per disk. Any ideas?

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Hello and welcome!

    It seems to me that the best approach would be a shell script. You should be able to do most of it with udevinfo, find and bash built-in functions. Here's a quick example:

    Code:
    #!/bin/bash
    
    #find all drives
    drives=$(find /sys/block -mindepth 1 -maxdepth 1 -name 'sd*' -printf '%f\n')
    
    # loop thru all drives found
    for drive in $drives; do
    
      # block device name (sda, sdb, etc.)
      printf "\nDrive: $drive\n"
    
      # bus type (ata, usb, etc.)
      bus_type=$(udevadm info --query=all --path /block/$drive|grep -i BUS)
      echo Bus type $bus_type
    
      # disk make/manuf/model info
      printf "Vendor/Model info: "
      udevadm info --query=all --path=/block/$drive|egrep 'VENDOR|MODEL'
    
      # serial number info
      printf "Serial Number info: "
      udevadm info --query=all --path=/block/$drive|egrep SERIAL
    
      # mounted partitions
      printf "Mounted directories:\n"
      dirs=$(df|awk "\$1 ~ /$drive/{print \$6}")
      for dir in $dirs; do
        printf "\tdir \"$dir\"\n"
        # now run some "find" commands on these dirs...
      done
    done

  3. #3
    Just Joined!
    Join Date
    Jan 2013
    Posts
    3
    I'm looking for something more in-depth than just a shell script. Maybe some thing with an sql backend maybe?

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by chknfrdstk View Post
    I'm looking for something more in-depth than just a shell script. Maybe some thing with an sql backend maybe?
    It sounds like you have more in mind for your task than you initially reported. What is supposed to go into this db? You could certainly create a MySQL db and a table and populate it with data culled from your usb-program. But what exactly do you want to track in your db? And more importantly, why must you use/expect a database?

  5. #5
    Just Joined!
    Join Date
    Jan 2013
    Posts
    3
    What would be nice to find is a program that could take a disk (identified by s/n or blkid or some other unique identifier or user input) and build a db (flat file or SQL) based on the contents of that disk. The data could then be searched/sorted based on owner, group, size or date.

    The disk could be put into storage somewhere and when a file is needed the program can be used to find which disk the file is on.

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by chknfrdstk View Post
    What would be nice to find is a program that could take a disk (identified by s/n or blkid or some other unique identifier or user input) and build a db (flat file or SQL) based on the contents of that disk. The data could then be searched/sorted based on owner, group, size or date.

    The disk could be put into storage somewhere and when a file is needed the program can be used to find which disk the file is on.
    I see. This still sounds like a simple roll-your-own job to me, using a shell script and MySQL.

    I would create a database, e.g.:

    Code:
    mysqladmin -p create filelisting
    then create a table, e.g.:
    Code:
    mysql -p filelisting -e 'create table files (id INT(6) NOT NULL, diskid VARCHAR(30), filename VARCHAR(30), pathname VARCHAR(150), PRIMARY KEY(id))'
    this table has four fields:
    1) unique ID
    2) disk ID (of USB disk, etc.)
    3) filename
    4) above filename's absolute path

    then write a bash script that finds all files in a dir, for a specific disk, and write each file as a record into the database, e.g.:
    Code:
    #!/bin/bash
    
    # unique file UID (there are better ways to do this)
    fuid=0
    
    # id of USB disk
    duid='samsung_123'
    
    # file all files in given dir
    for file in $(find /dir /-type f); do
    
      # get file and path nae
      filename=$(basename $file)
      pathname=$(dirname $file)
    
      # insert record into db
      mysql -p filelisting -e 'INSERT INTO files VALUES('$fuid','$duid','$filename','$pathname''
    
      # one-up the unique file id
      fuid+=1
    done
    That code is untested, unoptimized, etc. , but gives you the general idea.

    To find a file by name (let's say the name is in the variable $myfile), you could query the db like:
    Code:
    mysq -p filelisting -e "SELECT diskid FROM files WHERE filename = '$myfile'"
    and that would return the disk ID(s) of the disk(s) containing the file by that name. There are various ways you could tailor your query, of course. You could also easily alter the MySQL tables to contain extra fields reflecting file metadata (user id, group id, etc.).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •