You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
13 MIN READ TIME

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

Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
May 2021
VIEW IN STORE

Other Articles in this Issue


WELCOME
Pico boo!
I remember people used to talk about market
WHO WE ARE
We’re trying out Raspberry Pi and Pico projects this issue – so what’s your favourite maker project or Pi add-on?
REGULARS AT A GLANCE
Newsdesk
Is Wayland ready  for prime time?
Linux lands on the Red Planet
The Perseverance rover’s successful Mars landing took Linux and open source software to another planet
Secured-core server announced
AMD and Microsoft have teamed up to bring a greater degree of security to devices
SALUTING KERNELCI
Alexandra Pereira is a software engineer at Collabora.
RISKY BUSINESS
Keith Edmunds is MD of Tiger Computing Ltd,
Make your own laptop
A modular notebook that users can easily configure and upgrade
Bye bye Python 2
Fedora is now almost free from Python 2, though some packages still require the aging language
Play games with Steam Link
Steam Link enables you to stream games from your PC to other devices
Distro watch
What’s lurking behind the free software sofa?
PRIORITIES THE PEOPLE
Matt Yonkovit is Percona’s Head of Open Source
TRIED AND TRUSTED
Jon Masters has been involved with Linux for
Kernel Watch
Jon Masters summarises the latest happenings in the Linux kernel, so you don’t have to
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to lxf.answers@futurenet.com
Mailserver
Write to us at Linux Format, Future Publishing, Quay House, The Ambury, Bath BA1 1UA or lxf.letters@futurenet.com .
HotPicks
Photoflare
Qytdl
Version: GIT Web: https://github.com/ jahendrie/qytdl
KWipe
Version: 3.0.3 Web: https://github . com/PyCoder/KWipe
GabTag
Version: 2.1Web: https://github.com/ lachhebo/GabTag
Fvid
Version: 1.0.0 Web: https://github.com/ AlfredoSequeida/fvid
Draw.io
Version: 14.4.2 Web: https://github . com/jgraph/drawio
Spech
Version: 0.2.1 Web: https://github.com/ megahertz/spech
0 A.D.
Version: Alpha 24 Web: https://play0ad.com
Quadplay
Version: Beta update 65 Web: https:// github.com/morgan3d/quadpla
Guitar
Version: 1.2.0 Web: https://github.com/ soramimi/guitar
Sonice
Version: 2.2.1 Web: https://github.com/ sunny/so-nice
On the disc
Discover the highlights from this month’s packed DVD!
REVIEWS
AMD Ryzen 9 5950X
Zak Storey climbs onto the closest thing the computing industry has to a unicorn, and straps in for the ride of his life
WD Black SN850 1TB
Move over Samsung, there’s a new PCIe 4.0 storage king in town says Alan Dexter, as he reaches for the ermine-lined cloak
SparkyLinux 2021.03
Its rather unimpressive-looking screenshots intrigued Mayank Sharma, who wants to find how this distro has survived for as long as it has
SystemRescue 8.0
If you’re as adventurous with your installations as Mayank Sharma, then you too would want to keep a copy of this distro handy…
CentOS Stream 8
Mayank Sharma takes a look at the distro that has the open source community up in arms, to see what the hullabaloo is all about
Spiritfarer
In these turbulent times management has shown uncharacteristic compassion and allowed Rachel Watts to contemplate life
ROUNDUP
Audio workstations
The Linux desktop has always had its share of music software. Michael Reed looks at the cream of the current Digital Audio Workstations crop
Plugin support
There are many plugin standards in the computer audio world
Installation
How easy is it to install a DAW?
User interface and ease of use
It’s the nerve centre, so it’d better be good
JACK support
The JACK system offers an advantage to Linux-based musicians
MIDI workflow
For electronic music, MIDI workflow is the most important aspect of a DAW
Audio workflow
Recording audio, chopping it up and processing it are key DAW features
Special features
What makes a particular DAW stand out from the rest?
Audio workstations
The verdict
PLUG & PLAY PI PROJECTS
PLUG & PLAY Pi PROJECTS
Jonni Bidwell with the help of Tom’s Hardware bring you the best in Raspberry Pi-based fun
Get your kit ready
Prepare your USB cables, SD cards and a cup of tea. It’s Raspberry Pi project time…
Image recognition
Get into machine learning and train your Pi to recognise and classify other Pis, without having to write a single line of code
Multi-room Pi audio
Get your Raspberry Pis in sync, audibly throughout your home
Pi-based KVM over IP
Use your Pi as a remote control for your PC, even if it won’t boot to an OS
Gaming on RetroPie
Turn your Raspberry Pi 4 (or 3 or Zero) into a retro gaming rig with RetroPie and dubious internet downloads
Environment sensors
With sensors and data and neat tidy cables, that’s how our garden grows
Bitcoin Node with RaspiBlitz
Make the Bitcoin network a bit bigger and better with a Pi and a large storage device
MotionEyeOS
Learn how to build your own motiontriggered home security camera
IN-DEPTH
A PARENT ’S GUIDE TO PROGRAMMING
Mike Bedford investigates which languages to consider if you want to help your children get a head-start in coding
ALL YOUR DATABASE ARE BELONG TO US
With the help of Patrick McFadin, the Datastax database guru, Jonni Bidwell gets a handle on the wherefore and why of Kubernetes
Pi USER
BACK IN THE TECH SADDLE
Nicole Parrot is the director of technology at
Register now for the Coolest Projects 2021
Individual and team entries are now open so you can show off your amazing builds to the world!
MX-Fluxbox Pi 21.02.20
Les Pounder is always on the lookout for a new slice of Pi and with MX Linux he discovers something fresh from the oven
Escape from the evil space squids!
Captain Les Pounder of the USS Rentaprize has to evade the clutches of the evil space squids, but can he do the maths to escape the planet?
New tricks for the Pico voltmeter
Tam Hanna expands his basic Raspberry Pi Pico voltmeter to improve accuracy and add a resistor measurement capability
TUTORIALS
Terminal multiplexer
Shashank Sharma is enthralled by screen multiplexers, and Byobu presents an elegant and efficient approach to managing multiple windows
Run the perfect videoconference session
Move over Zoom, Skype and all the rest – Nick Peers reveals how to set up your own public (and private) group video chats for free with Jitsi Meet
Getting started in 3D using Blender
Even if you’ve never used Blender before, Michael Reed will have you blending like a pro by the end of this tutorial
Run an Apple II on your Linux system
Les Pounder steps into his time machine and travels back to 1977, and not just to watch Star Wars on the big screen for once
How to add special effects to photographs
Fancy getting more creative in your photography? Mike Bedford offers some inspiration by introducing a few special effects for you to explore
Co-editing documents with Confluence
Integration of OnlyOffice Docs with Confluence enables multiple users to edit office documents in a single interface, as Kseniya Fedoruk reveals
CODING ACADEMY
PYTHON: Immerse yourself in a first-person shooter
Calvin Robinson looks to the iconic PC games Doom and Wolfenstein 3D for inspiration, in the latest instalment in his Python coding series
Chat
X
Pocketmags Support