Docsity

Esercizio LINUX base, Esercizi di Sistemi di reti

Toggle sidebar
bg1
Hardlink
cp -l luca.txt ../luca.txt
i due luca.txt hanno stesse dimensioni (non è un semplice collegamento)
Se modifico il file anche il suo hard link viene aggiornato
se cancello il file originale l’hard link rimane
l’hard link non occupa il doppio dello spazio su disco perché è un file duplicato
se guardo ls -i luca.txt (inode)
se guardo ls -i ../luca.txt
ho lo stesso inode
Quindi un hard link è un file con lo stesso inode dell’originale, che riporta le stesse dimensioni
dell’originale (senza però realmente occuparle su disco) e si comporta come un collegamento, ma
cancellato il file originale non si libera lo spazio su disco: l’hard link rimane come se fosse una
copia dell’originale
Poi esiste il soft link (che è come il collegamento di windows)
cp -s luca.txt luca2.txt
se cancello il file originale il soft link non sa a cosa puntare
cp -u luca.txt ../ lo copia solo se il sorgente è più nuovo
And, "An example is worth a hundred paragraphs..."
Create two files:
$ touch blah1
$ touch blah2
Enter some data into them:
$ echo "Cat" > blah1
$ echo "Dog" > blah2
And as expected:
$cat blah1; cat blah2
Cat
Dog
Let's create hard and soft links:
$ ln blah1 blah1-hard
$ ln -s blah2 blah2-soft
Let's see what just happened:
$ ls -l
blah1
blah1-hard
blah2
blah2-soft -> blah2
Changing the name of blah1 does not matter:
$ mv blah1 blah1-new
$ cat blah1-hard
Cat
pf3

Anteprima parziale del testo

Scarica Esercizio LINUX base e più Esercizi in PDF di Sistemi di reti solo su Docsity!

Hardlink

cp -l luca.txt ../luca.txt

i due luca.txt hanno stesse dimensioni (non è un semplice collegamento)

Se modifico il file anche il suo hard link viene aggiornato

se cancello il file originale l’hard link rimane

l’hard link non occupa il doppio dello spazio su disco perché è un file duplicato

se guardo ls -i luca.txt (inode)

se guardo ls -i ../luca.txt

ho lo stesso inode

Quindi un hard link è un file con lo stesso inode dell’originale, che riporta le stesse dimensioni

dell’originale (senza però realmente occuparle su disco) e si comporta come un collegamento, ma

cancellato il file originale non si libera lo spazio su disco: l’hard link rimane come se fosse una

copia dell’originale

Poi esiste il soft link (che è come il collegamento di windows)

cp -s luca.txt luca2.txt

se cancello il file originale il soft link non sa a cosa puntare

cp -u luca.txt ../ lo copia solo se il sorgente è più nuovo

And, "An example is worth a hundred paragraphs..."

Create two files:

$ touch blah $ touch blah

Enter some data into them:

$ echo "Cat" > blah $ echo "Dog" > blah

And as expected:

$cat blah1; cat blah Cat Dog

Let's create hard and soft links:

$ ln blah1 blah1-hard $ ln -s blah2 blah2-soft

Let's see what just happened:

$ ls -l blah blah1-hard blah blah2-soft -> blah

Changing the name of blah1 does not matter:

$ mv blah1 blah1-new $ cat blah1-hard Cat

blah1-hard points to the inode, the contents, of the file - that wasn't changed.

$ mv blah2 blah2-new $ ls blah2-soft blah2-soft $ cat blah2-soft cat: blah2-soft: No such file or directory

The contents of the file could not be found because the soft link points to the name,

that was changed, and not to the contents.

Similarly, If blah1 is deleted, blah1-hard still holds the contents; if blah2 is deleted,

blah2-soft is just a link to a non-existing file.

A hardlink isn't a pointer to a file, it's a directory entry (a file) pointing to the same

inode. Even if you change the name of the other file, a hardlink still points to the file.

If you replace the other file with a new version (by copying it), a hardlink will not

point to the new file. You can only have hardlinks within the same filesystem. With

hardlinks you don't have concept of the original files and links, all are equal (think of

it as a reference to an object). It's a very low level concept.

On the other hand, a symlink is actually pointing to another path (a file name); it

resolves the name of the file each time you access it through the symlink. If you

move the file, the symlink will not follow. If you replace the file with another one,

keeping the name, the symlink will point to the new file. Symlinks can span

filesystems. With symlinks you have very clear distinction between the actual file

and symlink, which stores no info beside the path about the file it points to.

Both are pointers to files; the difference is the kind of pointer. A symbolic link points

to another file by name. It has a special mode bit that identifies it as a symbolic link,

and its contents are the name of the real file. Because it just contains a name, that

name does not actually have to exist, or may exist on a different filesystem. If you

replace the named file (change its contents without affecting its name), then the link

still contains the same name, and so now it points to the new file. You can easily

identify a symbolic link and see the name of the file it points to.

A hard link points to the file by inode number. As such, hard links are no different

than the first name of a file. There is no "real" name vs. hard link name; all hard links

are equally valid names for the file. Because of this, the file you link to must actually

exist and be in the same filesystem where you are trying to create the link. If you

delete the original name, then the hard link still points to the same file. Because all

hard links are equally valid name(s) for the file, you can not look at one and see the

other names for the file; to find this, you have to go looking at every file and

compare their inode number to find the other name(s) that have the same inode

number.

You can tell how many names a file has from the output of ls -l. The first number

after the file mode is the link count. A file with more than 1 link has other name(s)

somewhere, and conversely, a file with a link count of only 1 has no (other) hard

links.

A hardlink can only work on the same filesystem, it is simply a different name for the

same inode (files are internally referenced by inodes). A file will only be deleted from

disk when the last link to its inode is gone (you rmd or unlinkd the last link).

Hardlinks usually only work for files, not directories.