I have a directory /folder1/folder2 containing two type of files:
file.txt
file.txt0* (* means any number)
I wrote a script to list all files matching pattern "file.txt0*" occurrencies in folder "/folder1/folder2":
find -wholename /folder1/folder2/file.txt0*
But it always returns nothing.
Any suggestion?
-name searches for the filename and not for the path. You would need to write the search like this:
find /folder1/folder2/ -name file.txt0*
make sure you are in proper relative directory. below should work, if you in root folder and folder1/folder2 are present in / (root)
find /folder1/folder2 -iname file.txt0*
-i does a case-insensitive search.
Related
I would want to find all files in a given directory that contain the entire content of a given file (not necessarily a text file).
I tried to achieve this goal through the use of find and grep but wasn't successful due to the search across several lines.
"SearchBin" is a simple python script that can check if one file exists inside some others: https://github.com/Sepero/SearchBin
find . -type f -print0 | xargs -0 searchbin.py -f needle
where needle is the file whose contents are to be found.
Terminal noob need a little help :)
I have a 98 row long filename list in a .csv file. For example:
name01; name03, etc.
I have an external hard drive with a lot of files in chaotic file
structure. BUT the file names are consistent, something like:
name01_xy; name01_zq; name02_xyz etc.
I would like to copy every file and directory from the external hard
drive which begins with the filename stored in the .csv file to my
computer.
So basically it's a search and copy based on a text file from an eHDD to my computer. I guess the easiest way to do is a Terminal command. Do you have any advice? Thanks in advance!
The task can be split into three: read search criteria from file; find files by criteria; copy found files. We discuss each one separately and combine them in a one-liner step-by-step:
Read search criteria from .csv file
Since your .csv file is pretty much just a text file with one criterion per line, it's pretty easy: just cat the file.
$ cat file.csv
bea001f001
bea003n001
bea007f005
bea008f006
bea009n003
Find files
We will use find. Example: you have a directory /Users/me/where/to/search and want to find all files in there whose names start with bea001f001:
$ find /Users/me/where/to/search -type f -name "bea001f001*"
If you want to find all files that end with bea001f001, move the star wildcard (zero-or-more) to the beginning of the search criterion:
$ find /Users/me/where/to/search -type f -name "*bea001f001"
Now you can already guess what the search criterion for all files containing the name bea001f001 would look like: "*bea001f001*".
We use -type f to tell find that we are interested only in finding files and not directories.
Combine reading and finding
We use xargs for passing the file contents to find a -name argument:
$ cat file.csv | xargs -I [] find /Users/me/where/to/search -type f -name "[]*"
/Users/me/where/to/search/bea001f001_xy
/Users/me/where/to/search/bea001f001_xyz
/Users/me/where/to/search/bea009n003_zq
Copy files
We use cp. It is pretty straightforward: cp file target will copy file to directory target (if it is a directory, or replace file named target).
Complete one-liner
We pass results from find to cp not by piping, but by using the -exec argument passed to find:
$ cat file.csv | xargs -I [] find /Users/me/where/to/search -type f -name "[]*" -exec cp {} /Users/me/where/to/copy \;
Sorry this is my first post here. In response to the comments above, only the last file is selected likely because the others have a carriage return \r. If you first append the directory to each filename in the csv, you can perform the move with the following command, which strips the \r.
cp `tr -d '\r' < file.csv` /your/target/directory
I know what's the directory which contains a file which name I don't know, but I know its name's ending (for example, .txt), also I know there's exactly one file with such ending in the directory.
I've tried the following code:file=grep -w $1/*.txt when $1 is the directory address which didn't help at all, neither did file=$1/*.txt.
Is there any way to achieve what I'm looking for?
If you know the precise directory and a wildcard which will not match any other files, you could run a loop which loops only once.
for file in "$1"/*.txt; do
: "$file" refers to your file here
done
Of course, in a lot of situations, you don't really need to know the precise file name. If you want to run grep on the file which matches your wildcard, just do that:
grep "xyzzy" "$1"/*.txt
What you're looking for is the find command.
you can use it like this (make sure you're in the directory you would like to search):
find . -iname '*.txt'
use the command man find to learn more about the command.
/usr/bin/find directory_name -iname "*.txt"
if you want to operate on those searched file you can even use with find -exec
I am trying to perform a grep search for a term across CSS files only in a huge directory of multiple websites.
This is what I am doing:
grep -irs --include=\*.css "search_term" *
Which is kind of what is specified in the man pages for grep. the \*.css to me indicates that the search be limited only to files with the .CSS extension.
As a test, I created a file called random.xxx in a test directory which has other CSS files that contain the search term. But performing the above command actually yields random.xxx as the result of the grep search instead of the CSS files.
What am I doing wrong here?
I always use find to grep things recursively:
ex. find /etc/ -type f -name '*.conf' -exec grep -si 'help' {} +
In you case replace /etc/ by whatever path you wish, '*.conf' by '*.css' and 'help' by you search pattern.
It always works for me, hope it helps!
I think you need to quote the include pattern !
Like so:
grep --include="\*js" foo *
Thanks for your question, I myself needed to escape the *.
I am trying to list all file names that contain a certain string but do not contain another string. For this particular case, I want all filenames containing "*.java" but not "*Test.java". To find and save the first set, I was using:
find -name "*.java" > sources.txt
But I don't know how to exclude the files that contain "*Test.java" in the file name.
I'm new to bash, so sorry if I've missed something obvious.
You can use:
find -name "*.java" ! -name "Test.java"