Sed replacement and modification of link files

Source: Internet
Author: User

Sed replacement and modification of link files

  1. Sed replacement and modification of link files

  2. # System and version
  3. [Root @ localhost ~] # Cat/etc/redhat-release
  4. CentOS release 6.8 (Final)
  5. [Root @ localhost ~] # Sed -- version
  6. GNU sed version 4.2.1
  7. Copyright (C) 2009 Free Software Foundation, Inc.
  8. This is free software; see the source for copying conditions. There is NO
  9. Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE,
  10. To the extent permitted by law.

  11. Because sed-I/etc/sysconfig/selinux (The selinux file is a soft link of/etc/selinux/config), SELINUX is not closed after the configuration file is restarted, only then can we find that the sed-I cannot directly modify the soft link file. The following is the consequence of my modifications:
  12. [Root @ localhost ~] # Ll/etc/sysconfig/selinux
  13. Lrwxrwxrwx. 1 root 17 Dec 20 :31/etc/sysconfig/selinux-> ../selinux/config
  14. [Root @ localhost ~] # Ll/etc/selinux/config
  15. -Rw-r --. 1 root 458 Dec 20 :31/etc/selinux/config


  16. [Root @ localhost ~] # Sed-I "s/SELINUX = enforcing/SELINUX = disabled/g"/etc/sysconfig/selinux
  17. [Root @ localhost ~] # Ll/etc/sysconfig/selinux
  18. -Rw-r --. 1 root 457 Dec 22 19: 18/etc/sysconfig/Selin

  19. We found that the linked file is no longer a linked file. Later we checked the sed -- help option and found the following options.

  20. -- Follow-symlinks
  21. Follow symlinks when processing in place; hard links will still be broken.

  22. -I [SUFFIX], -- in-place [= SUFFIX]
  23. Edit files in place (makes backup if extension supplied). The default operation mode is
  24. Break symbolic and hard links. This can be changed with -- follow-symlinks and -- copy.

  25. -C, -- copy
  26. Use copy instead of rename when shuffling files in-I mode. While this will avoid breaking
  27. Links (symbolic or hard), the resulting editing operation is not atomic. This is rarely
  28. Desired mode; -- follow-symlinks is usually enough, and it is both faster and more secure.



  29. # Test
  30. [Root @ localhost ~] # Echo "test"> test
  31. [Root @ localhost ~] # Ln-s test-zsq
  32. [Root @ localhost ~] # Ll-I test-zsq # Link file
  33. 260727 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test
  34. [Root @ localhost ~] # Cat test-zsq
  35. Test

  36. # If you delete the link directly, the linked file will become invalid.
  37. [Root @ localhost ~] # Sed-I "s/test // g" test-zsq
  38. [Root @ localhost ~] # Ll-I test-zsq
  39. 260726-rw-r --. 1 root 1 Dec 22 19: 29 test-zsq

  40. # Re-add and re-test, and add the-c option
  41. [Root @ localhost ~] # Rm-rf test-zsq
  42. [Root @ localhost ~] # Ln-s test-zsq
  43. [Root @ localhost ~] # Ll-I test-zsq
  44. 260726 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test
  45. [Root @ localhost ~] # Echo "test"> test
  46. [Root @ localhost ~] # Sed-I-c '/test/d' test-zsq
  47. [Root @ localhost ~] # Ll-I test-zsq
  48. 260726 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test

  49. # -- Follow-symlinks Option
  50. [Root @ localhost ~] # Rm-rf test-zsq
  51. [Root @ localhost ~] # Ln-s test-zsq
  52. [Root @ localhost ~] # Echo "test"> test
  53. [Root @ localhost ~] # Cat test
  54. Test
  55. [Root @ localhost ~] # Sed-I -- follow-symlinks '/test/d' test
  56. [Root @ localhost ~] # Ls-l test-zsq
  57. Lrwxrwxrwx. 1 root 4 Dec 22 19: 50 test-zsq-> test
  58. [Root @ localhost ~] # Cat test


  59. -- Follow-symlinks is faster and safer than-c.
  60. -C, -- copy
  61. Use copy instead of rename when shuffling files in-I mode.
  62. While this will avoid breaking links (symbolic or hard),
  63. Resulting editing operation is not atomic. This is rarely
  64. The desired mode; -- follow-symlinks is usually enough, and
  65. It is both faster and more secure.


  66. The connection file/etc/rc. local is also a soft connection of/etc/rc. d/rc. local. Use sed to add or delete the startup service.
  67. It is possible that the configured link file becomes invalid and the service cannot be started.


  68. Running the same operations in the centos 5.x series does not produce similar results.
  69. It is found that the sed version is different, and the centos 5 series still uses the old version of sed, without the -- follow-symlinks similar options.

  70. [Root @ DNS ~] # Sed -- version
  71. GNU sed version 4.1.5
  72. Copyright (C) 2003 Free Software Foundation, Inc.
  73. This is free software; see the source for copying conditions. There is NO
  74. Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE,
  75. To the extent permitted by law.
  76. [Root @ DNS ~] # Sed -- help
  77. Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  78. -N, -- quiet, -- silent
  79. Suppress automatic printing of pattern space
  80. -E script, -- expression = script
  81. Add the script to the commands to be executed
  82. -F script-file, -- file = script-file
  83. Add the contents of script-file to the commands to be executed
  84. -I [SUFFIX], -- in-place [= SUFFIX]
  85. Edit files in place (makes backup if extension supplied)
  86. -C, -- copy
  87. Use copy instead of rename when shuffling files in-I mode
  88. (Avoids change of input file ownership)
  89. -L N, -- line-length = N
  90. Specify the desired line-wrap length for the 'l' command
  91. -- Posix
  92. Disable all GNU extensions.
  93. -R, -- regexp-extended
  94. Use extended regular expressions in the script.
  95. -S, -- separate
  96. Consider files as separate rather than as a single continuous
  97. Long stream.
  98. -U, -- unbuffered
  99. Load minimal amounts of data from the input files and flush
  100. The output buffers more often
  101. -- Help display this help and exit
  102. -- Version output version information and exit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.