By RikSaunderson


2010-10-27 15:26:44 8 Comments

I have a directory with roughly 100000 files in it, and I want to perform some function on all files beginning with a specified string, which may match tens of thousands of files.

I have tried

ls mystring*

but this returns with the bash error 'Too many arguments'. My next plan was to use

find ./mystring* -type f

but this has the same issue.

The code needs to look something like

for FILE in `find ./mystring* -type f`
do
    #Some function on the file
done

3 comments

@jacanterbury 2012-07-13 12:15:44

ls | grep "^abc"  

will give you all files beginning (which is what the OP specifically required) with the substringabc.
It operates only on the current directory whereas find operates recursively into sub folders.

To use find for only files starting with your string try

find . -name 'abc'*

@matson kepson 2018-08-08 09:30:28

If you want to restrict your search only to files you should consider to use -type f in your search

try to use also -iname for case-insensitive search

Example:

find /path -iname 'yourstring*' -type f

You could also perform some operations on results without pipe sign or xargs

Example:

Search for files and show their size in MB

find /path -iname 'yourstring*' -type f -exec du -sm {} \;

@Sergio Tulentsev 2010-10-27 15:27:50

use

find . -name 'mystring*'

@Eldar 2010-10-27 15:31:34

and to execute some function "find . -name 'mystring*' -exec [command]" for example delete "find . -name 'mystring*' -exec rm {} \;"

@Sergio Tulentsev 2010-10-27 15:34:53

Where {} is a placeholder for file name :-) (so you can construct your own actions)

@Brian Z 2014-10-17 06:56:32

This seems to return all results containing the string? The

@Frank Nocke 2017-03-12 12:52:12

...or find . -iname 'mystring*' for case insensitive search.

@jacanterbury 2018-03-14 11:37:16

OP refers to a single directory so insert -maxdepth 1 before the -name to limit find to the current folder

@Sergio Tulentsev 2018-03-14 11:40:22

@jacanterbury: it is not clear if this directory contains other directories (besides the 100k files). It it doesn't, this is not needed. But thanks, might be useful to someone.

Related Questions

Sponsored Content

49 Answered Questions

[SOLVED] How do I find all files containing specific text on Linux?

35 Answered Questions

[SOLVED] How to do a recursive find/replace of a string with awk or sed?

  • 2009-10-17 21:10:42
  • Tedd
  • 528279 View
  • 687 Score
  • 35 Answer
  • Tags:   bash sed awk replace

17 Answered Questions

[SOLVED] How can I exclude all "permission denied" messages from "find"?

15 Answered Questions

[SOLVED] How do I find files that do not contain a given string pattern?

13 Answered Questions

[SOLVED] In Bash, how can I check if a string begins with some value?

19 Answered Questions

[SOLVED] How do I remove all .pyc files from a project?

  • 2009-04-24 11:51:39
  • Teifion
  • 303223 View
  • 681 Score
  • 19 Answer
  • Tags:   bash

5 Answered Questions

2 Answered Questions

[SOLVED] find files matching either some string or some other string in bash

  • 2010-04-23 13:57:21
  • Tim
  • 743 View
  • 1 Score
  • 2 Answer
  • Tags:   bash find

Sponsored Content