Sed replacement and modification of link files
- Sed replacement and modification of link files
- # System and version
- [Root @ localhost ~] # Cat/etc/redhat-release
- CentOS release 6.8 (Final)
- [Root @ localhost ~] # Sed -- version
- GNU sed version 4.2.1
- Copyright (C) 2009 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE,
- To the extent permitted by law.
- 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:
- [Root @ localhost ~] # Ll/etc/sysconfig/selinux
- Lrwxrwxrwx. 1 root 17 Dec 20 :31/etc/sysconfig/selinux-> ../selinux/config
- [Root @ localhost ~] # Ll/etc/selinux/config
- -Rw-r --. 1 root 458 Dec 20 :31/etc/selinux/config
- [Root @ localhost ~] # Sed-I "s/SELINUX = enforcing/SELINUX = disabled/g"/etc/sysconfig/selinux
- [Root @ localhost ~] # Ll/etc/sysconfig/selinux
- -Rw-r --. 1 root 457 Dec 22 19: 18/etc/sysconfig/Selin
- We found that the linked file is no longer a linked file. Later we checked the sed -- help option and found the following options.
- -- Follow-symlinks
- Follow symlinks when processing in place; hard links will still be broken.
- -I [SUFFIX], -- in-place [= SUFFIX]
- Edit files in place (makes backup if extension supplied). The default operation mode is
- Break symbolic and hard links. This can be changed with -- follow-symlinks and -- copy.
- -C, -- copy
- Use copy instead of rename when shuffling files in-I mode. While this will avoid breaking
- Links (symbolic or hard), the resulting editing operation is not atomic. This is rarely
- Desired mode; -- follow-symlinks is usually enough, and it is both faster and more secure.
-
-
- # Test
- [Root @ localhost ~] # Echo "test"> test
- [Root @ localhost ~] # Ln-s test-zsq
- [Root @ localhost ~] # Ll-I test-zsq # Link file
- 260727 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test
- [Root @ localhost ~] # Cat test-zsq
- Test
- # If you delete the link directly, the linked file will become invalid.
- [Root @ localhost ~] # Sed-I "s/test // g" test-zsq
- [Root @ localhost ~] # Ll-I test-zsq
- 260726-rw-r --. 1 root 1 Dec 22 19: 29 test-zsq
- # Re-add and re-test, and add the-c option
- [Root @ localhost ~] # Rm-rf test-zsq
- [Root @ localhost ~] # Ln-s test-zsq
- [Root @ localhost ~] # Ll-I test-zsq
- 260726 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test
- [Root @ localhost ~] # Echo "test"> test
- [Root @ localhost ~] # Sed-I-c '/test/d' test-zsq
- [Root @ localhost ~] # Ll-I test-zsq
- 260726 lrwxrwxrwx. 1 root 4 Dec 22 test-zsq-> test
- # -- Follow-symlinks Option
- [Root @ localhost ~] # Rm-rf test-zsq
- [Root @ localhost ~] # Ln-s test-zsq
- [Root @ localhost ~] # Echo "test"> test
- [Root @ localhost ~] # Cat test
- Test
- [Root @ localhost ~] # Sed-I -- follow-symlinks '/test/d' test
- [Root @ localhost ~] # Ls-l test-zsq
- Lrwxrwxrwx. 1 root 4 Dec 22 19: 50 test-zsq-> test
- [Root @ localhost ~] # Cat test
- -- Follow-symlinks is faster and safer than-c.
- -C, -- copy
- Use copy instead of rename when shuffling files in-I mode.
- While this will avoid breaking links (symbolic or hard),
- Resulting editing operation is not atomic. This is rarely
- The desired mode; -- follow-symlinks is usually enough, and
- It is both faster and more secure.
- 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.
- It is possible that the configured link file becomes invalid and the service cannot be started.
- Running the same operations in the centos 5.x series does not produce similar results.
- 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.
- [Root @ DNS ~] # Sed -- version
- GNU sed version 4.1.5
- Copyright (C) 2003 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE,
- To the extent permitted by law.
- [Root @ DNS ~] # Sed -- help
- Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
- -N, -- quiet, -- silent
- Suppress automatic printing of pattern space
- -E script, -- expression = script
- Add the script to the commands to be executed
- -F script-file, -- file = script-file
- Add the contents of script-file to the commands to be executed
- -I [SUFFIX], -- in-place [= SUFFIX]
- Edit files in place (makes backup if extension supplied)
- -C, -- copy
- Use copy instead of rename when shuffling files in-I mode
- (Avoids change of input file ownership)
- -L N, -- line-length = N
- Specify the desired line-wrap length for the 'l' command
- -- Posix
- Disable all GNU extensions.
- -R, -- regexp-extended
- Use extended regular expressions in the script.
- -S, -- separate
- Consider files as separate rather than as a single continuous
- Long stream.
- -U, -- unbuffered
- Load minimal amounts of data from the input files and flush
- The output buffers more often
- -- Help display this help and exit
- -- Version output version information and exit