From d780b45f0c8bf0207dbf432ee4539cf9a4faccc1 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Tue, 14 Mar 2023 20:14:24 +0100 Subject: [PATCH] Initial commit --- LICENSE | 9 +++++++ README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++ recursivefilecmd.sh | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100755 recursivefilecmd.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..067caca --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2023 Ruakij + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7480dbf --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +recursivefilecmd +== + +Execute a command on files with a specific extension recursively in a directory tree. + +
+ +# 1. Overview +## 1.1. Usage + +```sh +./recursivefilecmd.sh +``` + +
+ +### 1.1.1. Arguments + +- `extension`: The file extension to search for. +- `path`: The root directory to start searching for files. +- `command`: The command to execute on each file. + +
+ +### 1.1.2. Examples + +```sh +./recursivefilecmd.sh 'hcl' 'services/' nomad run -detach +``` + +```sh +./recursivefilecmd.sh 'yaml' 'config/' kubectl apply -f +``` + +
+ +## 1.2. Behaviour + +The file-name including its path are added to the end of the given command. + +All files are executed in-sequence. + +When `path` is a file which matches the extension, only it is executed.
+When the extension doesnt match, the directory is used instead, walking up up to 3 times, searching for files matching the extension. + +
+ +# 2. Development +## 2.1. Features + +Maybe i'll add some more features, here a few i can think of: + +- [ ] Convert project to golang +- [ ] Add options for optional things +- [ ] Replace `extension`-filter with a regex-match +- [ ] Make recursive optional +- [ ] Parameterize passed-filepath (e.g. # for all & #1 for regex-group 1) diff --git a/recursivefilecmd.sh b/recursivefilecmd.sh new file mode 100755 index 0000000..30497ec --- /dev/null +++ b/recursivefilecmd.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +extension=$1 +path=$2 +cmd="${@:3}" + +execInDir() { + file=$1 + + # get folder + folder=$(dirname "$file") + # get local file + filename=$(basename "$file") + ( + # switch to it + cd "$folder" + # exec cmd with filename behind it + $cmd $filename + ) +} + +# Check if path is file and matches extension +if [[ -f "$path" && "${path##*.}" = "$extension" ]]; then + execInDir $path +else + if [[ -f "$path" ]]; then + # Strip any filename + path=$(dirname "$path") + fi + + # Try 3 times + for i in {0..3}; do + # Get files with extension + files=$(find "$path" -iname "*.$extension") + if [[ "$files" = "" ]]; then + # If there are none, go up and search again + path="${path}/.." + else + for file in $files; do + execInDir $file + done + break + fi + done +fi