Methods and precautions for creating, modifying and deleting Linux soft links

Create soft link

In Linux, you can use  ln -s [源文件] [软链接文件] the command to create soft links.

  1. First, open the terminal and enter the target path. You can use  pwd the command to view the current path.

  2. Use  mkdir the command to create a  test directory named and use  touch the command to create two test files in that directory, named  test.txt and  val.txt.

    mkdir test
    touch test/test.txt
    touch test/val.txt
    
  3. Next, we will create a  test soft link to the directory, using the following command:

    ln -s test test_chk_ln
    
  4. After completing the creation of soft links, you can use  the ll or  ls -l command to view all files, including soft links.

    ll
    

Modify soft link

To modify the source file or directory pointed to by a soft link, you can use the following command:

ln -snf [新的源文件或目录] [软链接文件]

Make sure to replace  [新的源文件或目录] and  [软链接文件] with the actual path and filename.

Delete soft link

When deleting soft links, make sure to use the correct method to avoid accidentally deleting the actual associated data.

  • Correct way to delete (only delete soft links):

    rm -rf ./test_chk_ln
    

    To prevent accidental deletion, you can ask whether to delete the soft link:

    rm -ri ./test_chk_ln
    
  • Wrong deletion method (the content pointed to by the soft link will also be deleted):

    rm -rf ./test_chk_ln/
    

It is recommended that before performing any deletion operation, confirm whether you want to delete soft links or actual data, and proceed with caution to avoid data loss.

The article is reprinted in: Programming Log

Guess you like

Origin blog.csdn.net/qq_29639425/article/details/131663712