Find and replace string in a tree. Recursive - linux

I have tried searching for answer here and I found some but they dont seem to work for me. I want to rename a project file and I find it that the esiest way would be to replace its name in every single file and ALSO every file name as well in the directory.
Could someone please point me a way (in windows or linux) to replace recursively a string in a directory in every file and every file name, with another string?
I would appreciate all help.

Bash
In Unix you can do it in bash shell. You have to make use of find, mv and sed and the following three commands as examples:
Replace string "ABC" with "345" in the name of every directory
in the current directory or in the subdirectories recursively.
find . -depth -type d -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the name of every file in
the current directory or in the subdirectories recursively.
find . -depth -type f -name "*ABC*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//ABC/345}"' par0 {} \;
Replace string "ABC" with "345" in the content of every file
in the current directory or in the subdirectories recursively.
find . -depth -type f -name "*" -exec bash -c 'sed -i "s/ABC/345/g" "$1"' par0 {} \;

Related

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

Bash: Find files containing a certain string and copy them into a folder

What I want:
In a bash script: Find all files in current directory that contain a certain string "teststring" and cop them into a subfolder "./testfolder"
Found this to find the filenames which im looking for
find . -type f -print0 | xargs -0 grep -l "teststring"
..and this to copy found files to another folder (here selecting by strings in filename):
find . -type f -iname "stringinfilename" -exec cp {} ./testfolder/ \;
Whats the best way to combine both commands to achieve what I described at the top?
Just let find do both:
find . -name subdir -prune -o -type f -exec \
grep -q teststring "{}" \; -exec cp "{}" subdir \;
Note that things like this are much easier if you don't try to add to the directory you're working in. In other words, write to a sibling dir instead of writing to a subdirectory. If you want to wind up with the data in a subdir, mv it when you're done. That way, you don't have to worry about the prune (ie, you don't have to worry about find descending into the subdir and attempting to duplicate the work).

bash script parameter all file with beginning with a specified string

I am trying to make the code find all directories start with same letters, this is the code so far. I have two directories lit and lite I should be able to see both directories with I search for lit.
for I in $*
do
echo "the directories $(pwd)/"$1" was modified on "$(date -d "$(stat -c '%y' $1)"
'+%d %d %H:%M'$1)
done
The find command can take the type of files your looking for, and also perform a search for a given name.
find . -type d -name "lit*" -exec ls -ld {} \;
Here we set -type d for directories and -name <search>*" for the name of the files to search for.
You can then execute a command for each result using the -exec parameter

Bash - Find several specific folders in a directory and its sbdirectories and rename these specific folders

I just started to study Bash. I want to do a script to find some specific folders in a directory and its subdirectories and if it exist, rename it into the same folder where we have found it. The same specific folder can be in some subdirectories.
I use this:
file=`find . -name a`
if [ -d $file ]
then
rename 's/a/b/' $file
fi
But don't work. Is there anyway to do this process?
Thanks.
Finally, i solved the problem with this:
find . -name "a" -type d -execdir rename 's/a/b/' {} \; &>/dev/null
You can do this with oneliner:
find . -name "a" -type d -execdir rename 's/a/b/' {} \;
The parameter to name might be regex.
With -type d it will find all directories.
-execdir changes to a matching item's directory and then executes the rename command, passing the filename of the item at hand as an argument ({}).

Search for text files in a directory and append a (static) line to each of them

I have a directory with many subdirectories and files with suffixes in those subdirectories (e.g FileA-suffixA FileB-SuffixB FileC-SuffixC FileD-SuffixA, etc).
How can I recursively search for files with a certain suffix, and append a user-defined line of text to those files? I feel like this is a job for grep and sed, but I'm not sure how I would go about doing it. I'm fairly new to scripting, so please bear with me.
You can do it like
find /where/to/search -type f -iname '*.SUFFIX' -exec echo "USER DEFINED STRING" >> \{\} \;
find searches in the suplied path
-type f finds only files
-iname '*.SUFFIX' find the .SUFFIXed names, case ignored
find ./ -name "*suffix" -exec bash -c 'echo "line_to_add" >> $1' -- {} \;
Basically you use find to get a list of the files. Then you use bash to echo append your line to that list.

Resources