Sponsored Content
Top Forums Shell Programming and Scripting Find and Replace in multiple files (Shell script) Post 26664 by LivinFree on Thursday 22nd of August 2002 01:05:39 AM
Old 08-22-2002
I would bet that peter's is faster...

You could make it a split second faster by getting rid of the `ls *` though...

If could just be:
for y in *
since the shell will expand the * into all of the file names...
If you had directories in there, you'd want to use "find" to get only the files...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace files in multiple folders

Hi there, I would like to write a script to automate the copy and renaming of files in multiple dir. I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it. Example: Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to... (6 Replies)
Discussion started by: lodey
6 Replies

2. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies

3. Shell Programming and Scripting

Shell Script to replace tokens in multiple files

I have multiple script files that I have created, that allow me to simply replace a few tokens at the top of the file, and then not have to go through the actual script and change anything. I have about 10 of them, but I was hoping to find a way to write a small script that would allow me to input... (20 Replies)
Discussion started by: cbo0485
20 Replies

4. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

5. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

6. UNIX for Advanced & Expert Users

linux find and replace in multiple files

Is there a reason why all of these examples of linux find and replace in multiple files use find and grep to pipe into sed or perl. Why not just use sed or perl directly with something like this? sed -i 's/echo/burnbaby/g' booboo*... (2 Replies)
Discussion started by: cokedude
2 Replies

7. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

8. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

9. Shell Programming and Scripting

Find and replace in multiple files

Hi, I have php files in main dir and sub dir's as well. I need to find "new mysqli('localhost', 'System', 'xxxxxx', 'System', '3306');" and replace as "new mysqli('localhost', 'unx_sys', 'yyyy', 'unx_sys', '3306');" I tried like: sed 's/new mysqli\(*\)\;$/new... (1 Reply)
Discussion started by: ashokvpp
1 Replies

10. UNIX for Beginners Questions & Answers

Find and replace from multiple files

Hello everybody, I need your help. I have a php site that was expoited, the hacker has injected into many php files a phishing code that was discovered and removed in order to have again a clean code. Now we need to remove from many php files that malware. I need to create a script that find and... (2 Replies)
Discussion started by: ninocap
2 Replies
FASTRM(1)						    InterNetNews Documentation							 FASTRM(1)

NAME
fastrm - Quickly remove a list of files SYNOPSIS
fastrm [-de] [-u|-uN] [-s|-sM] [-c|-cI] base-directory DESCRIPTION
fastrm reads a list of either file names or storage API tokens, one per line, from its standard input and removes them. Storage API tokens are removed via the SMcancel() interface. If a file name is not an absolute path name, it is considered to be relative to base-directory as given on the command line. The base- directory parameter must be a simple absolute pathname (it must not contain multiple consecutive slashes or references to the special directories "." or ".."). fastrm is designed to be faster than the typical "| xargs rm" pipeline when given a sorted list of file names as input. For example, fas- trm will usually chdir(2) into a directory before removing files from it, meaning that if its input is sorted, most names passed to unlink(2) will be simple names. This can substantially reduce the operating system overhead from directory lookups. fastrm assumes that its input is valid and that it is safe to call unlink(2) on every file name it is given. As a safety measure, however, fastrm when running as root will check with stat(2) that a file name doesn't specify a directory before removing it. (In some operating systems, root is allowed to unlink directories, even directories which aren't empty, which can cause file system corruption.) The input to fastrm should always be sorted -- or even better be in the order file names are output by find(1) -- if speed is an issue and the input isn't solely storage API tokens. (It deals fine with unsorted input, but is unlikely to be any faster in that case than a simple "xargs rm" command.) Sorting may even slightly speed up the removal of storage API tokens due to caching effects, since sorting will tend to keep all of the tokens from a particular storage method together. Various additional optimizations for removing files can be turned on and/or tuned with options (see below). Which options will be most effective depends heavily on the underlying structure of the file system, the way in which directories are stored and searched, and similar often underdocumented operating system implementation details. The more sophisticated the underlying operating system and file system, the more likely that it will already perform the equivalent of these optimizations internally. OPTIONS
-d Don't remove any files. Instead, print a list of the files that would be removed to standard output. Each line contains either the current directory of fastrm at the time it would do the unlink and the relative path name it would pass to unlink(2) as two fields sep- arated by whitespace and a "/", the absolute path name (as a single field) that would be passed to unlink(2), or the string "Token" and the storage API token that would be removed. -e Treat an empty input file as an error. This is most useful when fastrm is last in a pipeline after a preceding sort(1) command, ensur- ing that fastrm will fail if the sort fails. -cI Controls when fastrm calls chdir(2). If the number of files to be unlinked from a given directory is at least I, then fastrm will change to that directory before unlinking those files. Otherwise, it will use either the absolute path names or a path name relative to the current directory (whichever is likely more efficient). The I parameter is optional; if just -c is given, -c1 is assumed, which will cause fastrm to always chdir before calling unlink(2). The default is -c3. Use -c0 to prevent fastrm from ever using chdir(2). -sM When -s is given and the number of files to remove in a directory is greater than M, rather than remove files in the order given, fas- trm will open the directory and read it, unlinking files in the order that they appear in the directory. On systems with a per-process directory cache or that use a linear search to find files in a directory, this should make directory lookups faster. The M parameter is optional; if just -s is given, -s5 is assumed. When this option is in effect, fastrm won't attempt to remove files that it doesn't see in the directory, possibly significantly speed- ing it up if most of the files to be removed have already been deleted. However, using this option requires fastrm to do more internal work and it also assumes that the order of directory listings is stable in the presence of calls to unlink(2) between calls to read- dir(3). This may be a dangerous assumption with some sophisticated file systems (and in general this option is only useful with file systems that use unindexed linear searches to find files in directories or when most of the files to be removed have already been deleted). This optimization is off by default. -uN Specifying this option promises that there are no symbolic links in the directory tree from which files are being removed. This allows fastrm to make an additional optimization to its calls to chdir(2), constructing a relative path using "../.." and the like to pass to chdir(2) rather than always using absolute paths. Since this reduces the number of directory lookups needed with deeply nested direc- tory structures (such as that typically created by traditional news spool storage), it can be a significant optimization, but it breaks horribly in the presence of symbolic links to directories. When -u is given, fastrm will use at most N levels of ".." segments to construct paths. N is optional; if just -u is given, -u1 is assumed. This optimization is off by default. fastrm also accepts -a and -r options, which do nothing at all except allow you to say "fastrm -usa", "fastrm -ussr", or "fastrm -user". These happen to often be convenient sets of options to use. EXIT STATUS
fastrm exits with a status of zero if there were no problems, and an exit status of 1 if something went wrong. Attempting to remove a file that does not exist is not considered a problem. EXAMPLES
fastrm is typically invoked by INN via expirerm(8) using a command like: fastrm -e /usr/local/news/spool/articles < expire.list To enable all optimizations and see the affect on the order of removal caused by -s, use: fastrm -d -s -e -u ~news/spool/articles < expire.list If your file system has indexed directory lookups, but you have a deeply nested directory structure, you may want to use a set of flags like: fastrm -e -u3 ~news/spool/articles < expire.list to strongly prefer relative paths but not to use readdir(2) to order the calls to unlink(2). You may want to edit expirerm(8) to change the flags passed to fastrm. NOTES
fastrm defers opening the storage subsystem or attempting to parse any INN configuration files until it encounters a token in the list of files to remove. It's therefore possible to use fastrm outside of INN as a general fast file removal program. HISTORY
fastrm was originally written by kre@munnari.oz.au. This manual page rewritten in POD by Russ Allbery <rra@stanford.edu> for InterNetNews. $Id: fastrm.1,v 1.1.2.1 2001/02/03 07:38:44 rra Exp $ SEE ALSO
expirerm(8) 3rd Berkeley Distribution INN 2.3.2 FASTRM(1)
All times are GMT -4. The time now is 06:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy