Loop through all files in a directory (as preliminary step in larger coding project)
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Last edited by EVL; 01-16-2017 at 11:40 PM.
Reason: to clarify
#!/bin/bash
FILES=$( find /home/aragorn/Documents -type f -name "*" )
for f in $FILES; do
echo "Testing $f"
done
exit 0
A few things you should note. The place where find is looking for the files should be verified. You used "home/eric/doxDUP", which will work only if you are in directory / because directory home/ is under /. Then, if you are using the option "-name", the expression following must match a name only, not a path. So, if you use "/*", find will try to match all files having names starting with a slash. If you read the man pages for find, you can learn about options "-wholename" and "-path". Maybe this is what you want. But the above code adapted to your filesystem should work. One last thing, I put a "$f" in the argument to echo so that it prints the file names from FILES.
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
do
...
done< <(find /home/aragorn/Documents -type f)
Tried debugging for-loop at command-line, as suggested
Quote:
Originally Posted by rtmistler
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.
There are a number of tools you can use to debug bash scripts, some of them are described here.
Thanks for the suggestions. As regards debugging the for loop,
I have tried many versions of a for loop --
all at the command-line, as you suggested.
To separate you either use a newline or a semi-colon, just like:
Code:
user$ pwd
/home/user/Programming/Tmp
user$ ls
getopts_rankmirrors tester
or
Code:
user$ pwd; ls
/home/user/Programming/Tmp
getopts_rankmirrors tester
user$
Hence the example from the link cyperciti works (it uses no semi-colons, as it uses newlines).
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
shopt -s globstar
for f in /home/eric/doxDUP/**
do
[[ -f "$f" ]] && echo "$f is a file"
[[ -d "$f" ]] && echo "$f is a directory"
done
shopt -u globstar
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.