Created
October 20, 2021 15:37
-
-
Save rogeruiz/3c6070f34f4482455441783619bc0c97 to your computer and use it in GitHub Desktop.
Find files and move them to a new place given the name of a file and the destination dirname and path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
file_name=$1 | |
new_path=$2 | |
full_path=$(find docs/ -iname "${file_name}") | |
extracted_file_name=$(basename "${full_path}") | |
# shellcheck disable=2116,2086 | |
lowercase_file_name=$(echo ${extracted_file_name} | tr '[:upper:]' '[:lower:]') | |
if [ -f "${full_path}" ] | |
then | |
mv "${full_path}" "${new_path}${lowercase_file_name}" | |
fi |
I made a tweak to have this script output the part needed for the redirect flow. Then you can run ./migrate-by-filename.sh a-file-to-move.md docs/new/path/ | pbcopy
#!/bin/bash
set -e
file_name=$1
new_path=$2
full_path=$(find docs -iname "${file_name}")
extracted_file_name=$(basename "${full_path}")
# shellcheck disable=2116,2086
lowercase_file_name=$(echo ${extracted_file_name} | tr '[:upper:]' '[:lower:]')
if [ -f "${full_path}" ]
then
files=$(mv -v "${full_path}" "${new_path}${lowercase_file_name}")
from=$(echo "${files}" | awk '{print $1}')
to=$(echo "${files}" | awk '{print $3}')
cat <<REDIRECT
{
to: '/${to%.*}',
from: [
'/${from%.*}',
],
},
REDIRECT
fi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script may error if
find docs/
finds more than a single document matching the name so you might need to be more specific in your search.Usage
Make sure that the second argument, the destination dirname and path need to include the final slash otherwise it will not be saved properly.