Skip to main content
Engineering LibreTexts

05-D.7.7: Handling Text Files - ln Command

  • Page ID
    32535
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    The ln Command

    Hard Links

    Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links are more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems. ls -l command shows all the links while the link column shows number of links. Links have actual file contents, and removing any link just reduces the link count, but doesn’t affect other links. We cannot create a hard link for a directory to avoid recursive loops. If the original file is removed, then the link will still show the content of the file.

    Command to create a hard link is:

    Syntax:

    ln [,OPTION/]... [,-T/] ,TARGET LINK_NAME/     (1st form)
    ln [,OPTION/]... ,TARGET/                      (2nd form)
    ln [,OPTION/]... ,TARGET/... ,DIRECTORY/       (3rd form)
    ln [,OPTION/]... ,-t DIRECTORY TARGET/...      (4th form)
    

    In the first form, the command creates a link to TARGET with the name LINK_NAME.

    In the second form, the command creates a link to TARGET in the current directory.

    In the third and fourth forms, the command creates links to each TARGET in DIRECTORY. The ln command creates hard links by default, to create symbolic links you must yse the --symbolic option. By default, each destination (name of new link) should not already exist. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

    # A simple link of one file to another name - notice the output of thje ls -li command 
    # showing the file's node number is the same for both files, bacause they are links
    pbmac@pbmac-server $ ln program1.cpp linkprogram1.cpp
    pbmac@pbmac-server $ ls -li
    total 0
    12987377 -rw-r--r-- 2 pbmac pbmac 0 Aug  4 17:42 linkprogram1.cpp
    12987377 -rw-r--r-- 2 pbmac pbmac 0 Aug  4 17:42 program1.cpp
    
    # link the file to a file with the same name in the directory subFolder
    pbmac@pbmac-server $ ln program1.cpp subFolder/
    pbmac@pbmac-server $ ls subFolder/
    program1.cpp
    
    # link the file to a different name in the directory subFolder
    pbmac@pbmac-server $ ln program1.cpp subFolder/anotherLink.cpp
    pbmac@pbmac-server $ ls subFolder/
    anotherLink.cpp  program1.cpp
    
    # link a file in a subfolder to a file with the same name in the current folder.
    pbmac@pbmac-server $ ln subFolder/testFile.txt
    pbmac@pbmac-server $ ls
    linkprogram1.cpp  myStuff.txt  program1.cpp  subFolder  testFile.txt
    pbmac@pbmac-server $  
    

    The image shows the concept - two different filenames pointing to the same space on the physical disk.

    2 different names pointing to the same physical disk space - this is a hard link
    Figure \(\PageIndex{1}\): Hard link files. ("hard links" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    Symbolic/Soft Links

    • A symbolic link is similar to the file shortcut feature which is used in Windows Operating systems. Each symbolic linked file contains a separate Inode value that points to the original file. Similar to hard links, any changes to the data in either file is reflected in the other. Symbolic links can be linked across different file systems, although if the original file is deleted or moved, the symbolically linked file will not work correctly (called hanging link).
    • ls -li command shows the node numbers, all linked files will show the first permission column value of l, and the filename field points to original file.
    • Symbolic link contains the path for original file and not the contents.
    • Removing symbolic link doesn’t affect anything, but if removing the original file, the link becomes a “dangling” link which points to nonexistent file.
    • A symbolic link can link to a directory.
    • Link across filesystems: If you want to link files across the filesystems, you can only use symlinks links.

    Symbolic/Soft Link Syntax

    $ ln  -s [original filename] [link name] 

    As mentioned above, the symbolic, or soft link, has a different filename, but the link points back to the original filename, not to the disk drive. Notice in the following example, the node numbers are different signifying that these are two different names; also the filename field shows the link back to program1.cpp.

    # notice the node numbers are different, the first colum of permissions is a 'l', and filename points back to original file
    pbmac@pbmac-server $ ls -li program1.cpp newprogram.cpp 
    12987272 lrwxrwxrwx 1 pbmac pbmac 12 Aug  5 07:55 newprogram.cpp -> program1.cpp
    12987377 -rw-r--r-- 4 pbmac pbmac  0 Aug  4 17:42 program1.cpp
    

    This can be viewed as the shown in the image to the right. The linked filename, file2.txt, points back to the original filename. The original filename points to the physical file on disk.

    symbolic links point back to the original filename, which in turns points to the data
    Figure \(\PageIndex{1}\): Soft links files. ("soft links" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    Adapted from:
    "ln command in Linux with Examples" by Rangeesh_A_R, Geeks for Geeks is licensed under CC BY-SA 4.0
    "Soft and Hard links in Unix/Linux" by Akash Gajjar, Geeks for Geeks is licensed under CC BY-SA 4.0


    05-D.7.7: Handling Text Files - ln Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?