From d4c4463f708bd8b644b730ebbd95c24d096c9286 Mon Sep 17 00:00:00 2001 From: Malin Freeborn Date: Wed, 7 Aug 2024 18:03:14 +0200 Subject: [PATCH] embiggen links --- basics/hard_links.md | 38 +++++++++++++++++++++++ basics/links.md | 18 +++-------- basics/soft_links.md | 72 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 14 deletions(-) create mode 100644 basics/hard_links.md create mode 100644 basics/soft_links.md diff --git a/basics/hard_links.md b/basics/hard_links.md new file mode 100644 index 0000000..e34b084 --- /dev/null +++ b/basics/hard_links.md @@ -0,0 +1,38 @@ +--- +title: "hard links" +tags: [ "basics", "links" ] +--- + +A hard link is one file which exists in multiple locations. + +Each file has an ID, which is kept on the hard disk's partition. +Each hard link has the same ID, because they are the same file. +This ID is called the 'inode'. + +Create a file, and a hard link: + +```bash +fortune > $file_1 +mkdir -p x/y/z/ +ln $file_1 x/y/z/$file_2 +``` +Have a long look at the file with the `-l` flag, and check the inode with `-i`: + +```bash +ls -li $file_1 x/y/z/$file_2 +``` + +Since they are the same file, you can make a change to one, and it changes both: + +```bash +fortune | tee x/y/z/$file_2 +cat $file_1 +cat x/y/z/$file_2 +``` + +# Danger Zone + +- hard links will not work on directories, only standard files and fifos. +- `git` will destroy and remake files, so it will not respect hard links. +- Files cannot have a hard link on another disk partition, because the inode is stored on each partition. + diff --git a/basics/links.md b/basics/links.md index 3c5d028..733dc85 100644 --- a/basics/links.md +++ b/basics/links.md @@ -1,19 +1,9 @@ --- title: "links" -tags: [ "Documentation", "Basics" ] +tags: [ "basics", "links" ] --- -Link from X to Y. -```bash -ln -s X ../otherdir/Y -``` - -If you want a hard link, this will make a single file exist in two locations. -If it is deleted in one location, it continues to exist in the other. - -```bash -ln *X* *Y* -``` - -Both files must be on the same hard drive, as they have the same inode (check this with `ls -i file`). +There are two types: +- [Soft links](soft_links.md) +- [Hard links](hard_links.md) diff --git a/basics/soft_links.md b/basics/soft_links.md new file mode 100644 index 0000000..0af7d04 --- /dev/null +++ b/basics/soft_links.md @@ -0,0 +1,72 @@ +--- +title: "soft links" +tags: [ "basics", "links" ] +--- +A soft link is a file which says how to go to another file. +When a program encounters a soft link, it will make a guess at whether it should ignore it, or try to get to that file. + +To make a soft link to a file in the current directory, linking is easy: + +```bash +fortune > $file_1 +ln -s $file_1 $link_1 +``` + +Now imagine your directory looks like this: + +``` +dir_0/ +├── dir_1 +│   └── file_1 +├── dir_2 +│   └── file_1 +├── file_1 +└── link_1 + +``` + +Inside `dir_1`, making a soft link to `dir_0/file_1` would mean putting the directions to that file: + +```bash +cd dir_1 +ln -s ../file_1 link_1 +``` + +The real content of the file is just '`../file_1`, so making it from another directory would mean writing exactly the same address to that file: + +```bash +ln -s ../file_1 dir_2/link_2 +``` + +Both symlinks are identical, except for the name. + +``` +dir_0/ +├── dir_1 +│   ├── file_1 +│   └── link_1 <-- This one points to ../file_1 +├── dir_2 +│   ├── file_1 +│   └── link_2 <-- This one points to ../file_1 as well. +└── file_2 + +``` + +Since it's just an address, you can delete the original file, then make another. + +```bash +rm file_1 +ls -l dir_1/ +fortune > file_1 +cat dir_2/link_2 +fortune | tee -a file_1 +cat dir_1/link_1 +``` + +Last, let's make a link from `dir_2/link_2` to `dir_1/file_1` (this will delete the old link): + +```bash +ln -s -f ../dir_1/file_1 dir_2/link_2 +cat dir_2/link_2 +``` +