4 ms·
I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its
by archduck 4y ago
I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry.
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.
- hummusFiend 4y agoFWIW `realpath` on macOS should be functionally equivalent to `readlink -f` - particularly if you ignore all the other functionality `readlink` provides.
- archduck 4y agoNice, thank you! I did not know about `realpath`. Piping absolute paths and copying them to the clipboard is a pattern I use a lot, so this tip will get substantial mileage out of me.
- ineedasername 4y ago>a copy of your original directory, with only the modified files actually taking up space What's the use case for this sort of thing?
- gizmo686 4y agoAt work, we have a codebase that is used by numerous projects. The solution for project specific changes is 'checkout the repo, and make your changes locally'. 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.
- ttyprintk 4y ago‘git worktree’ helps juggling multiple branches.
- archduck 4y agoWhen I need to shuffle around and/or rename my media files, for instance, it's risky to operate on the originals themselves. I've screwed up hundreds of mp3 files by issuing a bad `rename` command. I've lost the hierarchical structures of genres and artists and albums and such by accidentally moving them into the same directory together. And so on. 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.
- Phrodo_00 4y agoYou could use hardlinks for this, though (as long as both places are in the same filesystem). Then when you're finishes you just delete the old folder.
- archduck 4y agoHuh. As a hardlink avoider, I never even thought of this. (I don't have a good reason for avoiding hardlinks - it's mainly just because I didn't grok them enough to predict their behavior. When you teach yourself, you gotta expect some gaps!) Thanks for the suggestion.
- pixelbeat__ 4y agoNote realpath is the preferred interface these days. For some history see: https://unix.stackexchange.com/a/136527 https://unix.stackexchange.com/a/136527
- archduck 4y agoWhoa, realpath includes some flags for relative paths as well! That's bad ass. 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.
- JJMcJ 4y ago> `readlink -f` 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.
- 1vuio0pswjnm7 4y ago"And `cp -a`, which is like `cp -r` which leaves permissions intact - meaning you can prepend `sudo` without the hassle of changing ownership back." 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.
- f1refly 4y ago[fire@host ~]$ pax bash: pax: command not found
- 1vuio0pswjnm7 4y agoxbps-query -Rs pax [-] pax-20201030_1 POSIX archiving utility pax from MirOS (plus tar and cpio) https://ftp.debian.org/debian/pool/main/p/pax/ https://ftp.debian.org/debian/pool/main/p/pax/ Or try BSD.
- 1vuio0pswjnm7 4y agohttps://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/p...
- jwilk 4y agoFor canonicalizing directories, you can use this portably: dir=$(cd -P -- "$dir" && pwd) with the usual gotcha that $() strips trailing newlines.
- coned88 4y agoWhat does `$()`?