14 Ağustos 2008 Perşembe

Find and Replace Command in Linux

Linux'de dosyalar içinde toplu "find and replace" işlemi yapmak için oldukça kullanışlı bir komut:


You can do a find and replace on files from the Linux shell using the find and sed commands. The first example below shows doing a find and replace only in the current directory while the second example below shows doing a find and replace recursing into subdirectories…

  1. Replace “www.trendics.com” with “tools.trendics.com” in all html files in the current directory…
  2. find . -maxdepth 1 -name "*.html" -type f -exec sed -i 's/www.trendics.com/tools.trendics.com/' {} \;

  3. Replace “www.trendics.com” with “tools.trendics.com” in all text files and all subdirectories…
  4. find . -name "*.txt" -type f -exec sed -i 's/www.trendics.com/tools.trendics.com/' {} \;

Here is how this works…

  • The dot after the find command specifies to start in the current directory
  • The -maxdepth 1 specifies to only include the current directory
  • The -name "*.txt" switch specifies to only find txt files
  • The -type f specifies to only match files
  • The -exec xyz {} \; specifies to execute xyz for each file where xyz is a sed command specifying to substitute “tools.trendics.com” for “www.trendics.com”

Kaynak: Trendics Blog


Hiç yorum yok: