Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

วิธีหา Broken Symlinks ใน Linux

Symbolic Links หรือ symlinks เป็นวิธีลดความซับซ้อนในการจัดการ Linux system ค่อนข้าง common บน servers หรือ workstations ที่ linking one directory to another ใช้เพื่อให้ applications ส่งข้อมูลไปอีกที่ใน directory tree โดยไม่เปลี่ยน configurations - essential สำหรับ maintaining a healthy system แต่ปัญหากับการใช้ symlinks คือไม่มี guarantee ว่าคุณจะมี both directories ใน link นั้นเสมอไป นี่คือวิธีหาและแก้ไข broken symlinks ใน Linux

มี incredibly helpful application ชื่อว่า symlinks ใน most repositories เป็น simple command-line utility ที่ให้ helpful output และ options สำหรับ deleting those same broken symlinks

ติดตั้งด้วยคำสั่งต่อไปนี้:

# For Debian/Ubuntu-based distro
sudo apt install symlinks
# For Fedora/CentOS
sudo dnf install symlinks

มีสอง key options สำหรับ symlinks คือ -d ซึ่งจะ delete dangling links และ -r ซึ่งจะ recursively ทำ option ที่ระบุผ่าน subdirectories

ยังสามารถใช้ built-in find tool ใน Linux ได้ นี่คือ less user-friendly example แต่ helpful เพื่อ learn find command และ how it works

แรก สร้าง symbolic link เกี่ยวข้องกับการนำ existing file และใช้คำสั่ง ln link ไปยัง file ที่ยังไม่มีอยู่ จะเป็นตัวอย่างอย่างนี้สำหรับผม

touch test-file.txt
ln -s test-file.txt linked-file.txt

เห็นผ่านคำสั่ง ls ว่า link exists

ตอนนี้ทำให้ symlink นั้นแตก

rm test-file.txt

เห็นว่าแม้จะ remove original file แล้ว คำสั่ง ls -l ยัง report link นี้ นี่คือที่มาของปัญหา ไฟล์เหล่านี้อาจอยู่ใน directories ต่างกัน ทำให้ harder to check หาก original file ยังอยู่

วิธีแก้ broken symlinks คือแค่ delete them ไม่ possible to bring them back จึงแค่ clear them from virtual directory tree

รายงาน broken symlinks ด้วย tool symlinks ใช้คำสั่ง:

symlinks .

สังเกต "." indicating present working directory เปลี่ยนสำหรับ directory ที่พยายาม search Output อาจดูแบบนี้:

dangling: /home/jperkins/linked-file.txt -> test-file.txt

บ่งชี้ว่า "linked-file.txt" dangling และ symlink แตก ในการ delete ใช้คำสั่ง:

symlinks -d .

Output จะดูเหมือนครั้งก่อน แต่จะมีบรรทัด "deleted" เพิ่ม

แก้ broken symlinks ด้วย find ใช้คำสั่ง:

find . -xtype l

สังเกตอีกครั้ง "." representing present working directory จะ produce less user-friendly output แต่ยัง helpful

และเพื่อ delete ให้เพิ่ม option delete

find . -xtype l -delete

ไม่ได้ output สำหรับอันนี้ แต่ถ้ารันอีกครั้งโดยไม่มี -delete option จะไม่หาอะไรได้

แค่นี้คุณสามารถหา broken symlinks และ delete พวกมันได้ง่าย ก่อนที่จะก่อให้เกิด issues เพิ่มเติม มี tutorials เกี่ยวกับ symlinks มากกว่านี้ควร check out สามารถเรียนรู้เพิ่มเติมเกี่ยวกับ difference between a symlink and a hard link และเมื่อไหร่ควรใช้พวกมัน