الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

ln

Make links: hard links (a second name for the same data) or symbolic links (a pointer to a path).

المستوى
امتحان RHCSA
المجموعة
الملفات والمجلدات
يحتاج root
لا
الحزمة
coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

  • A hard link is another directory entry for the same inode. Both names are equal; the data stays until the last name is removed. Only for files, and only inside one file system.
  • A symbolic (soft) link is a small file that contains a path. It can point to folders and to other file systems. If the target is removed, the link is broken.

Remember the order: ln -s TARGET LINKNAME — first what exists, then the new name (like cp).

طريقة الكتابة

ln [OPTION]... TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...

الخيارات 16

الأكثر استخدامًا

-s, --symbolic
Make a symbolic link instead of a hard link.
-f, --force
Remove an existing target name first.
-n, --no-dereference
If LINK_NAME is a link to a folder, treat it as a normal file (use with -f to replace the link).
-r, --relative
With -s: make the link path relative to the link's place.
-i, --interactive
Ask before removing an existing name.
-v, --verbose
Print each link made.
-t, --target-directory=dir
Make the links in this folder.
-T, --no-target-directory
Treat LINK_NAME as a normal file always.
-L, --logical
For a hard link to a symlink: link to the file the symlink points to.
-P, --physical
For a hard link to a symlink: link to the symlink itself.
-d, -F, --directory
Let root try to hard link folders (almost always fails).
--backup[=<control>]
Make a backup of each existing name.
-b
Like --backup without an argument.
-S, --suffix=suffix
The ending of backup files.
--help
Show help.
--version
Show the version.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Make a symbolic link

    ln -s /etc/httpd/conf/httpd.conf ~/httpd.conf
    ls -l ~/httpd.conf
    lrwxrwxrwx. 1 student student 26 Oct 25 10:00 /home/student/httpd.conf -> /etc/httpd/conf/httpd.conf

    The l and the arrow show the link and its target.

  2. 2Make a hard link

    ln data.txt data-copy.txt
    ls -li data.txt data-copy.txt

    Same inode number, link count 2. Both names are the same file.

  3. 3Replace an existing link (switch a release)

    ln -sfn /srv/app/releases/43 /srv/app/current

    -f replaces, -n treats the old link as a file (not as the folder it points to).

  4. 4Make a relative link that survives moving the tree

    ln -sr /srv/app/shared/config.yml /srv/app/current/config.yml

    The link stores a path like ../shared/config.yml.

  5. 5Link a program into your PATH

    ln -s /opt/tool/bin/tool ~/.local/bin/tool

    Now you can run tool from anywhere.

  6. 6Find broken links

    find /etc -xtype l

    Links whose target is gone. Clean them after removing software.

  7. 7Show where a link points

    readlink -f /usr/bin/python3

    -f follows all links to the final file.

  8. 8Keep log files of an app on a bigger disk

    mv /var/lib/app/logs /data/app-logs && ln -s /data/app-logs /var/lib/app/logs

    The app still writes to the old path. Check SELinux labels on the new place.

  9. 9Make several links in a folder

    ln -s -t /usr/local/bin /opt/tools/bin/*

    One link per program in /usr/local/bin.

  10. 10See how many names a file has

    stat -c '%h %n' /etc/passwd

    %h = the number of hard links.

  11. 11Show each link that is made

    ln -sv /etc/os-release ~/os
    '/home/student/os' -> '/etc/os-release'

    Clear output for scripts and logs.

انتبه

  • The order is TARGET then LINK. Swapping them is the most common mistake.
  • A relative target in ln -s is relative to the link's folder, not to your current folder. Use -r or an absolute path.
  • Hard links do not work across file systems ("Invalid cross-device link") or to folders.
  • A normal user cannot hard link a file of another user (kernel protection fs.protected_hardlinks).

نصائح للحفظ

  • ln -s = shortcut (soft). ln = second name (hard).
  • ln -sfn = replace a link safely.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man ln