Searching for a string in multiple files on Linux [closed] - linux

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
How to search in multiple files or folders for a string inside a plain text file?
For example I need to find the string "foo" in all files in the folder "/home/thisuser/bar/baz/"

You need to have read privileges on the files you will be searching in. If you do have them, then simply use
grep -r "foo" /home/thisuser/bar/baz/*
to search in a certain folder or
grep "foo" /home/thisuser/bar/baz/somefile.txt
if you need to search in a specific file, in this case "somefile.txt".
Basically the syntax is
grep [options] [searched string] [path]
// -r is an option which states that it will use recursive search
Another useful options are "-n" to show on which line in which file the string is located, "-i" to ignore case, "-s" to suppress some messages like "can't read file" or "not found" and "-I" to ignore binary files.
If you use
grep -rnisI "foo" /home/thisuser/bar/baz/*
you will exactly know where to look.

Use
grep -r "foo" /home/thisuser/bar/baz/

Related

How to find out human unreadable file in the directory? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a source code which is having text and binary file. I have to find and collect all the human unreadable files present in source code. How I can do this?
Although the answer of Far Had is correct, you don't even need a for-loop for this. As you state yourself, all your files are within one directory, so you can simply run:
file *
The answers containing "text" (be it ASCII, unicode or something else) indicate human readable files.
This piece of code returns a list of all non ascii text files in current directory.
Hope this will help:
for i in `find . -type f`; do file $i; done |grep -v text | cut -d : -f 1
You could replace the . (dot) after the find with any other location in your filsystem.
One way is to use perl (File::Find module) like this:
perl -MFile::Find -e '#directories=shift || "."; sub wanted { ! -T && print "$File::Find::name\n"; }; find(\&wanted, #directories);'
NOTE: The above command defaults to searching the current directory.
To search a specific directory e.g. /tmp, just type the above command followed by a space and /tmp

What is the purpose of the below command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
What is the purpose of the below command ?
grep -ir nashorn ./ | grep "^[^:]*\.java"
If finds all lines that contain the string nashorn, case-insensitively, in files in the current directory hierarchy whose names contain .java.
The -i option to grep makes it match case-insensitively. The -r option makes it recurse into all directories in the directory arguments and search all the files. So the first part of the pipeline matches nashorn in all files in the current directory, recursively.
The output of that command will be in the format:
filename:matching line
The second grep matches those lines. ^ means the beginning of the lines, [^:]* means a sequence of characters that doesn't include :, which restricts it to the filename part of the line. ANd \.java matches .java literally. So it only matches lines where .java is in the filename part of the line.

Shell Script program to execute all pdf files in one folder to convert them in .xml or .json format and save the output in another folder [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm in of need of help.
How do i write a shell script program using Apache Tika as the converter to help me execute .pdf files in one directory and save them in another directory in a different format (json, xml, etc).
A rough example, using JSON:
outdir=../out.d
mkdir -p "$outdir"
for f in *.pdf; do
java -jar tika-app.jar --json "$f" >"$outdir/${f%.pdf}.json" \
|| rm -- "$outdir/${f%.pdf}.json"
done
Obviously, this expects tika-app.jar to be in the current directory; adjust to taste. Similarly, see http://tika.apache.org/1.6/gettingstarted.html for full command-line usage.
To understand the ${f%.pdf} shell syntax, see BashFAQ #73.

delete files with specific string in the filename [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How is it possible to delete files containing a string as an embedded string except at the
beginning or end by using wild-cards.
I'm an amateur started Ubuntu less than a month.
rm ?*foo?*
removes files containing foo provided that there is at least one character before and after, so "foobar" and "barfoo" will NOT be deleted, whereas "barfoobar" will be.
As a precaution, do
ls ?*foo?*
first to make sure that you aren't deleting the wrong stuff. And be very careful not to accidentally include any spaces as rm ?* foo?* is almost certainly very bad. To provide some protection, wrap the argument in quotes, thus
rm "?*foo?*"
I don't think this is possible with a single expansion pattern. You can use grep for filtering instead:
ls -d '*foo*' | egrep -v '^foo|foo$' | xargs rm
So the ls lists everything containing foo, then egrep removes the files with matches at the beginning/end, and finally xargs runs a command (rm in this case) on each remainder.
The dangerous thing about this technique is that filenames may contain special characters like line breaks or asterisks, so use at your own risk!

rename multiple files shell in Linux [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a number of files such as file_022.bmp, file_023.bmp...file_0680.bmp. I need to rename these to something a little bit more convenient such as file_1.bmp, file_2.bmp...file_658.bmp.
Is there a bash script that I could write to do this for me? Thanks for the help and advice.
Luke H
if you're on a debian based linux system then you can use the rename script which accepts regular expressions to rename files. Some more info because I find it hard to find the man page.
e.g.
harald#Midians_Gate:~$ ls p*.php
parse.php pd.php pgrep.php preg_based.php proc.php
suppose I want to change the extension to .perl and prepend the name with file_
then I use command:
rename -n 's/([a-z]*)\.php/file_$1.perl/' p*.php
would give
parse.php renamed as file_parse.perl
pd.php renamed as file_pd.perl
pgrep.php renamed as file_pgrep.perl
preg_based.php renamed as preg_file_based.perl
proc.php renamed as file_proc.perl
I select and capture the base filename ([a-z]*) and then use it in the substitution $1 and append .perl and prepend $1 with the regular string file_
the -n option makes it test run without changing anything
As you can see from this example your selecting regexp needs to be correctly thought out or you get cases like the above preg_based.php where you wanted file_preg_based.perl :)
to compensate for that I would've needed to use ([a-z_]*) here
It's one of the many reasons why I keep hanging on to debian, I'd love to find the equivalent for other non-debian systems though :-/
if you have files a.bmp,b.bmp,c.bmp
and you want to end up with file_1.bmp, file_2.bmp, file_3.bmp
using bash:
mkdir result
index=1
for i in *.bmp
do
mv "$i" "result/file_"$((index++)).bmp
done
notes:
using a subdirectory is advised to avoid accidentally overwriting a file that looks like file_xx.bmp
if you have too many files to fit in the command line after expansion you could use something like:
mkdir result
index=1
find . -name "*.bmp" | while read i
do
echo mv "$i" "result/file_"$((index++)).bmp
done
after inspecting the output remove the 'echo'

Resources