Find only text files in directory [duplicate] - linux

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.

Related

Archiving Shell Script [duplicate]

This question already has answers here:
Find and xargs to correctly handle filenames with spaces in their names
(3 answers)
Closed 3 years ago.
I'm very new to shell script and I'm having issues with archiving files.
I have a folder with .xlsm files and I want those files that passed the retention period. I was able to archive except I'm having issues with those files having spaces with their filename eg. X y z.xlsm. below is my sample code.
find ${work_dir} -type f -mtime +${retention} | xargs tar -cvf ${Destination}/archive.tar
Any idea how to achieve it?
Thanks!
Use NUL as delimiter (-print0 for find, and --null for xargs).
find ${work_dir} -type f -mtime +${retention} -print0 | xargs --null tar -cvf ${Destination}/archive.tar

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

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' {} \;

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\//'

Replace all \\ with / in files and subdirs [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 5 years ago.
i need a quick command (linux or windows) to replace every \\ with a /, and all tries with sed failed because of the /.
(I already tried find . -name '*.*' -exec sed -i 's/\\///g' {} \;, but i think it failed with the "/".
find . -name '*.*' -type f -exec sed -i 's:\\\\:/:g' {} \;
You need to escape each backslash, and using a colon or comma as separators is generally recommended when making replacements with forward-slash. However, escaping the forward slash works too:
find . -name '*.*' -type f -exec sed -i 's/\\\\/\//g' {} \;
As pointed out in comments the OS module is probably what you really need to look at.
Edit: thanks to #tripleee for reminding me of the -type f line, which limits it to files, rather than including the current directory.
Also, I copied the syntax *.* from the OP but in general it isn't helpful. * alone is usually what you want, since files aren't guaranteed to have a dot in their name. Assuming you were happy to include files not containing a dot, the simplest thing to do here is have no -name at all:
find . -type f -exec sed -i 's:\\\\:/:g' {} \;

Sed replacement not working when using variables [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 9 years ago.
something strange is happening when trying to replace string with sed. This works :
find /home/loni/config -type f -exec sed -i 's/some_pattern/replacement/g' {} \;
So it works when I manually type the strings. But in the case below replacement doesn't occur :
find /home/loni/config -type f -exec sed -i 's/${PATTERN}/${REPLACEMENT}/g' {} \;
When I echo these two variables PATTERN and REPLACEMENT they have the correct values.
I'm trying to replace all occurences of pattern string with replacement string in all files in my config directory.
Try
find /home/loni/config -type f -exec sed -i "s/${PATTERN}/${REPLACEMENT}/g" {} \;
instead. The ' quotes don't expand variables.
Not sure if I got this right, but if you want to replace the ${PATTERN} with ${REPLACEMENT} literally you have to escape the dollar and maybe the braces, those are reserved characters in regular expressions:
find /home/loni/config -type f -exec sed -i -e 's/\$\{PATTERN\}/\$\{REPLACEMENT\}/g' {} \;

Resources