Find a file with the extension .csv in a directory matching a pattern
Solution 1
The pattern [a_]*
matches names that start with either of the characters a
or _
. The pattern *[.csv]
matches names that end with one of the characters .
, c
, s
or v
. To match names that start with a_
, use -name 'a_*'
. To match names that end with .csv
, use -name '*.csv'
.
find ../ -name 'a_*' -a -name '*.csv'
or equivalently find ../ -name 'a_*.csv'
matches files whose name starts with a_
and ends with .csv
. This does not filter on the directories traversed to reach the file.
If the files are in subdirectories of the parent directory (e.g. ../a_foo/wibble.csv
), you don't need find
: the find
command is only useful to search directory trees recursively. You can use echo
or ls
:
ls ../a_*/*.csv
If the files can be in subdirectories below the a_*
directories (e.g. ../a_foo/wibble.csv
or ../a_foo/bar/wibble.csv
but not ../qux/a_foo/wibble.csv
), then call find
and tell it to search the a_*
directories.
find ../a_* -name '*.csv'
Alternatively, instead of using find
, you can use the **
wildcard to search in subdirectories recursively. In ksh93, you need to enable this pattern with set -o globstar
first. In bash, you need to enable this pattern with shopt -s globstar
first. In zsh, this pattern is enabled by default. Other shells such as plain sh don't have **
.
ls ../a_*/**/*.csv
If the a_*
directories can themselves be at any depth below the parent directory, you can either use find -path
or **
:
find .. -path '*/a_*/*.csv'
ls ../**/a_*/**/*.csv
Solution 2
There are many ways of doing this. If you just want to list the files, you can use ls
:
ls a_date\(s\)/*csv
Or, with find
:
find .. -path '*a_date*/*csv'
find ../allCSVs/a_date\(s\)/ -name '*csv'
Related videos on Youtube
VP7
Updated on September 18, 2022Comments
-
VP7 almost 2 years
I have a folder in which I have many subfolders.
The Root folder name is allCSV and sub foldername is will be like
a_date(s), b_date(s), c_date(s) ...
I want a file which is in a_date(s) and ends with
.csv
.I tried with:
find ../ -name '[a_]*' -a -name '*[.csv]'
But it is showing all the files ending with
.csv
-
cuonglm about 9 yearsWhy not
find allCSV/a_date -type f -name '*.csv'
? -
VP7 about 9 years@cuonglm I dont want to search in a particular date. the folder a_date, a_date2 may have the CSV file, while a_date3 may not have.
-
Stéphane Chazelas about 9 years
find .. -path '*/a_*/*' -name '*.csv'
-
VP7 about 9 years@StéphaneChazelas Thank you Very much. It worked perfectly.
-
-
VP7 about 9 yearsI would like to use using command line.
-
justin about 9 years@VP7:If you have installed Recoll you would have noticed that it could be run from the command prompt too.
-
VP7 about 9 yearsIt will be helpful if I can achieve with default vim commands
-
justin about 9 years@VP7:Do you mean you couldn't install Recoll?