Skip to content

Instantly share code, notes, and snippets.

@Mikael-Lovqvist
Last active September 23, 2022 02:15
Show Gist options
  • Save Mikael-Lovqvist/ac5f62c6d00ec3bceaf9c2d9412775c3 to your computer and use it in GitHub Desktop.
Save Mikael-Lovqvist/ac5f62c6d00ec3bceaf9c2d9412775c3 to your computer and use it in GitHub Desktop.
Move files to a target location and then symlink them back. If there is a lot of files and expansion would be too long for the command line you can use single quotes such as: mvln '*.json' /path/to/target
#!/bin/bash
#The file system may of course change during these operations but at least the stage test is likely to prevent a half run but not guaranteed
#Out of laziness we may not add flags here and instead make a python script for more advanced use
# https://stackoverflow.com/a/33271194/19961748
target=${@:$#}
files=${*%${!#}}
function stage_file {
target_file="${target}${1}"
if [ -f "${target_file}" ]; then
echo There was already a file present at the target location "${target_file}". >&2
exit 1;
fi
}
function process_file {
target_file="${target}${1}"
source_file="${1}"
mv "${source_file}" "${target}"
ln -s "${target_file}" "${source_file}"
}
function stage_operation {
while read -r line
do
stage_file $line
done < <(ls -d1 $files)
}
function perform_operation {
while read -r line
do
process_file $line
done < <(ls -d1 $files)
}
stage_operation;
perform_operation;
@Mikael-Lovqvist
Copy link
Author

Just realized I am not checking that source files exists in staging so will have to update that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment