Linux: Find File Names With 4 or 5 Characters

How to find files on Linux with a name shorter than n characters?

The -name option of find operates on the file name without the path. You can't say "shorter than" in a pattern, but you can say "longer than" and negate:

find . -not -name '?????*'

List all files with length of name between 3 and 6 characters

shopt -s extglob
ls ??@(?|??|???|????).* 2>/dev/null

Linux - Find files that do not contain certain characters

You don't indicate which OS your question applies to, but one way to determine the set of matching files on Mac OS X or Linux would be:

find . -maxdepth 1 -type f -name "?????*" | egrep -v "./abc"

Note that this will list only files in the current directory. If you want to include files in subdirectories, you'll need to remove the maxdepth argument.

Also note that these commands are case-sensitive. You'll need to use -iname and -i to make them case-insensitive.

EDIT:

If you really need to use the echo command, the following will work:

echo `find . -maxdepth 1 -type f -name "?????*" | egrep -v "./abc"`

Find files containing either character x or y?

You can use glob pattern in find command like this:

find . -name '*[][*?]*'

Above will find files that have any of these characters present in their names:

  1. ]
  2. [
  3. *
  4. ?

    • It is important to quote the pattern provided in -name option so that shell doesn't attempt to expand it.
    • Also important to keep ] before [ inside [...]

use find to match filenames starting with number

Either this:

$ find -E .  -type f -regex '.*/[[:digit:]]+$'

Or this:

$ find .  -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'

work to find files with names composed entirely of digits (i.e., no extension)

List files that only have number in names

find's -regex matches against the whole path, not just the filename (I myself seem to forget this once for every time I use it).

Thus, you can use:

find . -regex '.*/[0-9]+\.html'

(^ and $ aren't necessary since it always tests against the whole path.)

Using find also has advantages when you want to do something with the files, e.g. using the built-in -exec, -print0 and pipe to xargs -0 or even (using Bash):

while IFS='' read -r -d '' file
do
# ...
done < <(find . -regex '.*/[0-9]+\.html' -print0)

echo with a glob, ls|grep, etc. tend to stop working when filenames contain spaces (or even newlines) (which I realise won't happen in this case; it's more of a matter of future-proofing and making good habits).

find files that have number in file name greater than

You may use seq for that, but it works only if all files have same naming convention:

seq -f "%02g-a.txt" 6 10
06-a.txt
07-a.txt
08-a.txt
09-a.txt
10-a.txt

I.e.:

cat `seq -f "%02g-a.txt" 6 10` > bigfile.txt


Related Topics



Leave a reply



Submit