Directory trees
PYTHON 3: Monitoring cycles in directory trees
Mihalis Tsoukalos sips a cold Mythos in the Greek sun while working with dictionaries, os.walk(), files, symbolic links and directories.
OUR EXPERT
Mihalis; Tsoukalos is a systems engineer and technical writer. He’s also the author of Go Systems Programming and Mastering Go. Contact him on @mactsouk.
W e’re going to use Python 3 to write Linux command line utilities that work with files and finds cycles in directory trees. The thinking here is that it’s faster to write in Python 3 than to use C. However, the tutorial presents some directions for developing the same utility in both C and Go. Let’s start by understanding the problem we’re trying to solve.
The idea behind the utility is that with UNIX symbolic links it’s possible to create cycles (aka loops) in filesystems. Put simply, without symbolic links, there would be no cycles problem. This can perplex back-up software such as tar or utilities such as find and it can sometimes have security implications: for example, accessing files outside of the designated directory tree. FScycles.py, the utility we’ll be focusing on in this tutorial, attempts to inform us about such situations.
The screenshot shows a directory tree using the tree utility – it’s the ~/go directory where Go puts external packages. Because this directory doesn’t contain any symbolic links, create some using Before getting into the meat of the subject let’s understand the various types of files that can be found in Linux. Remember that UNIX treats everything as a file, even hardware devices such as mice, keyboards and printers.
Here’s our directory structure used for testing. It’s good practice to test system utilities before applying them on production machines.
Some of the files that can be found in Linux are regular files, directories, symbolic links, sockets, named pipes and block devices. Almost every programming language offers specialised functions or other ways for determining the type of file or path. What interests us are directories and symbolic links to directories.
A symbolic link is a file that links to another directory or file by using its path. Although you can create symbolic links programmatically, you usually use the ln utility for this task.
Finally, bear in mind that you need to have the necessary permissions to examine some system locations, files and directories. This can limit the output of all utilities that traverse directory trees, including the ones shown here.
Processing arguments