by archduck · 4y ago · 0 comments
That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.)
And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo` without the hassle of changing the ownership back.
I never see `lndir` on these lists either. It makes a copy of a directory, but all of the non-directory files in the target are replaced with symlinks back to the source while directories are preserved as-is. Meaning that when you `cd` into it, you are actually landing in a copied structure of the source directory instead of the source directory itself, as would be the case if you just symlinked the source folder.
Once inside, any file you want to modify without affecting the original just needs you to create the symlink into a file, which you can do with `sed -i '' $symlink`. There you have it: effectively a copy of your original directory, with only the modified files actually taking up space (loosely speaking).
Looks like I have a few pull requests to submit.
I was surprised to see it was already installed with coreutils, so I've apparently had it for a while now. Thanks for the heads up.
I always use pax instead of cp
mkdir newdir
cd olddir
pax -rw -pp . ../newdir
preserves permissions and access times cd olddir
pax -rw -pe . ../newdir
preserves ownership, permissions and access times.bash: pax: command not found
[-] pax-20201030_1 POSIX archiving utility pax from MirOS (plus tar and cpio)
https://ftp.debian.org/debian/pool/main/p/pax/Or try BSD.
Other languages have this also. In fact it's a C language system call in Linux, though it only goes one indirection level at a time.
This is useful if you run a script and want to use files contained in the directory where the script is located. This comes up if you have a symlink to the script in any /something/something-else/bin directory.
Use cases:
Script needs its own location for e.g. configuration files.
User of the script needs the location for e.g. another script that needs to be run that isn't in the search path.
dir=$(cd -P -- "$dir" && pwd)
with the usual gotcha that $() strips trailing newlines.What's the use case for this sort of thing?
If you `lndir` your mp3 directory to a functional copy of it, a playground of sorts, you can move things around and rename them without having to worry about scenarios like having to listen to a bunch of mp3s in order to put them back to where they're supposed to be.
When you're satisfied with the re-organization of your files, you can replace your symlinks with the original files. Since none of the directories are symlinked, you never have to worry about `cd`ing into a place you didn't intend to.
What that has turned into is 'checkout the repo and make a new directory structure that symlinks everything back to the original'. The keeps the maintenance burden relatively small as you can easily update the base version, and only need to worry about the files you changed.
I certainly wouldn't recommend using this approach for anything; but it is not as terrible as it sounds.