Find all files with name containing string
Use find
:
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1
if you want it recursive) containing "string" and will print it on the screen.
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
If you want to use grep
(but I think it's not necessary as far as you don't want to check file content) you can use:
ls | grep touch
But, I repeat, find
is a better and cleaner solution for your task.
How to find all files containing specific text (string) on Linux
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Linux find file names with given string recursively
Use the find command,
find . -type f -name "*John*"
How to list filenames containing a specific string
its pretty simple
list files using ls
ls -lrt *ABC*
list directory using ls
ls -ld *ABC*
using ls and grep
ls -l | grep -i "ABC"
using find command
find . -name '*abc*'
using case insensitive search in find
find . -iname '*abc*'
How to find all files containing specific text (string) on Linux
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
R: locating files that their names contain a specific string from a directory and match to my list of wanted files
This works:
library(stringr)
# get this via list.files in your actual code
files <- c("ctrl_S978765_uns_dummy_00_none.txt",
"ctrl_S978765_3S_Cookie_00_none.txt",
"S59607_3S_goody_3M_V10.txt",
"ctrlnuc30-100_S3245678_DMSO_00_none.txt",
"ctrlRAP_S0846567_3S_Dex_none.txt",
"S6498432_2S_Fulra_30mM_V100.txt")
ids <- data.frame(`ID Code` = c("S978765", "S978765", "S306223", "S897458", "S514486"),
CLnumber = c(1, 2, 1, 1, 2),
stringsAsFactors = FALSE)
str_subset(files, paste(ids$ID.Code, collapse = "|"))
#> [1] "ctrl_S978765_uns_dummy_00_none.txt" "ctrl_S978765_3S_Cookie_00_none.txt"
str_subset
takes a character vector and returns elements matching some pattern. In this case, the pattern is "S978765|S978765|S306223|S897458|S514486"
(created by using paste
), which is a regular expression that matches any of the ID codes separated by |
. So we take files
and keep only the elements that have a match in ID Code
.
There are many other ways to do this, which may or may not be more clear. For example, you could pass ids$ID.Code
directly to str_subset
instead of constructing a regular expression via paste
, but that would throw a warning about object lengths every time, which could get confusing (or cause problems if you get used to ignoring it and then ignore it in a different context where it matters). Another method would be to use purrr
and keep
, but while that might be a little bit more clear to write, it would be a lot more inefficient since it would mean making multiple passes over the files vector -- not relevant in this context, but possibly very relevant if you suddenly need to do this for hundreds of thousands of files and IDs.
Find all files whose names contain a given string, and highlight it
For convenience you can create a BASH function for this:
hlt() { find . -iname '*'"$1"'*' | grep --color "$1"; }
and call it as:
hlt foo
Find all files with a filename beginning with a specified string?
Use find
with a wildcard:
find . -name 'mystring*'
Related Topics
Ioctl VS Netlink VS Memmap to Communicate Between Kernel Space and User Space
Sorting on the Last Field of a Line
How to Convert Yyyymmddhhmmss to a Date Readable by 'Date'
Start Script After Another One (Already Running) Finishes
Replace Whitespace with a Comma in a Text File in Linux
How to Calculate CPU Utilization of a Process & All Its Child Processes in Linux
Assembly: Printf Not Printing New Line
Limitations of Intel Assembly Syntax Compared to At&T
Sed with Literal String--Not Input File
Linking 32-Bit Library to 64-Bit Program
Bash Copy All Files Except One
How to Get the Source Code for the Linux Utility Tail
Why Does Printf Overwrite the Ecx Register
Are There Standards for Linux Command Line Switches and Arguments
How to Set a Custom Baud Rate on Linux