Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to execute a command, let's call it abc and add as a parameter each filename in the current folder. How could I do this in C Shell (or other scripting language)?

share|improve this question
    
Perhaps you can do just abc *? If not, explain more exactly what you want. –  Daniel Andersson Feb 27 '13 at 13:43

2 Answers 2

up vote 1 down vote accepted

Using csh or tcsh, this would iterate over all the files and directories in the current directory. I've used quotes around the $i variable substitution in case some of the names have spaces.

foreach i (*)
   abc "$i"
end

If you'd like to get only the files, not the directories, use the -f test. Here I've used the C shell's short-form if statement.

foreach i (*)
   if (-f "$i") abc "$i"
end
share|improve this answer

Look up xargs. If your shell is anything like mine, the following should work nicely:

find . -type f -maxdepth 1 | xargs abc

If you are concerned about spaces in the file names, see Daniel Andersson's comment below.

share|improve this answer
    
This will give all files in subdirectories as well. It will also give all directories, though the question specifies "files" (but that might be unclear from the questioner). It will also break on every filename containing a space. find . -maxdepth 1 -mindepth 1 -type f -print0 | xargs -0 abc or find . -maxdepth 1 -mindepth 1 -type f -exec abc {} + would fix these points. –  Daniel Andersson Feb 27 '13 at 13:48
    
@DanielAndersson All valid comments... I updated my answer a bit. I can't remember the last time I saw a space in a file name on a *nix system though. :-) –  kmort Feb 27 '13 at 14:15
    
@kmort I agree that filenames with spaces are rare on a Linux system but if you post an answer that doesn't consider the possibility (or at least point out that it will fail if there are spaces) someone is certain to jump on you here on the Superuser site. It's just the way it is here. –  Nicole Hamilton Feb 27 '13 at 17:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.