<p>I have 16,000 jpg's from a webcan screeb grabber that I let run for a year pointing into the back year. I want to find a way to grab every 4th image so that I can then put them into another directory so I can later turn them into a movie. Is there a simple bash script or other way under linux that I can do this.</p>

<p>They are named like so......</p>

<p>frame-44558.jpg</p>

<p>frame-44559.jpg</p>

<p>frame-44560.jpg</p>

<p>frame-44561.jpg</p>

<p>Thanks from a newb needing help.</p>

<hr>

<p>Seems to have worked.
Couple of errors in my origonal post. There were actually 280,000 images and the naming was.
/home/baldy/Desktop/webcamimages/webcam_2007-05-29_163405.jpg
/home/baldy/Desktop/webcamimages/webcam_2007-05-29_163505.jpg
/home/baldy/Desktop/webcamimages/webcam_2007-05-29_163605.jpg</p>

<p>I ran.
cp $(ls | awk '{nr++; if (nr % 10 == 0) print $0}') ../newdirectory/</p>

<p>Which appears to have copied the images. 70-900 per day from the looks of it.</p>

<p>Now I'm running
 mencoder mf://*.jpg -mf w=640:h=480:fps=30:type=jpg -ovc lavc -lavcopts vcodec=msmpeg4v2 -nosound -o ../output-msmpeg4v2.avi</p>

<p>I'll let you know how the movie works out.</p>

<p>UPDATE: Movie did not work. 
Only has images from 2007 in it even though the directory has 2008 as well.
webcam_2008-02-17_101403.jpg  webcam_2008-03-27_192205.jpg
webcam_2008-02-17_102403.jpg  webcam_2008-03-27_193205.jpg
webcam_2008-02-17_103403.jpg  webcam_2008-03-27_194205.jpg
webcam_2008-02-17_104403.jpg  webcam_2008-03-27_195205.jpg</p>

<p>How can I modify my mencoder line so that it uses all the images?</p>

[{'answer_id': 71798, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 1, 'selected': False, 'text': "<p>An easy way in perl (probably easily adaptable to bash) is to glob the filenames in an array then get the sequence number and remove those that are not divisible by 4</p>\n\n<p>Something like this will print the files you need:</p>\n\n<pre><code>ls -1 /path/to/files/ | perl -e 'while (&lt;STDIN&gt;) {($seq)=/(\\d*)\\.jpg$/; print $_ if $seq &amp;&amp; $seq % 4 ==0}'\n</code></pre>\n\n<p>You can replace the print by a move...</p>\n\n<p>This will work if the files are numbered in sequence even if the number of digits is not constant like <code>file_9.jpg</code> followed by  <code>file_10.jpg</code> )</p>\n"}, {'answer_id': 71808, 'author': 'DustinB', 'author_id': 7888, 'author_profile': 'https://Stackoverflow.com/users/7888', 'pm_score': 5, 'selected': True, 'text': "<p>One simple way is: </p>\n\n<pre>\n$ touch a b c d e f g h i j k l m n o p q r s t u v w x y z\n$ mv $(ls | awk '{nr++; if (nr % 4 == 0) print $0}') destdir\n\n</pre>\n"}, {'answer_id': 71819, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 2, 'selected': False, 'text': "<p>Create a script move.sh which contains this:</p>\n\n<pre><code>#!/bin/sh\nmv $4 ../newdirectory/\n</code></pre>\n\n<p>Make it executable and then do this in the folder:</p>\n\n<pre><code>ls *.jpg | xargs -n 4 ./move.sh\n</code></pre>\n\n<p>This takes the list of filenames, passes four at a time into move.sh, which then ignores the first three and moves the fourth into a new folder.</p>\n\n<p>This will work even if the numbers are not exactly in sequence (e.g. if some frame numbers are missing, then using mod 4 arithmetic won't work).</p>\n"}, {'answer_id': 71835, 'author': 'Alexey Feldgendler', 'author_id': 10682, 'author_profile': 'https://Stackoverflow.com/users/10682', 'pm_score': 1, 'selected': False, 'text': "<pre><code>seq -f 'frame-%g.jpg' 1 4 number-of-frames\n</code></pre>\n\n<p>…will print the names of the files you need.</p>\n"}, {'answer_id': 71871, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Just iterate over a list of files:</p>\n\n<pre><code>files=( frame-*.jpg )\ni=0\nwhile [[ $i -lt ${#files} ]] ; do\n  cur_file=${files[$i]}\n  mungle_frame $cur_file\n  i=$( expr $i + 4 )\ndone\n</code></pre>\n'}, {'answer_id': 71918, 'author': 'masto', 'author_id': 11974, 'author_profile': 'https://Stackoverflow.com/users/11974', 'pm_score': 0, 'selected': False, 'text': '<p>This is pretty cheesy, but it should get the job done.  Assuming you\'re currently cd\'d into the directory containing all of your files:</p>\n\n<pre><code>mkdir ../outdir\nls | sort -n | while read fname; do mv "$fname" ../outdir/; read; read; read; done\n</code></pre>\n\n<p>The <code>sort -n</code> is there assuming your filenames don\'t all have the same number of digits; otherwise <code>ls</code> will sort in lexical order where <code>frame-123.jpg</code> comes before <code>frame-4.jpg</code> and I don\'t think that\'s what you want.</p>\n\n<p>Please be careful, back up your files before trying my solution, etc. I don\'t want to be responsible for you losing a year\'s worth of data.</p>\n\n<p>Note that this solution does handle files with spaces in the name, unlike most of the others.  I know that wasn\'t part of the sample filenames, but it\'s easy to write shell commands that don\'t handle spaces safely, so I wanted to do that in this example.</p>\n'}, {'answer_id': 73038, 'author': 'Iain', 'author_id': 12060, 'author_profile': 'https://Stackoverflow.com/users/12060', 'pm_score': 1, 'selected': False, 'text': "<p>Given masto's caveats about sorting:</p>\n\n<p><code>ls | sed -n '1~4 p' | xargs -i mv {} ../destdir/</code></p>\n\n<p>The thing I like about this solution is that everything's doing what it was designed to do, so it feels unixy to me.</p>\n"}, {'answer_id': 73125, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 2, 'selected': False, 'text': "<p>As suggested, you should use</p>\n\n<pre><code>seq -f 'frame-%g.jpg' 1 4 number-of-frames\n</code></pre>\n\n<p>to generate the list of filenames since 'ls' will fail on 280k files. So the final solution would be something like:</p>\n\n<pre><code>for f in `seq -f 'frame-%g.jpg' 1 4 number-of-frames` ; do\n  mv $f destdir/\ndone\n</code></pre>\n"}, {'answer_id': 73146237, 'author': 'M.Viking', 'author_id': 10276092, 'author_profile': 'https://Stackoverflow.com/users/10276092', 'pm_score': 0, 'selected': False, 'text': '<p>brace expansion {m..n..s} is more efficient than seq. AND it allows a bit of output formatting:</p>\n<pre><code>$ echo {0000..0010..2}\n0000 0002 0004 0006 0008 0010\n</code></pre>\n<p>Postscript: In <code>curl</code> if you only want every fourth (nth) numbered images so you tell curl a step counter too. This example range goes from 0 to 100 with an increment of 4 (n):</p>\n<pre><code>curl -O &quot;http://example.com/[0-100:4].png&quot;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/71776', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11950/']