Loop through all files in a directory (as preliminary step in larger coding project)
Hello.
Here's my code so far to loop through all files in a directory structure, including subdirectories. #!/bin/bash FILES=$(find home/eric/doxDUP -type f -name '/*') for f in $FILES do echo "Testing" done I've found related questions elsewhere on this forum and others, but for some reason it's not working. It just returns me to the command prompt -- even though there are files in the subdirectories. And for each such file, it should write "Testing" to the standard output. This is just a step toward a larger coding objective, which will involve changing file dates. Any assistance greatly appreciated. Thanks. EVL |
Take a close look at what you have in the -name option and determine what it will really give you, as opposed to what you want it to give you.
Also, there seems to be a slash missing from the start of the path given. |
Try using code tags to preserve formatting. Try this:
Code:
#!/bin/bash |
Hi,
I tried the following Code:
#!/bin/bash |
As others have suggested, you should debug your find command details first. A great way to do this also is to just perform that find command on the command line to see the results. Then once you have it correct, you can code it within the script.
As others have mentioned, please use [code][/code] tags around code to make it easier to read and retain formatting. When you say that this is part of a step towards a larger project, it is a good idea to share the intentions of that larger project. Reason for that is because you may be looking to perform operations which are far easier to do in another language, or another means of execution. There are a number of tools you can use to debug bash scripts, some of them are described here. You also may wish to check a highly similar question in this thread where the OP was also looking to perform a loop to find files and perform an operation on those files. |
Firstly, if you want ALL files, why both with -name or -wholename at all?
I would also add that all presented solutions will fail if there are any directories or files with white space in them. For this reason, you should use a while loop: Code:
while read -r filename |
http://mywiki.wooledge.org/BashPitfa...8ls_.2A.mp3.29
and http://mywiki.wooledge.org/UsingFind So something like the last examples at the UsingFind page (one already given in the previous answer, the other round the lines of: Code:
# POSIX |
Thanks to all who replied ..
I am working on these suggestions today .. & looking forward to reporting results ..
|
Tried debugging for-loop at command-line, as suggested
Quote:
I have tried many versions of a for loop -- all at the command-line, as you suggested. For instance, using examples at https://www.cyberciti.biz/faq/bash-for-loop/ I tried Code:
It just hangs, & I press CNTRL-C. In order to not use a script, I type the above at the command line, without #!/bin/bash. (I hope that's correct.) Your link to bash scripting for dummies is extremely informative. Any suggestions much appreciated. EVL |
Quote:
Code:
for f in {1..3}; do echo "eg $f"; done Quote:
|
To separate you either use a newline or a semi-colon, just like:
Code:
user$ pwd Code:
user$ pwd; ls If you want to avoid quite some grief, go straight to mywiki.wooledge.org (or bashhackers). There you will find common problems, just like what grail mentioned in his post. Imho it is better to not get used to it at all, instead of getting rid of it later. |
It is also possible to use the native bash shell functionality provided by setting the globstar shell option rather than invoking 'find'.
Code:
#!/bin/bash |
Thanks I got this .. when I have a second, I'll post the code
|
All times are GMT -5. The time now is 10:34 PM. |