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

  • @VP:try (lesbonscomptes.com/recoll) .A good software to search items in a folder including in the folder contents. – justin May 25 '15 at 7:02
  • Why not find allCSV/a_date -type f -name '*.csv'? – cuonglm May 25 '15 at 7:08
  • @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. – VP7 May 25 '15 at 7:46
  • 2
    find .. -path '*/a_*/*' -name '*.csv' – Stéphane Chazelas May 25 '15 at 9:47
  • @StéphaneChazelas Thank you Very much. It worked perfectly. – VP7 May 25 '15 at 10:18

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

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'

You could use Recoll that helps you to search any document type and also restrict your search to a particular folder as shown below.

Simple Search Simple Search

Advanced Search(To restrict search to a particular folder) Restricting Search

  • I would like to use using command line. – VP7 May 25 '15 at 9:26
  • @VP7:If you have installed Recoll you would have noticed that it could be run from the command prompt too. – justin May 25 '15 at 9:32
  • It will be helpful if I can achieve with default vim commands – VP7 May 25 '15 at 9:41
  • @VP7:Do you mean you couldn't install Recoll? – justin May 25 '15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.