Selecting Numerical Files In Bash

Put your programming skills to the test in these challenges.

Selecting Numerical Files In Bash

Post by jadecook on Sun Oct 19, 2014 12:53 pm
([msg=84241]see Selecting Numerical Files In Bash[/msg])

I want to move files from one folder to another that are in order from lowest to highest. For example:

Code: Select all
IMG_3276.CR2
IMG_3277.CR2
IMG_3278.CR2
IMG_3279.CR2
IMG_3280.CR2


I'd like my program to read something like this:

Code: Select all
ENTER PHOTO START: 3276
ENTER PHOTO END: 3280


Then it selects all the in between and moves them to a folder of my choosing, is this possible?

Don't mind using another language, this just seemed right for what I'm trying to do.
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 78
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Selecting Numerical Files In Bash

Post by cyberdrain on Sun Oct 19, 2014 2:54 pm
([msg=84244]see Re: Selecting Numerical Files In Bash[/msg])

Hmm, interesting problem. This could easily be done with a for loop. Use ls to sort the files correctly and then you could use grep/sed/cut with mv/cp to move/copy them around. Might not be efficient, but it will probably work. I don't feel like writing a script, but there's probably tutorials on that.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Selecting Numerical Files In Bash

Post by limdis on Sun Oct 19, 2014 5:29 pm
([msg=84249]see Re: Selecting Numerical Files In Bash[/msg])

I've done a lot of this when manipulating my dictionaries. But that is with words not files. Also I did not have a range I was splitting by size. Shouldn't be too hard. I'll write up something when I get a chance later tonight hopefully. Ninjex I'm waiting for some ruby foo from you :geek:
"The quieter you become, the more you are able to hear..."
"Drink all the booze, hack all the things."
User avatar
limdis
Moderator
Moderator
 
Posts: 1657
Joined: Mon Jun 28, 2010 5:45 pm
Blog: View Blog (0)


Re: Selecting Numerical Files In Bash

Post by -Ninjex- on Sun Oct 19, 2014 6:19 pm
([msg=84252]see Re: Selecting Numerical Files In Bash[/msg])

If you are good with regular expressions, or would like to start learning it on the go.
I recommend changing from bash to the zsh shell. It has extremely powerful globing and can make things like this easier.

As for the code:

Code: Select all
#!/bin/bash

echo -n 'Start Photo: '
read start
echo -n 'End Photo: '
read endl

for i in $(seq $start $endl)
do
  echo "IMG_$i.CR2"
done


Just change the echo in the for loop to do what you want with the images. I'll let you figure out how to change this so that you can pipe the output into other useful commands <3

-- Mon Oct 20, 2014 12:05 pm --

limdis wrote:Ninjex I'm waiting for some ruby foo from you :geek:


Sure, just for you...

Code: Select all
#!/usr/bin/ruby

print "Start Photo: "
start = gets.chomp.to_i
print "End Photo: "
endl = gets.chomp.to_i

(start..endl).to_a.map { |i| puts %x( echo IMG_#{i}.CR2 ) }
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Selecting Numerical Files In Bash

Post by jadecook on Sun Oct 19, 2014 8:29 pm
([msg=84253]see Re: Selecting Numerical Files In Bash[/msg])

Got what I needed done by using this code.

Code: Select all
echo -n "enter photo start: "
read photoSTART
echo -n "enter photo end: "
read photoEND
echo -n "enter folder id: "
read stockNumber
processID=$photoSTART
while [ $processID -le $photoEND ]
do
    echo "moving: /IMG_$processID.CR2"
    cp "/IMG_$processID.CR2" "/newLocation"
    processID=$(( processID+1 ))
done
echo "Phew, glad thats done."
User avatar
jadecook
Experienced User
Experienced User
 
Posts: 78
Joined: Fri Aug 17, 2012 2:20 pm
Blog: View Blog (0)


Re: Selecting Numerical Files In Bash

Post by -Ninjex- on Sun Oct 19, 2014 11:36 pm
([msg=84255]see Re: Selecting Numerical Files In Bash[/msg])

jadecook wrote:Got what I needed done by using this code.


Yeah, ummm about that
Code: Select all
cp "/IMG_$processID.CR2" "/newLocation"

s//newlocation/newlocation/
and why are you calling that variable processID lol

Also, take into consideration what I suggested. It would be nice of you.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests