I've got a folder structure like this:
/path/to/directory/foldernameAUTO_001
/path/to/directory/foldername_002
I'm trying to search through /path/to/directory to find instances where "foldernameAUTO" exists alongside any other directories that have the same base name but without "AUTO" and a higher number after the underscore. For example, if I have a directory named "testfolderAUTO_001", I need to find "testfolder_002" or "testfolderAUTO_002".
However, my loop seems to be stuck; here's what I have:
#!/bin/bash
Folder=/path/to/directory/
while IFS='/' read -r blank path to directory foldername_seq; do
echo "Found AUTO of $foldername_seq"
foldername=$(echo "$foldername_seq" | cut -d_ -f1) && echo "foldername is $foldername"
seq=$(echo "$foldername_seq" | cut -d_ -f2) && echo "sequence is $seq"
printf -v int '%d/n' "$seq"
(( newseq=seq+1 )) && echo "New sequence is 00$newseq"
echo "Finding successors for $foldername"
find $Folder -name "$foldername"_00"$newseq"
noauto=$(echo "${foldername:0:-4}") && echo "NoAuto is $noauto"
find $Folder -name "$noauto"_00"newseq"
echo ""
done < <(find $Folder -name "*AUTO*")
Yet, I keep getting the same output repeatedly, showing the same directory multiple times. Here's the output of my loop for clarity:
Found AUTO of foldernameAUTO_001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername
5 Answers
The issue likely arises from how your loop reads input. It seems you're always processing the same directory. Check this line:
```
while IFS='/' read -r blank path to directory foldername_seq; do
```
If the `foldername_seq` isn't being correctly updated, you may end up processing the same value each time. Make sure your input is flowing correctly into the loop!
Your `printf` statement is also suspect; it should be `'
'` and not `/n`. Just checking to make sure you correct that!
I see a few areas to tweak for efficiency. Instead of using `echo ... | cut ...`, consider switching to:
```bash
foldername="${foldername_seq%%_*}"
seq="${foldername_seq##*_}"
```
These expansions are cleaner. Also, the `printf` for formatting your sequence number should use the format string like `%03d` to ensure three-digit output. Keep in mind to quote your variable properly to avoid unexpected issues if there are spaces in directory names.
Agreed! Using `${...}` also helps with variable clarity when using special characters!
Found the culprit! You need to specify the type of files being searched. Change:
```bash
done < <(find $Folder -name "*AUTO*")
```
to:
```bash
done < <(find $Folder -type d -name "*AUTO*")
```
This makes sure you're only looking at directories, not files with similar names.
Definitely! And don't forget to quote `$Folder` in your command to prevent issues with directories that have spaces.
Just wanted to say this is a well-structured question! Even though there are some details missing, you clearly explained your issue. That's a great way to get help. Keep it up!
That's a solid suggestion! Quoting variables is key to avoiding problems down the line.