Error: grep: find: Is a directory - linux

I am new to Linux and going through some tutorials and samples. I created a file called test and put alex and alexander in it. I'm trying to find instances of just alex.
If I do grep alex * I get the error:
grep: find: Is a directory.
If I do cat test | grep alex then I get (as expected)
alex
alexander (with alex in red)
Why does the first cause an error, and the second produce expected results?

If you want to grep phrase from specific file use:
# grep "alex" test
In case you use grep alex * it will search through all files inside the current work directory. In case subdirectory will be met it will tell you something like grep: find: Is a directory
If you want to perform a recursive search use -r key. For example
# grep -r "alex" /some/folder/
In this case all files and files inside subdirectories from /some/folder/ will be checked.
And you can always use man grep.

The correct answer would be:
grep -d skip alex /etc/*

Setting the environmental variable GREP_OPTIONS to include the value "--directories=skip" should suppress that “Is a directory” message (i.e. enter GREP_OPTIONS="--directories=skip" or add that line to one of your login initialization files like .bashrc to make that behavior permanent).
> cat test
alex
alexander
> grep alex *
grep: mysql_data: Is a directory
grep: sql_updates: Is a directory
test:alex
test:alexander
grep: tmp_files: Is a directory
> GREP_OPTIONS="--directories=skip"
> grep alex *
test:alex
test:alexander
Also since there is a command named “test” and another named “find” it’s usually not advisable to use those as file or directory names. That shouldn’t be a problem here but it can lead to other conflicts.

If you wanted to go through all the directories including the find directory, you can run this command:
grep alex * -r
Which will go through all the directories in your current directory recursively.

Or using grep with -l flag or -w flag
$ grep -l alex /etc/*
To return list of files containing word "alex" in the /etc directory
$ grep -w alex /etc/*
To return list of lines from files in the /etc directory containing "alex" word

Related

List all folder and subfolder inside it where folder names start with a* or b* or c* with path

I need a folder and subfolder inside it to be displayed where names that start with A* or B* or C* and display along with path
Below Command Does not Display as expected
$ ls -l | egrep d
You can display the current directory by using the system environment variable PWD. You can combine the PWD with your ls command
using ls -ld
ls -ld $PWD/A* $PWD/B* $PWD/C*
EDIT
If you want a list of all the directories and sub directories you can use the find command.
find . > subfolders.txt && cat subfolders.txt | egrep -i "^./E|^./g"
This command will recursively list all contents on your current working directory and send the output to a txt file named subfolders.txt. Then it will read the contents of subfolders.txt and using egrep, you can filter out anything that starts with "./E" or "./g". the -i option means it is case insensitive.
NOTE: This will also display the files contained in those subfolders.
find . | grep -E '/A|/B|/C'
find is better than ls for your requirements.

How to find a file containing specific text in Centos7?

I have a server which runs on centos 7. I need to find a file containing 0774386850 so that I can replace with another string. Kindly give me a Linux command to give me that file
By using grep command, you can achieve what you expected
grep -rlnw '/path/to/somewhere/' -e '0774386850'
-r or -R is recursive,
-n is line number, and
-w stands for match the whole word.
-l (lower-case L) file name of matching files.
grep -r "0774386850 " /path/to/dir
You can use either grep/fgrep or find to achieve your goal.

Finding multiple strings in directory using linux commends

If I have two strings, for example "class" and "btn", what is the linux command that would allow me to search for these two strings in the entire directory.
To be more specific, lets say I have directory that contains few folders with bunch of .php files. My goal is to be able to search throughout those .php files so that it prints out only files that contain "class" and "btn" in one line. Hopefully this clarifies things better.
Thanks,
I normally use the following to search for strings inside my source codes. It searches for string and shows the exact line number where that text appears. Very helpful for searching string in source code files. You can always pipes the output to another grep and filter outputs.
grep -rn "text_to_search" directory_name/
example:
$ grep -rn "angular" menuapp
$ grep -rn "angular" menuapp | grep some_other_string
output would be:
menuapp/public/javascripts/angular.min.js:251://# sourceMappingURL=angular.min.js.map
menuapp/public/javascripts/app.js:1:var app = angular.module("menuApp", []);
grep -r /path/to/directory 'class|btn'
grep is used to search a string in a file. With the -r flag, it searches recursively all files in a directory.
Or, alternatively using the find command to "identify" the files to be searched instead of using grep in recursive mode:
find /path/to/your/directory -type f -exec grep "text_to_search" {} \+;

How to append list of home directory to a text file and how to move files?

I know a very basic of unix and I am writing a java program which I only need couple of commands to solve my problem.
I appreciate if anyone knows how I can get desired output:
What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
I really appreciate if any one can help me out with these.
What command I can use to append a full list of objects of my home directory to a file "report.txt". Assume I am in different directory?
ls -la ~ > report.txt
What command I can use to move all files in my directory that begin with either a, b, or c, to a subdirectory, sorting, of my current directory?
mv a* b* c* yourdirectory
If I go to /proc directory. What does this command do?
cd 'ps | grep ps| cut -f 1'
This command will give "bash: cd: ps | grep ps| cut -f 1: No such file or directory" error

grep command on linux ( especially grep --exclude)

I'm newbie on linux issues and before asking here I googled it however I couldnt find a clue about grep --exclude.So I wonder what does the line below ?? Thanks in advance
grep --exclude=\*.svn\* -r REVISION 1.0.0.8/*1.0.0.8.config > 1.0.0.8-REVISION.txt
SOLVED BY MYSELF
search REVISION on "1.0.0.8/*1.0.0.8.config" file exluding .svn file and send output to 1.0.0.8-Revision.txt
To grep through a directory while excluding various file types (in your example anything prefixed with .svn ) you probably want to do the following:
grep -r "SOMETHING" --exclude="*.svn*" /path/to/directory > 1.0.0.8-REVISION.txt 2>&1
This will grep through all files within a directory recursively, then post the matches to a file called 1.0.0.8-REVISION.txt which is located relative to where you are currently searching.
If you provide more information abut exactly what you're trying to grep/find I can update my answer with something that will work exactly for your case.
you can find grep on man pages here.
what exclude does:
--exclude=PATTERN:
Recurse in directories skip file matching PATTERN.
your command will search, recursively, in all directories, skipping file pattern "*.svn*" and searching for file pattern "1.0.0.8/*1.0.0.8.config > 1.0.0.8-REVISION.txt"

Resources