Error: "grep: Argument list too long" [duplicate] - linux

This question already has answers here:
How can I grep while avoiding 'Too many arguments' [duplicate]
(5 answers)
Closed 7 years ago.
I am trying to run the following command, but gets argument too long error. Can you help?.
HOST# grep -rl 'pattern' /home/*/public_html/*
-bash: /bin/grep: Argument list too long
Is there a way to override this error and grep the pattern matching files I want in all users public_html directory. There are around 500+ users in the same server.

Use find
find /home/*/public_html -type f -exec grep -l 'pattern' {} +
The + modifier makes it group the filenames in manageable chunks.
However, you can do it with grep -r. The arguments to this should be the directory names, not filenames.
grep -rl 'pattern' /home/*/public_html
This will just have 500+ arguments, not thousands of filenames.

Related

How to get deleted files into a log file. Bash script [duplicate]

This question already has answers here:
Linux find and delete files but redirect file names to be deleted
(5 answers)
Closed 1 year ago.
So im using the script
find /path/to/files/* -mtime +60 -exec rm {} \;
How can i collect the deleted files and transfer them into a logfile in Bash script
You could do something like:
find /path/... -print ... | tee -a <log.file>
The -print will print out all the hits, and the tee will append that to some log.file.
Side note: the * at the end of your /path/to/files/* seems superfluous.
Side note2: if you just want to delete the files, find has a built-in -delete.

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

Grep for a file with a specific name [duplicate]

This question already has answers here:
Exclude a string from wildcard search in a shell
(3 answers)
Closed 5 years ago.
In my repository I have several files, including two specific JAR files named as follows:
backend-0.0.1-SNAPSHOT.jar
backend-0.0.1-SNAPSHOT.jar.original
I need to get only the first one, and I need to fetch it only with its name: "backend". The version is not static; it can change.
So I have done this:
ls | grep 'backend'
But this one get me both of them, so I need to grep for files beginning with backend and ending by .jar.
How can I use this?
Don't use the output of ls for scripting. Use find instead:
find . -maxdepth 1 -type f -name 'backend*.jar'
Or, without using grep:
ls backend*.jar
ls | grep '^backend.*.jar$'
$ means, that there are no symbols after r in jar.

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.

How to grep a string in a directory and all its subdirectories? [duplicate]

This question already has answers here:
How do I recursively grep all directories and subdirectories?
(26 answers)
Closed 9 years ago.
How to grep a string or a text in a directory and all its subdirectories'files in LINUX ??
If your grep supports -R, do:
grep -R 'string' dir/
If not, then use find:
find dir/ -type f -exec grep -H 'string' {} +
grep -r -e string directory
-r is for recursive; -e is optional but its argument specifies the regex to search for. Interestingly, POSIX grep is not required to support -r (or -R), but I'm practically certain that System V grep did, so in practice they (almost) all do. Some versions of grep support -R as well as (or conceivably instead of) -r; AFAICT, it means the same thing.

Resources