Is there a way to control order of recursion with find?

Currently, I'm using find to get the contents of a few directories, and save the directories, file names, and file contents to a lua table.

The Lua table looks like:

Paths = {
    ["path/to/files] = {
        FILE_NAME = [[
             file contents
        ]], -- other files
    },
    ["other/path/to/files"] = {...}
}

The find command I'm running is:

find {hierarchy1,hierarcy2} -type d -or -type f

This gets all of the files and folders, but recurses down into the first sub directory before returning file names. I do get the files in the top level directory; but those get listed last for each hierarchy, throwing off the desired hierarchy for the lua file. I tried tree, and that does all of the operations in the correct order, but gives full paths for each file when not printing with a tree structure. Is there anyway to control the recursion in find?

could you show me an example of the output you get currently.

Sure, the output I'm getting is:

DialogueSystem
DialogueSystem/Private
DialogueSystem/Private/DialogueSystemPrivatePCH.h
DialogueSystem/Private/Dialogues
DialogueSystem/Private/Dialogues/DialoguesGameMode.cpp
DialogueSystem/Private/Dialogues/DialoguesCharacter.cpp
DialogueSystem/Private/Dialogues/DialogueActor.cpp
DialogueSystem/Private/Dialogues/UI
DialogueSystem/Private/Dialogues/UI/UDialogueHUD.cpp
DialogueSystem/Private/Dialogues/UI/DialogueChoiceStyles.cpp
DialogueSystem/Private/Dialogues/UI/DialogueHUD.cpp
DialogueSystem/Private/Dialogues/UI/BanterHUD.cpp
DialogueSystem/Private/Dialogues/UI/SBanterUI.cpp
DialogueSystem/Private/Dialogues/UI/UDialogueChoice.cpp
DialogueSystem/Private/Dialogues/UI/DialogueChoiceWidgetStyle.cpp
DialogueSystem/Private/Dialogues/UI/SDialogueChoiceUI.cpp
DialogueSystem/Private/Dialogues/UI/BaseDialogueHUD.cpp
DialogueSystem/Private/Dialogues/Conversations
DialogueSystem/Private/Dialogues/Conversations/BaseDialogue.cpp
DialogueSystem/Private/DialogueSystem.cpp
DialogueSystem/DialogueSystem.Build.cs
DialogueSystem/Public
DialogueSystem/Public/Dialogues
DialogueSystem/Public/Dialogues/DialoguesCharacter.h
DialogueSystem/Public/Dialogues/TArrayHelper.h
DialogueSystem/Public/Dialogues/DialogueActor.h
DialogueSystem/Public/Dialogues/UI
DialogueSystem/Public/Dialogues/UI/UDialogueHUD.h
DialogueSystem/Public/Dialogues/UI/BanterHUD.h
DialogueSystem/Public/Dialogues/UI/SBanterUI.h
DialogueSystem/Public/Dialogues/UI/DialogueChoiceStyles.h
DialogueSystem/Public/Dialogues/UI/SDialogueChoiceUI.h
DialogueSystem/Public/Dialogues/UI/AdvanceDialogueDelegate.h
DialogueSystem/Public/Dialogues/UI/DialogueHUD.h
DialogueSystem/Public/Dialogues/UI/DialogueChoiceWidgetStyle.h
DialogueSystem/Public/Dialogues/UI/BaseDialogueHUD.h
DialogueSystem/Public/Dialogues/UI/UDialogueChoice.h
DialogueSystem/Public/Dialogues/Typedefs.h
DialogueSystem/Public/Dialogues/DialoguesGameMode.h
DialogueSystem/Public/Dialogues/Conversations
DialogueSystem/Public/Dialogues/Conversations/DialogueNode.h
DialogueSystem/Public/Dialogues/Conversations/BaseDialogue.h
DialogueSystem/Public/Dialogues/Conversations/DialogueMode.h
DialogueSystem/Public/DialogueSystem.h
Messenger
Messenger/Private
Messenger/Private/Messenger.cpp
Messenger/Private/MessengerPrivatePCH.h
Messenger/Public
Messenger/Public/Messenger.h
Messenger/Messenger.Build.cs

It goes straight into the first sub-folder, before outputting files in the root folder.

Does this post on stackoverflow cover your problem? There is a solution to use the -depth option to make it process the files in a folder first, before processing the subfolders.

1 Like

Yeah, that's closer to what I'm looking for. I tried the -depth option earlier, and it is still a bit off. Looks like doing a IDFS search is the best I'll get with find. Some of the other suggestions on there look promising as well.

Time to have fun with recursive functions!

This looks like a good solution. Only problem is it breaks compatibility with Bash.

This is what ended up working in the end:

contents="$( find {DialogueSystem,Messenger} \
! -name . -type d -print -exec sh -c \
'find "$1" -maxdepth 1 ! -type d' {} {} \; )"
1 Like

Finding the answer by posting on this forum and reading other people's problems: 6 days.

Reading the man page: -maxdepth is at the beginning of the man page, that would take you about 10-20 minutes to read.

Conclusion: read the manual, it will save you time.

I didn't think about chaining find calls together.