Shell Utilities: shutil and glob
/step-by-step skill.Two modules replace most of the file-shuffling you do with cp, mv, rm -r, df and shell wildcards.
shutil: high-level file operations#
Copy#
shutil.copy(src, dst)copies a file's content and permission bits.dstcan be a directory.shutil.copy2(src, dst)also copies timestamps, likecp -p.shutil.copytree(src, dst)copies a whole directory tree, likecp -r.
Move and delete#
shutil.move(src, dst)moves a file or directory, likemv.shutil.rmtree(path)deletes a directory tree, likerm -rf. There is no undo.
Disk, commands and archives#
shutil.disk_usage(path)returns(total, used, free)in bytes, likedf.shutil.which("cmd")returns the full path of a command, orNone, likewhich.shutil.make_archive(base, "gztar", root_dir)creates a.tar.gz(orzip) archive.shutil.chown(path, user, group)changes ownership, likechown.
>>> import shutil
>>> shutil.copy2("/etc/hosts", "/tmp/hosts.bak")
'/tmp/hosts.bak'
>>> shutil.which("docker")
'/usr/bin/docker'
>>> total, used, free = shutil.disk_usage("/")
>>> f"{used / total:.0%} used"
'63% used'
>>> shutil.make_archive("/tmp/home-backup", "gztar", ".") # archive the current directory
'/tmp/home-backup.tar.gz'Copying or archiving system directories like
/etc/sshraisesPermissionErroras a normal user, because some files (such as SSH host keys) are readable only by root. Run the script withsudo, or catch the error and skip those files.
Example: alert when the disk is almost full#
#!/usr/bin/env python3
# disk_alert.py
import shutil
import sys
THRESHOLD = 80 # percent
def main(path="/"):
total, used, free = shutil.disk_usage(path)
percent = used / total * 100
print(f"{path}: {percent:.1f}% used, {free / 2**30:.1f} GiB free")
if percent > THRESHOLD:
# a non-zero exit code lets cron, systemd or your monitoring react
sys.exit(f"WARNING: {path} is above {THRESHOLD}%")
if __name__ == "__main__":
main(*sys.argv[1:])The original version of this example also sends an email with
smtplib. Seeexamples/Monitoring Disk Space and Sending Alerts.py.
glob: Unix-style wildcards#
glob.glob(pattern) returns the paths that match a shell-style pattern. Use ** with recursive=True to search subdirectories.
>>> import glob
>>> glob.glob("/etc/*.conf")
['/etc/resolv.conf', '/etc/sudo.conf', '/etc/host.conf', '/etc/logrotate.conf']
>>> glob.glob("/var/log/**/*.log", recursive=True)
['/var/log/dnf.log', '/var/log/nginx/access.log', '/var/log/nginx/error.log']pathlib has the same feature built in: Path("/var/log").rglob("*.log").
Exercises#
- Write
rotate.pythat gzips every*.logfile older than 7 days in a directory. Hint:os.stat(path).st_mtime,time.time()andshutil.copyfileobjwithgzip.open. - Make
disk_alert.pycheck every path given on the command line and exit non-zero if any of them is over the threshold.
Learn more in the shutil and glob documentation.