LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2006, 11:31 AM   #1
BlueNoteMKVI
LQ Newbie
 
Registered: Jun 2004
Distribution: Kubuntu/CentOS
Posts: 18

Rep: Reputation: 0
Help with shell script and wildcards


In my shop we have security cameras that save images to one of our servers. The images are stored in this format:
HH_MM_SS.jpg

We end up with 2-3 images per second stored on the server during busy times. While having such a detailed record is great, it bogs things down when I try to browse through the pictures to see what was going on at a given time. I wrote this script to get the first image from the first second of every minute during the day and copy it to another folder:
Code:
cd $1 #change directory to the first argument given
LIMIT=23 # 24 hours in a day
for ((a=0; a <= LIMIT ; a++))
do
  #pad the string if need be
  if [ ${#a} -eq 1 ]
  then
    b=0$a;
  else
    b=$a
  fi
  #make a directory if necessary
  if [ -e "$b" ]
  then
    echo $b exists
  else
    mkdir $b
  fi
  echo $b\*
  # move all files from this hour to the hour's directory
    mv $b\* $b/
The goal here is to have 24 directories, numbered 00 to 23, one for each hour. The images from that hour would then go into that directory. The trouble is the "mv" line - I get this error:
Code:
mv: cannot stat `23*': No such file or directory
What I'm trying to do is something like this:
Code:
mv 23* 23/
If I use that on the command line, all files that start with 23 would move to the directory ./23/ - but that doesn't work in the script. What am I doing wrong?
 
Old 10-03-2006, 11:46 AM   #2
Markie1006
LQ Newbie
 
Registered: Oct 2006
Distribution: Slackware
Posts: 12

Rep: Reputation: 0
Quote:
Originally Posted by BlueNoteMKVI
What I'm trying to do is something like this:
Code:
mv 23* 23/
You're escaping the wildcard so the shell is looking for the literal '*' character.

try this instead;-

Code:
mv ${b}* $b/
You'll probably need to send stderr to /dev/null to supress the annoying 'cannot move to a subdirectory of itself' message.

In fact you don't need to mess around checking if the directory exists first & creating as needed - just always attempt to create it anyway.

Code:
(mkdir $b; mv ${b}* $b/) 2>/dev/null
If it already exists, it will get ignored.

Last edited by Markie1006; 10-03-2006 at 11:47 AM.
 
Old 10-03-2006, 12:31 PM   #3
BlueNoteMKVI
LQ Newbie
 
Registered: Jun 2004
Distribution: Kubuntu/CentOS
Posts: 18

Original Poster
Rep: Reputation: 0
That did the trick - thanks!
 
Old 10-04-2006, 07:18 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
a bit simpler?

Code:
ls  ??_??_??.jpg | while read file;do

    dir=${file%_??.jpg}
    [ -d "$dir" ] || mkdir $dir
    echo mv $file $dir
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration