Skip to content

Instantly share code, notes, and snippets.

@rogeruiz
Created October 20, 2021 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeruiz/3c6070f34f4482455441783619bc0c97 to your computer and use it in GitHub Desktop.
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.
#!/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
@rogeruiz
Copy link
Author

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

migrate-by-filename.sh a-markdown-file.md docs/new/path/for/file/

Make sure that the second argument, the destination dirname and path need to include the final slash otherwise it will not be saved properly.

@gidjin
Copy link

gidjin commented Oct 22, 2021

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