{"id":2808,"date":"2018-01-30T13:44:52","date_gmt":"2018-01-30T10:44:52","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-ln-command\/"},"modified":"2018-01-30T13:44:52","modified_gmt":"2018-01-30T10:44:52","slug":"linux-ln-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-ln-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux ln Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p style=\"text-align: left;\">Sometimes, while working on the command line, you need to create links between files. This can be achieved using a dedicated command, dubbed <strong>ln<\/strong>. In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04 machine.<\/p>\n<h2 id=\"linux-ln-command\" style=\"text-align: left;\">Linux ln command<\/h2>\n<p style=\"text-align: left;\">As you&#8217;d have understood by now, the ln command lets you make links between files. Following is the syntax (or rather different syntax available) for this tool:<\/p>\n<p class=\"command\" style=\"text-align: left;\">ln [OPTION]&#8230; [-T] TARGET LINK_NAME\u00a0\u00a0 (1st form)<br \/>\nln [OPTION]&#8230; TARGET\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (2nd form)<br \/>\nln [OPTION]&#8230; TARGET&#8230; DIRECTORY\u00a0\u00a0\u00a0\u00a0 (3rd form)<br \/>\nln [OPTION]&#8230; -t DIRECTORY TARGET&#8230;\u00a0 (4th form)<\/p>\n<p style=\"text-align: left;\">And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre>In\u00a0 the\u00a0 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link \nto TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in \nDIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each \ndestination (name of new link) should not already exist. When creating hard links, each TARGET \nmust exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is \ninterpreted in relation to its parent directory.<\/pre>\n<p style=\"text-align: left;\">The following Q&amp;A-styled examples will give you a better idea on how the ln command works. But before that, it&#8217;s good you get a understanding of what&#8217;s the <a href=\"https:\/\/medium.com\/meatandmachines\/explaining-the-difference-between-hard-links-symbolic-links-using-bruce-lee-32828832e8d3\" target=\"_blank\" rel=\"noopener noreferrer\">difference between hard links and soft links<\/a>.<\/p>\n<h2 id=\"q-how-to-create-a-hard-link-using-ln\" style=\"text-align: left;\">Q1. How to create a hard link using ln?<\/h2>\n<p style=\"text-align: left;\">That&#8217;s pretty straightforward &#8211; all you have to do is to use the <em>ln<\/em> command in the following way:<\/p>\n<p class=\"command\" style=\"text-align: left;\">ln [file] [hard-link-to-file]\n<p style=\"text-align: left;\">For example:<\/p>\n<p class=\"command\" style=\"text-align: left;\">ln test.txt test_hard_link.txt<\/p>\n<p style=\"text-align: left;\"><a id=\"img-ln-hard-link\" class=\"fancybox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/ln-hard-link.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/linux-ln-command-tutorial-for-beginners-5-examples.png\" alt=\"How to create a hard link using ln\" width=\"428\" height=\"107\" title=\"\"><\/a><\/p>\n<p style=\"text-align: left;\">So you can see a hard link was created with the name <em>test_hard_link.txt<\/em>.<\/p>\n<h2 id=\"q-how-to-create-softsymbolic-link-using-ln\" style=\"text-align: left;\">Q2. How to create soft\/symbolic link using ln?<\/h2>\n<p style=\"text-align: left;\">For this, use the -s command line option.<\/p>\n<p class=\"command\" style=\"text-align: left;\">ln -s [file] [soft-link-to-file]\n<p style=\"text-align: left;\">For example:<\/p>\n<p class=\"command\" style=\"text-align: left;\">ln -s test.txt test_soft_link.txt<\/p>\n<p style=\"text-align: left;\"><a id=\"img-ln-soft-link\" class=\"fancybox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/ln-soft-link.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/linux-ln-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to create soft\/symbolic link using ln\" width=\"459\" height=\"112\" title=\"\"><\/a><\/p>\n<p style=\"text-align: left;\">The test_soft_link.txt file is a soft\/symbolic link, as\u00a0<a href=\"https:\/\/askubuntu.com\/questions\/17299\/what-do-the-different-colors-mean-in-ls\" target=\"_blank\" rel=\"noopener noreferrer\">confirmed<\/a> by its sky blue text color.<\/p>\n<h2 id=\"q-how-to-make-ln-remove-existing-destination-files-of-same-name\" style=\"text-align: left;\">Q3. How to make ln remove existing destination files of same name?<\/h2>\n<p style=\"text-align: left;\">By default, ln won&#8217;t let you create a link if a file of the same name already exists in the destination directory.<\/p>\n<p style=\"text-align: left;\"><a id=\"img-ln-file-exists\" class=\"fancybox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/ln-file-exists.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/linux-ln-command-tutorial-for-beginners-5-examples-2.png\" alt=\"ln command example\" width=\"500\" height=\"32\" title=\"\"><\/a><\/p>\n<p style=\"text-align: left;\">However, if you want, you can make ln override this behavior by using the <strong>-f<\/strong> command line option.<\/p>\n<p style=\"text-align: left;\"><a id=\"img-ln-f-option\" class=\"fancybox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/ln-f-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/linux-ln-command-tutorial-for-beginners-5-examples-3.png\" alt=\"How to make ln remove existing destination files of same name\" width=\"500\" height=\"57\" title=\"\"><\/a><\/p>\n<p style=\"text-align: left;\"><em><strong>Note<\/strong>: You can use the <strong>-i<\/strong> command line option if you want to make all this deletion process interactive.<\/em><\/p>\n<h2 id=\"q-how-to-make-ln-create-backup-of-existing-files-with-same-name\" style=\"text-align: left;\">Q4. How to make ln create backup of existing files with same name?<\/h2>\n<p style=\"text-align: left;\">If you don&#8217;t want ln to delete existing files of same name, you can make it create backup of these files. This can be achieved using the <strong>-b<\/strong> command line option. Backup files created this way will contain a tilde (~) towards the end of their name.<\/p>\n<p style=\"text-align: left;\"><a id=\"img-ln-b-option\" class=\"fancybox\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/ln-b-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/01\/linux-ln-command-tutorial-for-beginners-5-examples-4.png\" alt=\"How to make ln create backup of existing files with same name\" width=\"500\" height=\"63\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-create-links-in-directory-other-than-the-current-directory\" style=\"text-align: left;\">Q5. How to create links in directory other than the current directory?<\/h2>\n<p style=\"text-align: left;\">A particular destination directory (other than the current one) can be specified using the <strong>-t<\/strong> command line option. For example:<\/p>\n<p class=\"command\" style=\"text-align: left;\">ls test* | xargs ln -s -t \/home\/himanshu\/Desktop\/<\/p>\n<p style=\"text-align: left;\">The aforementioned command will create links to all test* files (present in the current directory) and put them in the Desktop directory.<\/p>\n<h2 id=\"conclusion\" style=\"text-align: left;\">Conclusion<\/h2>\n<p style=\"text-align: left;\">Agreed, <strong>ln<\/strong> isn&#8217;t something that you&#8217;ll require on daily basis, especially if you&#8217;re a newbie. But it&#8217;s a helpful command to know about, as you never know when it&#8217;d save your day. We&#8217;ve discussed some useful command line options the tool offers. Once you&#8217;re done with these, you can learn more about ln by heading to its <a href=\"https:\/\/linux.die.net\/man\/1\/ln\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div style=\"text-align: left;\">\n<p><b>Share this page:<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the command line, you need to create links between files. This can be achieved using a dedicated command, dubbed ln. In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2808","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2808"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2808\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}