For each file in directory bash script




















The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. How to go to each directory and execute a command on specific file? Ask Question. Asked 5 years, 9 months ago.

Active 5 years, 9 months ago. Viewed 20k times. Improve this question. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. Could somebody please provide the code to do the following: Assume there is a directory of files, all of which need to be run through a program.

The program outputs the results to standard out. I need a script that will go into a directory, execute the command on each file, and concat the output into one big output file.

However, if you care about the order in which the files are processed, you might be better off writing a loop. I think find processes the files in inode order though I could be wrong about that , which may not be what you want.

The bash shell provides an extended shell option for this using nullglob. So the loop basically becomes as follows inside the directory containing your files. This lets you safely exit the for loop when the expression. So on this condition failure, using continue we resume back to the for loop which won't run subsequently. Also note the usage of -- just before passing the file name argument. This is needed because as noted previously, the shell filenames can contain dashes anywhere in the filename.

Some of the shell commands interpret that and treat them as a command option when the name are not quoted properly and executes the command thinking if the flag is provided. The -- signals the end of command line options in that case which means, the command shouldn't parse any strings beyond this point as command flags but only as filenames. Double-quoting the filenames properly solves the cases when the names contain glob characters or white-spaces.

Since bash internally uses C style strings in which the null bytes are used to indicate the end of string, it is the right candidate for this. So using the printf option of shell to delimit files with this NULL byte using the -d option of read command, we can do below.

The nullglob and the printf are wrapped around.. Using find command this can be done as. For find implementations that don't support -print0 other than the GNU and the FreeBSD implementations , this can be emulated using printf. When used inside the loop, the shell has to execute system-calls twice for each iteration of the for-loop, once for opening and once for closing the file descriptor associated with the file.

Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. All I want to do is loop through all files in the root and execute a program only on.

The problem is the program I have to execute is 'xls2csv' to convert from. So, for each. For instance, I have a test. This could be horrible for your xls2csv. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Asked 11 years, 11 months ago. Active 6 years, 11 months ago. Since the Bash tag is used: that's what shopt nullglob is for! Besides, this also checks for files deleted during processing, before the loop reaches them. Also handles the case where you have a directory called dir. Don't be sorry; the whole purpose of Stack Overflow is to collect and curate canonical questions rather than have new users reask the same old questions.

Jonathan Leffler Jonathan Leffler k gold badges silver badges bronze badges. I believe there's an error in your code. CaseyKlimkowsky: Yes; when the code and the comments disagree, at least one of them is wrong. In this case, I think it is only the one — the code; often, it is actually both that are wrong.

Thanks for pointing that out; I've fixed it. James Andino James Andino Going to make the edits you pointed out. Also -r I see is often default, but could not find an example for what it prevents happening. Underrated answer. Try the most upvoted ones on a directory containing more than k files. Good luck if you're on a low-end machine. Sign up or log in Sign up using Google. Sign up using Facebook.



0コメント

  • 1000 / 1000