A CLI tool that walks a directory tree executing actions on files.
Use the walk
CLI tool to walk a directory tree listing or executing actions on files.
-
Cross-platform: Linux / macOS / Windows.
-
Allow passing a directory path to walk in.
-
Allow filtering by using a file extension.
-
Allow filtering by minimum file size.
-
Allow deleting the files matched (with option to provide logs).
-
Allow archiving a compressed copy of the files matched.
- Go version 1.18.6 (or above)
-
Run:
$ go install github.com/rossijonas/walk@latest
$ walk -h
Usage of walk:
-archive string
Path to directory where files should be archived
-del
Delete files
-ext string
File extension to filter out
-list
List files only
-log string
Log deletes to this file
-root string
Root directory to start (default ".")
-size int
Minimum file size
$ walk
README.md
docs/mydocument.txt
photos/dog.jpg
$ walk -root /tmp/testdir/
/tmp/testdir/file1.txt
/tmp/testdir/logs/log1.log
/tmp/testdir/logs/log2.log
/tmp/testdir/text/text1.txt
/tmp/testdir/text/text2.txt
$ walk -root /tmp/testdir/ -ext .log
/tmp/testdir/logs/log1.log
/tmp/testdir/logs/log2.log
$ walk -root /tmp/testdir/ -ext .log -del
Delete all files with .txt
extension inside /tmp/testdir/
and its subdirectories, and log deleted files to deleted_files.log
:
$ walk -root /tmp/testdir -ext .txt -log deleted_files.log -del
$ cat deleted_files.log
DELETED FILE: 2022/10/15 21:02:15 /tmp/testdir/file1.txt
DELETED FILE: 2022/10/15 21:02:15 /tmp/testdir/text/text1.txt
DELETED FILE: 2022/10/15 21:02:15 /tmp/testdir/text/text2.txt
DELETED FILE: 2022/10/15 21:02:15 /tmp/testdir/text/text3.txt
Compress and archive all files with .txt
extension inside /tmp/testdir/
and its subdirectories, delete them after archiving, and log deleted files to deleted_files.log
:
$ mkdir /tmp/testdir_txt_bkp
$ walk -root /tmp/testdir -ext .txt -archive /tmp/testdir_txt_bkp/ -log deleted_files.log -del
$ tree /tmp/testdir_txt_bkp
/tmp/testdir_txt_bkp
├── file1.txt.gz
└── text
├── text1.txt.gz
├── text2.txt.gz
└── text3.txt.gz
$ cat deleted_files.log
DELETED FILE: 2022/10/30 19:18:30 /tmp/testdir/file1.txt
DELETED FILE: 2022/10/30 19:18:30 /tmp/testdir/text/text1.txt
DELETED FILE: 2022/10/30 19:18:30 /tmp/testdir/text/text2.txt
DELETED FILE: 2022/10/30 19:18:30 /tmp/testdir/text/text3.txt
-
Add example Gif to README file.
-
Allow the user to provide more than one file extension.
-
Add more filtering options such as files modified after a certain date or files with long file names.
-
Create a companion tool for walk that restores the archived files in case they are needed again. Recreate the original directory by using the same approach you used to create the destination directory in the
archiveFile
function. Then use thegzip.Reader
type from thecompress/gzip
package to uncompress the archive files.
This is an exercise from the book "Powerful Command-Line Applications in Go", but it may differ from the original exercise.