Bash: Create hardlink if destination is inside same volume, copy if not -
Bash: Create hardlink if destination is inside same volume, copy if not -
my bash script makes copies of files multiple directories.
in order save space , maximize speed, prefer create hardlinks instead of copies, since copies need remain identical during life anyway.
the script ran in different computers, though, , there may case destination directory exists in different volume origin's. in such case, cannot create hardlink , need re-create file.
how check if origin , destination directory exist in same volume, either hard link or re-create depending on it?
a simple way so, seek both:
ln "$from" "$to" || cp "$from" "$to"
depending upon purposes, creating reference re-create (which lightweight hardlinked file, allows 2 copies edited/diverge in future) might work:
cp --reflink=auto "$from" "$to"
but, can obtain device filesystem's device id using stat
:
if [ $(stat -c %d "$from") = $(stat -c %d "$target_dir") ]; ln "$from" "$target_dir"/ else cp "$from" "$target_dir"/ fi
bash volume hardlink
Comments
Post a Comment