The Linux command line replaces the string "Go" in Multiple files in bulk

Source: Internet
Author: User
Tags logical operators

Linux command line bulk replace the string in multiple files "to Baidu Library" one is the Mahuinan method, one is the Sumly method, one is the 30T method is as follows: First, Mahuinan method: The SED command allows you to bulk replace strings in multiple files. Sed-I."s/Original string/new string/g"' Grep original string-RL directory ' For example: I want to replace Mahuinan with Huinanma, execute command: sed-I."s/mahuinan/huinanma/g" 'grep mahuinan-rl/www'This is the most simple batch replacement string command for Linux at the moment! The specific format is as follows: SED-I."s/oldstring/newstring/g"' Grep oldstring-rl/path ' instance code: SED-I."s/size/Sun and moon/g"' grep size how many-rl/usr/aa ' sed-I."s/size/Sun and moon/g"' grep size how many-rl.' II, sumly law Enforcement order: Perl-p-i-E"s/china/sumly/g"/www/*. htm/www/*.txt above means to replace "China" in all HTM and txt files in the WWW folder with "sumly" three, 30T Law execution command: perl-pi-e ' s|baidu|30t|g ' find/www- Type F ' above means that all files under the WWW folder, no extension, all "Baidu" are replaced with "30T" Linux bulk Replacement file Content command time: 2010-04-08 22:14:02 Category: Technical access: 351 views RSS 2.0 Reviews # Find-name *.php |xargs perl-pi-e ' s|21andy.com|www.21andy.com|g '; the above indicates that all files with the suffix. php are searched under the current directory and subdirectories, and the string 2 1andy.com replaced with www.21andy.com can also use the SED command, as follows: # sed-i "s/21andy.com/www.21andy.com/g" ' grep 21andy.com-rl./' Recent work to batch replacement life Order (Linux) May 9, 2010 read: 559 Posted in: Log Comment Bulk Replace file name a file: Find-name ' a.php '-exec cp/a.php {}-f \; bulk copy file.php to directory named B folder: find -name b-type d-exec cp/file.php {}-f \; Advanced usage plus perl, bulk replace some content in files named file.php: Find-name ' file.php ' | Xargs perl-pi-e "s| Original content | What to replace |g" Bulk Delete folder with file name file.php: Find-name ' file.php ' | Xargs rm-rffind Command Explanation: The Find command is used to search for files in the directory structure and perform the specified actions. This command provides quite a few look-up conditions and is powerful. Syntax: Find [start directory] Find conditions Operation Description: The Find command starts at the specified starting directory, searches its subdirectories recursively, finds the file that satisfies the search criteria, and takes relevant action on it. The search condition provided by this command can be a compound condition consisting of a logical operator not, and, or. logical operators and, or, notThe meaning is: (1) and: Logic and, in the command with "-a", is the system default option, indicating that only when the given conditions are satisfied, the search condition is satisfied. For example: Find–name ' tmp ' –xtype c-user ' inin '% the command looks for all the files that are satisfied with the three given conditions (2) or: Logical OR, expressed in the command "-o". The operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example: Find–name ' tmp ' –o–name ' mina* '% this command queries the file name ' tmp ' or all files matching ' mina* '. (3) Not: Logical, in command "! Said The operator indicates that a file was found that did not meet the given criteria. For example: Find! –name ' tmp '% this command queries the file name for all files that are not ' tmp '. It is necessary to note that these options can be enclosed in parentheses when using a number of logical options. In order to avoid the shell itself being misunderstood, it is necessary to add the escape character "\" to remove the meaning of the parentheses before the session number. Example: Find \ (–name ' tmp ' –xtype c-user ' Inin ') the options for this command mean the following: first, the n values in each of the following options can be entered in three ways, assuming that N is 20, then +20 means 20 (21,22,23, etc.)-20 means 20 Previous (19,18,17, etc) 20 means that exactly 20 is found by name and file attribute. -name ' string ' Find file name matches all files of the given string, wildcard characters * 、?、 [] in the string. -lname ' string ' Find file name matches all symbolic link files of the given string, wildcard characters * 、?、 [] are available within the string. -gid N finds all files belonging to the user group with ID number n. -uid N finds all files belonging to a user with ID number n. -group ' string ' finds all the files that belong to the user group named the given string. The-user ' string ' finds all the files that belong to the user name of the given string. -empty look for a directory or file of size 0. The-path ' string ' lookup path name matches all the files given to the string, and the wildcard characters * 、?、 [] are available within the string. -perm permissions find files and directories with the specified permissions, which can be represented as 711,644. -size n[bckw] Finds files of the specified file size, the characters following n represent units, the default is B, which represents 512-byte blocks. -type x to find a file of type X, X is one of the following characters: B-block device file C-character device file D catalog file p named pipe (FIFO) F Normal file L Symbolic link file (symbolic Links) s socket file-xtype x is basically the same as-type, but only finds the symbolic link file. Find-amin N on a time-based condition find all files that were accessed N minutes ago. -atime N finds all files that were accessed N days ago. -cmin N finds all files that have been modified for n minutes before the file status. -ctime N finds all files that have been modified by the file status N days ago. -mmin N finds all files where the contents of the file were modified before n minutes. -mtime N finds all files that have been modified by the contents of the file N days ago. The executable operation-exec command name {} Executes the given Linux command on a qualifying file without asking the user if the command needs to be executed. {} indicates that the argument to the command is the found file; the end of the command must end with "\;". The-OK command name {} performs a given Linux command on a qualifying file, unlike Exec, which asks the user if the command needs to be executed. The-ls lists all the files found in detail. The-fprintf file name is written to the specified file. -print Displays the found file name on the standard output device. Please refer to the C language book for-printf format format. Find command instance: find. –name ' main* ' –exec more {} \;% finds all files in the current directory that begin with main and displays the contents of those files. Find. \ (-Name a.out–o–name ' *.O ' \) >–atime +7–exec rm {} \;% Delete all A. Out or *.O files that have not been accessed within the current directory for a week. The "." In the% command Represents the current directory, where find starts with the current directory and finds the files in its subdirectories that meet the criteria specified later. % "\ (" and "\)" denotes parentheses (), where "\" is called an escape character. The reason for this is that for the Shell, (and) there is a different meaning, rather than the use of the combination condition here. % "-name a.out" means to find a file named a.out;% "-name ' *.o '" means to find all files whose names end in. O. The-o between the two-name represents the logical or (or), which is to find the file whose name is A.out or whose name ends with. O. The% find command finds this file under the current directory and its subdirectories, and then determines whether the last access time is 7 days ago (conditional-atime +7), if yes, execute the command rm (-exec rm {} \;) for the file. where {} represents the currently found qualified file name, \; is required by the syntax. % the last \ is the continuation character of the first line in the preceding command. When the command is too long to write on a line, you can enter a \, and then the system will display a, indicating that the user continues to enter the command. In the directory structure of the search file Find command in detail, I hope to help you.

The Linux command line replaces the string "Go" in Multiple files in bulk

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.