Last active
February 9, 2025 09:55
-
-
Save stephenchew/94c6dd21b77f426ac1b7619183cf2720 to your computer and use it in GitHub Desktop.
Create hardlink recursively
This file contains hidden or 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 | |
| SRC_FOLDER="$1" | |
| DST_FOLDER="$2" | |
| if [[ -z "$SRC_FOLDER" || -z "$DST_FOLDER" ]]; then | |
| echo "Usage: hardlink.sh <folder_or_file_name> <dest_folder>" | |
| echo "e.g: hardlink.sh sample-folder nice-storage" | |
| echo "Hard link will be created in nice-storage/sample-folder" | |
| exit 0 | |
| fi | |
| # This function takes in 2 parameters | |
| # $1 is the source file or folder | |
| # $2 is the destination folder | |
| function loop_and_create_hardlink() { | |
| local CURR_SRC_PATH="$1" | |
| local CURR_DST_FOLDER="$2" | |
| if [[ -d "$CURR_SRC_PATH" ]]; then | |
| for i in "$CURR_SRC_PATH"/*; do | |
| loop_and_create_hardlink "$i" "$CURR_DST_FOLDER/$i" | |
| done | |
| elif [[ -f "$CURR_SRC_PATH" ]]; then | |
| BASE_DIR="$(dirname "${CURR_SRC_PATH}")" | |
| mkdir -p "$DST_FOLDER/$BASE_DIR" | |
| # filename=$(basename $i) | |
| # ext="${filename##*.}" | |
| # if [[ "$ext" == "txt" || "$ext" == "nfo" ]]; then | |
| # continue | |
| # fi | |
| ln -v "${CURR_SRC_PATH}" "$DST_FOLDER/$CURR_SRC_PATH" | |
| # echo 'ln -v' "${CURR_SRC_PATH}" "$DST_FOLDER/$CURR_SRC_PATH" | |
| else | |
| echo "$CURR_SRC_PATH does not exist" | |
| exit 1 | |
| fi | |
| } | |
| loop_and_create_hardlink "$SRC_FOLDER" "$DST_FOLDER" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To Do
--flattenoption where a folder will not be created in the destination folder.