Last active
October 19, 2018 23:34
-
-
Save Deena-B/5e2123058923e7bbd5821a6ef8745801 to your computer and use it in GitHub Desktop.
Symlink_vs_Hard_link.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Symlink vs Hard link and Move vs Copy Experiments\n\nDate of most recent edit: 2018-10-17 " | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%cd ~/Documents/Work/Programming/Blog", | |
"execution_count": 200, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show where I am\n!pwd", | |
"execution_count": 201, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# make a new directory for this experiment\n# FS stands for File System \n!mkdir FSLink_exp01", | |
"execution_count": 202, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show that I made my new directory\n!ls", | |
"execution_count": 203, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "\u001b[34mFSLink_exp01\u001b[m\u001b[m Symlink vs Hard link.ipynb\r\nGitHub Pull Request.ipynb\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# move into my new directory\n%cd FSLink_exp01/", | |
"execution_count": 204, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog/FSLink_exp01\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show that I am in my new directory\n!pwd", | |
"execution_count": 205, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog/FSLink_exp01\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# create a text file\n!touch exp_file.txt", | |
"execution_count": 206, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# add text to my file\n!echo \"Hello Deena\" > exp_file.txt", | |
"execution_count": 207, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show file text\n!cat exp_file.txt", | |
"execution_count": 208, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Hello Deena\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# note that the filename \"exp_file.txt\" is a hard link to my file\n!ls -lsi exp_file.txt\n# the items below are (in the same order as indicated below)\n# inode_number number_of_data_blocks file_read_write_permitted number_of_hardlinks owner group (what_does_12_represent?) date_created time_created(or updated) file_name\n# see ls man for more details", | |
"execution_count": 209, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "8608615936 8 -rw-r--r-- 1 Ds2007laptop staff 12 Oct 17 00:17 exp_file.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# hard link another filename to the same file\n!ln exp_file.txt second_hardlink.txt", | |
"execution_count": 210, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# note that there are now two hardlinks \n!ls -lsi", | |
"execution_count": 211, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 16\r\n8608615936 8 -rw-r--r-- 2 Ds2007laptop staff 12 Oct 17 00:17 exp_file.txt\r\n8608615936 8 -rw-r--r-- 2 Ds2007laptop staff 12 Oct 17 00:17 second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%writefile second_hardlink.txt\nBye Deena", | |
"execution_count": 212, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Overwriting second_hardlink.txt\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# view text in file\n!cat second_hardlink.txt", | |
"execution_count": 213, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Bye Deena" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# note that the text of my original file has also been overwritten\n!cat exp_file.txt", | |
"execution_count": 214, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Bye Deena" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# make a symbolic link aka symlink\n!ln -s exp_file.txt first_symlink.txt", | |
"execution_count": 215, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show status of new and old files\n!ls -lsi\n# note that a symlink does not point to any data blocks\n# note that the symlink only knows about one of the hard links\n# note that the symlink file name includes a pointer to its hard link", | |
"execution_count": 216, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 16\r\n8608615936 8 -rw-r--r-- 2 Ds2007laptop staff 9 Oct 17 00:17 exp_file.txt\r\n8608615952 0 lrwxr-xr-x 1 Ds2007laptop staff 12 Oct 17 00:17 \u001b[35mfirst_symlink.txt\u001b[m\u001b[m -> exp_file.txt\r\n8608615936 8 -rw-r--r-- 2 Ds2007laptop staff 9 Oct 17 00:17 second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show that the symlink leads to the same file text\n!cat first_symlink.txt", | |
"execution_count": 217, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Bye Deena" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "change the text via the symlink \n(The double magic command doesn't work if I put a comment \n before it, in the same cell.)" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "%%writefile first_symlink.txt\nWe're all connected to the same data blocks", | |
"execution_count": 218, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Overwriting first_symlink.txt\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show the text is altered for other file links\n!cat exp_file.txt", | |
"execution_count": 219, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "We're all connected to the same data blocks" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!cat second_hardlink.txt", | |
"execution_count": 220, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "We're all connected to the same data blocks" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show a hard link remains even when you move its filename\n!pwd", | |
"execution_count": 221, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog/FSLink_exp01\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# note that tmp is a special name for a folder\n# a tmp folder will be erased when you turn your computer off and on\n# so be careful!!!\n!mkdir tmp", | |
"execution_count": 222, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!ls", | |
"execution_count": 223, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "exp_file.txt \u001b[35mfirst_symlink.txt\u001b[m\u001b[m second_hardlink.txt \u001b[34mtmp\u001b[m\u001b[m\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# move filename\n!mv second_hardlink.txt tmp/second_hardlink.txt", | |
"execution_count": 224, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!ls", | |
"execution_count": 225, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "exp_file.txt \u001b[35mfirst_symlink.txt\u001b[m\u001b[m \u001b[34mtmp\u001b[m\u001b[m\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# check that filename ended up where I ment to put it\n%ls tmp", | |
"execution_count": 226, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# view contents of file\n!cat tmp/second_hardlink.txt", | |
"execution_count": 227, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "We're all connected to the same data blocks" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# move symlink\n!mv first_symlink.txt tmp/first_symlink.txt", | |
"execution_count": 228, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!ls", | |
"execution_count": 229, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "exp_file.txt \u001b[34mtmp\u001b[m\u001b[m\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!ls tmp", | |
"execution_count": 230, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "\u001b[35mfirst_symlink.txt\u001b[m\u001b[m second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# view contents of file\n!cat tmp/first_symlink.txt", | |
"execution_count": 231, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "cat: tmp/first_symlink.txt: No such file or directory\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Moving a symlink destroys its link! " | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show the symlink filname is still present\n!ls -lsi tmp/", | |
"execution_count": 232, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 8\r\n8608615952 0 lrwxr-xr-x 1 Ds2007laptop staff 12 Oct 17 00:17 \u001b[35mfirst_symlink.txt\u001b[m\u001b[m -> exp_file.txt\r\n8608615936 8 -rw-r--r-- 2 Ds2007laptop staff 43 Oct 17 00:17 second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!pwd", | |
"execution_count": 233, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog/FSLink_exp01\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# move the symlink filename back\n!mv tmp/first_symlink.txt first_symlink.txt", | |
"execution_count": 234, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!ls -l", | |
"execution_count": 235, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 8\r\n-rw-r--r-- 2 Ds2007laptop staff 43 Oct 17 00:17 exp_file.txt\r\nlrwxr-xr-x 1 Ds2007laptop staff 12 Oct 17 00:17 \u001b[35mfirst_symlink.txt\u001b[m\u001b[m -> exp_file.txt\r\ndrwxr-xr-x 3 Ds2007laptop staff 96 Oct 17 00:19 \u001b[34mtmp\u001b[m\u001b[m\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show symlink is reconnected\n!cat first_symlink.txt", | |
"execution_count": 236, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "We're all connected to the same data blocks" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# move symlink again\n!mv first_symlink.txt tmp/first_symlink.txt", | |
"execution_count": 237, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# lets change the owner and group of the first_symlink.txt filename\n# see https://www.thegeekstuff.com/2012/06/chown-examples\n!sudo chown -h root:wheel tmp/first_symlink.txt", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# to change the owner:group to root:wheel you need to use sudo\n# but I don't know how to enter my user's password into a Jupyter notebook\n# so I entered the following line in my terminal (from the right directory)\n# !sudo chown -h root:wheel tmp/first_symlink.txt", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!pwd", | |
"execution_count": 238, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "/Users/Ds2007laptop/Documents/Work/Programming/Blog/FSLink_exp01\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# show tha the owner & group have changed\n!ls -l tmp/", | |
"execution_count": 239, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 8\r\nlrwxr-xr-x 1 root wheel 12 Oct 17 00:17 \u001b[35mfirst_symlink.txt\u001b[m\u001b[m -> exp_file.txt\r\n-rw-r--r-- 2 Ds2007laptop staff 43 Oct 17 00:17 second_hardlink.txt\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# copy first_symlink file name with flag -a\n!cp -a tmp/first_symlink.txt ./first_symlink.txt", | |
"execution_count": 240, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# note that cp with flag -a changed the owner and group\n!ls -l", | |
"execution_count": 241, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "total 8\r\n-rw-r--r-- 2 Ds2007laptop staff 43 Oct 17 00:17 exp_file.txt\r\nlrwxr-xr-x 1 Ds2007laptop staff 12 Oct 17 00:17 \u001b[35mfirst_symlink\u001b[m\u001b[m -> exp_file.txt\r\ndrwxr-xr-x 4 Ds2007laptop staff 128 Oct 17 00:19 \u001b[34mtmp\u001b[m\u001b[m\r\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# reconnected again :)\n!cat first_symlink.txt", | |
"execution_count": 242, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "We're all connected to the same data blocks" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "When I did this in reality, something blocked me from moving my equivalent of the first_symlink.txt filename back into its original folder. We thought that was because I was unable to move a filename owned by root into a directory owned by a User, without using sudo, but it turns out the directory that I moved it into was also owned by my user, so I don't know why I couldn't move my file back. I moved my file in the first place because I got an error when I ran `brew update` the error I got is pasted below: " | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Error: The `brew link` step did not complete successfully\nThe formula built, but is not symlinked into /usr/local\nCould not symlink bin/git\nTarget /usr/local/bin/git\nalready exists. You may want to remove it:\n rm '/usr/local/bin/git'\n\nTo force the link and overwrite all conflicting files:\n brew link --overwrite git\n\nTo list all files that would be deleted:\n brew link --overwrite --dry-run git" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Instead of totally removing the symlink, we moved it to a tmp folder. Then re-ran `brew update`. Replaced the filenames with new owner (switched from root to user in this case). Then re-ran `brew update` again and the error message was no more. " | |
} | |
], | |
"metadata": { | |
"gist": { | |
"id": "5e2123058923e7bbd5821a6ef8745801", | |
"data": { | |
"description": "Symlink_vs_Hard_link.ipynb", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.7.0", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/5e2123058923e7bbd5821a6ef8745801" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment