LinuxCommandLibrary

bfs

Breadth-first search files in a directory tree

TLDR

Find files by extension

$ bfs [root_path] -name '[*.ext]'
copy

Find files matching multiple path/name patterns
$ bfs [root_path] -path '[**/path/**/*.ext]' -or -name '[*pattern*]'
copy

Find directories matching a given name, in case-insensitive mode
$ bfs [root_path] -type d -iname '[*lib*]'
copy

Find files matching a given pattern, excluding specific paths
$ bfs [root_path] -name '[*.py]' -not -path '[*/site-packages/*]'
copy

Find files matching a given size range, limiting the recursive depth to "1"
$ bfs [root_path] -maxdepth 1 -size [+500k] -size [-10M]
copy

Run a command for each file (use {} within the command to access the filename)
$ bfs [root_path] -name '[*.ext]' -exec [wc -l] {} \;
copy

Find all files modified today and pass the results to a single command as arguments
$ bfs [root_path] -daystart -mtime [-1] -exec [tar -cvf archive.tar] {} \+
copy

Find empty files (0 byte) or directories and delete them verbosely
$ bfs [root_path] -type [f|d] -empty -delete -print
copy

SYNOPSIS

N/A: The 'bfs' command does not exist as a standard Linux utility.
Attempts to execute 'bfs' will typically result in a 'command not found' error.

DESCRIPTION

The term 'bfs' most commonly refers to the Breadth-First Search algorithm, a fundamental graph traversal algorithm. It is not a standalone command in standard Linux distributions. BFS explores all the neighbor nodes at the present depth before moving on to nodes at the next depth level. This methodical approach is widely used in various computer science applications, such as finding the shortest path in unweighted graphs, web crawlers, and network topology analysis.

While the Linux find command typically performs a Depth-First Search (DFS) by default for file system traversal, the principles of BFS are conceptually relevant to how certain data structures or operations might process information in a level-by-level manner. There is no direct executable named 'bfs' that users can invoke to perform a BFS traversal of a file system or other data structure.

CAVEATS

Users should be aware that 'bfs' is an algorithmic concept, not an executable program. If one encounters references to 'bfs' in a Linux context, it almost certainly pertains to the Breadth-First Search algorithm's application or conceptual understanding, rather than a specific command to be run from the terminal.

UNDERSTANDING BREADTH-FIRST SEARCH ALGORITHM

BFS systematically explores a graph or tree level by level. It starts at a source node, then visits all its direct neighbors, then all their unvisited neighbors, and so on. This approach ensures that all nodes at depth d are visited before any nodes at depth d+1. It is often implemented using a queue data structure to keep track of the nodes to visit next.

This is in contrast to Depth-First Search (DFS), which explores as far as possible along each branch before backtracking. While find uses DFS by default, the conceptual understanding of BFS is valuable for optimizing certain data processing tasks or for developing custom scripts that need to process files or directories in a level-by-level manner.

HISTORY

The Breadth-First Search algorithm was first devised by Edward F. Moore in 1959 for finding the shortest path in a labyrinth, and independently by C. Y. Lee in 1961 for wire routing. As 'bfs' refers to an algorithm rather than a specific Linux command, it does not have a command-specific development or usage history within the Linux operating system itself.

SEE ALSO

find(1), tree(1)

Copied to clipboard