Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. 1. Replacing all occurrences of one string with another in all files in the current directory:
  2. These are for cases where you know that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the approaches in 2.
  3.  
  4. All sed solutions in this answer assume GNU sed. If using FreeBSD or OS/X, replace -i with -i ''. Also note that the use of the -i switch with any version of sed has certain filesystem security implications and is inadvisable in any script which you plan to distribute in any way.
  5.  
  6. Non recursive, files in this directory only:
  7.  
  8. ```
  9. sed -i -- 's/old/new/g' *
  10. perl -i -pe 's/old/new/g' ./*
  11. ```
  12. (the perl one will fail for file names ending in | or space)).
  13.  
  14. Recursive, regular files (including hidden ones) in this and all subdirectories
  15.  
  16. ```
  17. find . -type f -exec sed -i 's/old/new/g' {} +
  18. ```
  19. If you are using zsh:
  20.  
  21. ```
  22. sed -i -- 's/old/new/g' **/*(D.)
  23. ```
  24. (may fail if the list is too big, see zargs to work around).
  25.  
  26. Bash can't check directly for regular files, a loop is needed (braces avoid setting the options globally):
  27.  
  28. ```
  29. ( shopt -s globstar dotglob;
  30. for file in **; do
  31. if [[ -f $file ]] && [[ -w $file ]]; then
  32. sed -i -- 's/old/new/g' "$file"
  33. fi
  34. done
  35. )
  36. ```
  37. The files are selected when they are actual files (-f) and they are writable (-w).
  38.  
  39. 2. Replace only if the file name matches another string / has a specific extension / is of a certain type etc:
  40. Non-recursive, files in this directory only:
  41.  
  42. ```
  43. sed -i -- 's/old/new/g' *baz* ## all files whose name contains baz
  44. sed -i -- 's/old/new/g' *.baz ## files ending in .baz
  45. ```
  46. Recursive, regular files in this and all subdirectories
  47.  
  48. ```
  49. find . -type f -name "*baz*" -exec sed -i 's/old/new/g' {} +
  50. ```
  51. If you are using bash (braces avoid setting the options globally):
  52.  
  53. ```
  54. ( shopt -s globstar dotglob
  55. sed -i -- 's/old/new/g' **baz*
  56. sed -i -- 's/old/new/g' **.baz
  57. )
  58. ```
  59. If you are using zsh:
  60.  
  61. ```
  62. sed -i -- 's/old/new/g' **/*baz*(D.)
  63. sed -i -- 's/old/new/g' **/*.baz(D.)
  64. ```
  65. The -- serves to tell sed that no more flags will be given in the command line. This is useful to protect against file names starting with -.
  66.  
  67. If a file is of a certain type, for example, executable (see man find for more options):
  68.  
  69. ```
  70. find . -type f -executable -exec sed -i 's/old/new/g' {} +
  71. ```
  72.  
  73. zsh:
  74.  
  75. ```
  76. sed -i -- 's/old/new/g' **/*(D*)
  77. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement