Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am randomly selecting files from a directory that I want to process, and I want to apply the touch command to them so that I know the last one that's been visualized, then apply the visualization program. Something like this:

while [ 1 ]; do ls | shuf | xargs "touch & vizprogram"; done

Since I am manually inspecting these datasets, I want to be able to "Esc" from the loop at any point, so that I interrupt the vizprogram and go through the loop again. Then when I find a file I want to follow on, I want to Ctrl+C and do an ls -rt | tail -n 1 to have the name of the last file I've used. I tried this with a for or while loop instead of xargs, but then I cannot do "Esc" on the main loop.

Any ideas how to do the "touch then vizprogram" within xargs?

share|improve this question
up vote 1 down vote accepted

If vizprogram returns true if exited with ESC and false if exited with Ctrl-C you can do:

while ls | shuf | parallel --halt 2 -j1 "touch {}; vizprogram {}"; do true; done

You can install GNU Parallel simply by:

chmod 755 parallel
cp parallel sem

Watch the intro videos for GNU Parallel to learn more:

share|improve this answer

Create a shell script such as the following:

#!/usr/bin/env bash
while [ $# -gt 0 ]
    touch "$1"
    vizprogram "$1"

Then have xargs execute it:

echo "file file2 file3" | xargs /path/to/
share|improve this answer
Not sure what you mean by cannot do "Esc". Ctrl-C works just fine with my answer. Let me know if I missed the point, I'll delete it then. – Daniel Beck Dec 5 '11 at 7:25
I use "Esc" to interrupt the vizprogram for files I don't want to follow on, but then do Ctrl+C to stop the loop when I find a file I want to closely inspect. I would like to have this file marked somehow, and doing a touch on it would give me its file name by being the one with the latest timestamp. – 719016 Dec 5 '11 at 7:46
@avilella Unless you edit vizprogram, there is no way to do this other than what I suggest. Your xargs method runs vizprogram with all files as arguments, in order -- you cannot interleave something else with a single program call. Individual invocations for every file, similar to what I suggest, or editing vizprogram are your only options I'm afraid (although I look forward to being proved wrong). – Daniel Beck Dec 5 '11 at 7:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .