1

My folder has 10,000 files and I need to look through all of them. They are all called: A0001.png, A0002.png, A0003.png,...A0100.png,...A0999.png,.. A1000.png,...A09999.png,...A10000.png.

I want to open them ten at a time, look through them, close them, and open the next ten, but I'm still getting familiar with using the terminal, so I'm not sure how to do that?

When I do:

open A000*.png

It opens too many.

Guest
  • 151
  • 1
    Opening the first ten files is simple (and already asked & answered). Opening ten at a time, reading/inspecting their content, closing them then opening the next ten and so on is a different question so please clarify what is your actual goal here (the title should match the content of your post) – don_crissti Mar 09 '17 at 19:17
  • @don_crissti "Opening the first ten files is simple (and already asked & answered)." - I could not find such a post. If it's not too much trouble, could you direct me to this post so I can check if it is what I need? – Guest Mar 09 '17 at 19:23
  • a quick search returns How to move the first x files and How to move 100 files... (some of the answers show how to do something with the last n files but it's easy to change them to select the first n files) – don_crissti Mar 09 '17 at 19:27
  • Do you want to execute open A000X command over a file A00X ? – GC 13 Mar 09 '17 at 19:31
  • @GC13 Sorry, I have clarified the question. Each of the files is an image file. I need to open 10 at a time and look through all the images. – Guest Mar 09 '17 at 19:37
  • Open the files.. with what? On osx, what is the command to open a single file? – Jacob Vlijm Mar 09 '17 at 20:41
  • @JacobVlijm The command to open a single file is just open . So if you want to open some image file called image.png, then you would just type open image.png and it will open with whatever software you use to open .png files. – Guest Mar 09 '17 at 20:54
  • Ok, I see you just accepted, so I guess your issue is solved. – Jacob Vlijm Mar 09 '17 at 20:56

2 Answers2

3

In zsh:

open A000*.png([1,10])

for the first 10 ones.

In bash, you can always do:

files=(A000*.png)
open "${a[@]:0:10}"

Or in a loop:

while ((${#files[@]})); do
  open "${files[@]:0:10}"
  files=("${files[@]:10}")
done

That would also work in zsh, though in zsh, you could do it in a little less awkward syntax:

while (($#files)) {
  open $files[1,10]
  files[1,10]=()
}

Another option is to use xargs:

printf '%s\0' A000*.png | xargs -0n10 open

Though that affects open's stdin. zsh has a zargs function with similar features:

autoload zargs # in ~/.zshrc
zargs -n 10 A000*.png -- open

That lets you define that open as a function that needs to do whatever you want to do with those 10 files like:

open() {
  image_viewer $argv
  read -q '?continue? ' || return 255
}
  • Yes, this is what I need, however, it seems to not work on the Mac OS X machine I'm using. I did not realize there would be a difference, I apologize. I updated the post to include a osx tag. Do you happen to know how to perform this similar command on Mac OS X (10.6.8)? Currently I receive the following error: -bash: syntax error near unexpected token `(' – Guest Mar 09 '17 at 19:48
  • @Guest, you need to use the zsh, not bash both of which come standard with macOS. – Stéphane Chazelas Mar 09 '17 at 20:51
1

Using a loop and catching user input, it can be achieved:

#!/bin/bash

# Store all list of files you want with extension png
arr=(./*.png)
for ((i=0; i<${#arr[@]};))
do
    # -s: do not echo input character
    # -n 1: read only 1 character (separate with space)
    read -s -n 1 key 

    for ((j=0; j<10; j++, i++))
    do  
        if [[ $key = "" ]]; then 
            open "${arr[$i]}";    # <--- This is where you will open your file.
        fi
    done
done
GC 13
  • 586