How can I search for specific file contents in all files in both current folder and all subfolders [duplicate] - linux

This question already has answers here:
How do I recursively grep all directories and subdirectories?
(26 answers)
Closed 4 years ago.
Using Mac terminal server linux/bash commands, how can I search for a particular text string in all *.txt files in the current folder plus all files in the subfolders inside the current folder?

grep -i "xxx" */*
xxx is the target text I am trying to find.

find . -type f -print | egrep ".txt$" | xargs grep "SearchPatern"
Explained as
Find all the file names in the current directory and below send to ....
grep which picks out the file name that end in .txt and send names to ....
xargs which will execute a grep command on each file to look for SearchPatern.

Related

How do I get all files with .md extension, in all subdirectories, that contain a phrase? [duplicate]

This question already has answers here:
Linux search text string from .bz2 files recursively in subdirectories
(4 answers)
Closed 6 months ago.
I have a parent folder named 'dev', and inside it are all my project folders. The ReadMe files of these projects contain the app type "type: game", for example. What I would like to do is to:
search through all subdirectories of the dev folder to find all the files with *.md" extension
then return the names of those directories which contain a .md files with containing the phrase "game"
I've tried piping find into grep like so:
find -type f -name "*.md" | grep -ril "type: game"
But it just returns the names of files from all subdirectories which contain the phrase "game" in any file.
find . -type f -name "*.md" -print0 | xargs -0 grep -il "type: game" | sed -e 's/[^\/]*$//'
This finds any files in the current directory and sub-directories with names ending with .md, then greps for files containing the string. We then use sed to trim the filename leaving only the directories containing a file ending in .md with the "type: game" inside.

How do I concatenate file contents into a single file in bash? [duplicate]

This question already has answers here:
Concatenating multiple text files into a single file in Bash
(12 answers)
Closed 4 years ago.
I have files in different directories under a parent directory, something like this:
Parent Dir
Dir 1 - File 1
Dir 2 - File 2
I want to have an output file that appends the content of File1 with File2. How do I do it in Bash?
Converting my comment to an answer. You can use following find + xargs pipeline command:
cd /parent/dir
find home -name "*.orc" -print0 | xargs -0 cat > test.orc

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

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.

how to list full paths of folders inside a directory in linux? [duplicate]

This question already has answers here:
Show full path when using options
(8 answers)
Closed 8 years ago.
I have a folder /home/Documents/myFolder and inside this folder there are lots other folders. I want to have a file list.txt which contains all the paths of the folders. I want the text file content like this:
/home/Documents/myFolder/1234
/home/Documents/myFolder/asd2
/home/Documents/myFolder/asdawgf
/home/Documents/myFolder/dawt
.
.
.
I tried this one but it was not what I want ls > /home/Documents/myFolde/list.txt
it just prints the folder names. I want the full paths.
Use find listing all directories (-type d) and then sed the output to get the full path correct:
find . -type d | sed -n 's:^\./:/home/Documents/myFolder/:'p > /home/Documents/myFolder/list.txt
you can use find:
find . > /home/Documents/myFolde/list.txt

Resources