Search recursively all files with a given name replacing a word [duplicate] - linux

This question already has answers here:
BASH: recursive program to replace text in a tree of files
(3 answers)
Closed 5 years ago.
From a given folder, I want to search recursively accross all subfolders searching for files with name file.txt replacing all occurences of Foo -case sensitive-
with Bar.
Which is the simplest way of achieving this with basic scripting (Bash / sed / grep / find...).

find + sed solution:
find . -type f -name "file.txt" -exec sed -i 's/Foo/Bar/g' {} \;

Related

shell command to extract the part of filename having characters? [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
In Bash, how to strip out all numbers in the file names in a directory while leaving the file extension intact
(1 answer)
Closed 5 years ago.
I have a file named(multi_extension123.txt). Before copying this file into the directory I need to remove files like
[multi_extension1234.txt
multi_extension1234.txt
multi_extension12345.txt] if present in the directory and then copy the earlier one to this directory. Can anyone give the solution with shellscript.
note: i need to remove only numerical numbers and extension alone.
I have tried this
$ filename= $file1
$ echo "${filename%[0-9].*}"
find . -type f maxdepth 0 mindepth 0 -name "'$filename'[0-9]*.txt" -exec rm -f {} \;

Using sed editor to replace a string containing backslash [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 5 years ago.
How can i use the sed to replace string containing backslash "\" is all files of specific directory.
I tried this but wont work for me
find /home/tds/nfb -type f -exec sed -i 's//var/www/tds///home/tds//' {} \;
I want to replace "/var/www/tds/" with "/home/tds/"
You can do
find /home/tds/nfb -type f -exec sed -i 's|/var/www/tds/|/home/tds/|' {} \;
where the delimiter / is replaced by |. (sed can use almost any character as a delimiter -- it picks whatever character follows the s). Alternatively, you could escape all your backslashes as follows and still used \:
sed -i 's/\/var\/www\/tds\//\/home\/tds\//'

Find only text files in directory [duplicate]

This question already has answers here:
Linux command: How to 'find' only text files?
(16 answers)
Closed 6 years ago.
I tried using this:
$ find . -type f -exec file {} \;
./alma: ASCII text
./jaj.C: C source, ASCII text
./repa: ASCII text, with escape sequences
./mas.cpp: C++ source, ASCII text
./capa: ASCII text
./valami: ASCII text
But if it's a cpp file for example it still writes text so I can't use grep to exclude binary files....what should I do?
Here's the fast method to do it:
find . -type f -exec grep -Iq . {} \; -print
-I in grep will ignore binary files, text files will match right away because of . (any character match), grep will give success for matched file, so -print from find will print the filename.

How to copy the results of a find command in linux [duplicate]

This question already has answers here:
Moving multiple files having spaces in name (Linux)
(3 answers)
Closed 7 years ago.
I have a find command which returns two files. Is there anyway of then copying those files to another directory? My find command is below if that will help.
find "$TEST" -iname "DTWD_????.JPG" -printf "%f\n"
New to linux, thanks.
You just need to use the -exec argument
find "$TEST" -iname "DTWD_????.JPG" -printf "%f\n" -exec mv -t '/home' '{}' +

Get latest file creation time in Unix [duplicate]

This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 7 years ago.
I've two files FileA and FileB. Can someone please let me know how to get time for latest created file in a folder in Unix?
Both for only two files and the general case of n files, you can use find:
find -type f -printf '%T# \n' | sort -n | tail -1
If the files need to match a pattern, you can use something like:
find -type f -name 'example*.txt' -printf '%T# \n' | sort -n | tail -1
This prints all modification times of files in the working directory, sorts them, then selects the last (largest) one.

Resources