Find files whose name is 4 characters long

78,338

Solution 1

Use the ? wildcard for file globbing:

ls -d /tmp/????

This will print all files and directories whose filename is 4-char long.

As suggested by @roaima, the -d flag will prevent ls to display the content of subdirectories that match the pattern.

Solution 2

List files in /tmp only:

cd /tmp
find . ! -name . -prune -path './????' -type f

List files in /tmp recursively:

find /tmp -path '*/????' -type f

Solution 3

Try:

find /tmp -type f -print| awk -F/ ' length($NF)  == 4 '

What awk does:

  • Using / as field separator,
  • Finding filename $NF (last field)
  • Computing length
  • And check if value is 4, then print it.

Solution 4

There's also a perl (5.10 or newer) solution:

perl -E 'say for </tmp/????>;'

A slightly more flexible version where you can specify the desired length:

perl -E 'my $w = "?" x shift; say for </tmp/$w>;' 4

Solution 5

This seems to me like the most straightforward way to find a file of four bytes:

find /tmp -type f -size 4c

Edit: to find a file name of four bytes:

find /tmp -type f -name '????'
Share:
78,338

Related videos on Youtube

Mike
Author by

Mike

Updated on September 18, 2022

Comments

  • Mike
    Mike almost 2 years

    I would like to find files whose name has only 4 characters.

    Example, there are three files under /tmp:

    $ ls /tmp
    txt
    file
    linux
    

    Output should only show file because it only has 4 characters.

  • frostschutz
    frostschutz about 9 years
    +1 for not using find
  • Mike
    Mike about 9 years
    Thanks It works! I have another question how to find the file in sub-directory without /tmp
  • dr_
    dr_ about 9 years
    Please open a separate question for this.
  • cuonglm
    cuonglm about 9 years
    This won't work if filename contains newline.
  • cuonglm
    cuonglm about 9 years
    This won't work if filename contains newline
  • Amine Maalfi
    Amine Maalfi about 9 years
    @cuonglm if your going to be pedantic about newlines, at least apply it evenly across all answers to a question. Any globbing with ?(s) is susceptible to this defect.
  • cuonglm
    cuonglm about 9 years
    I mention this in all answers which won't handle newline in filename. Globbing doesn't have this trouble.
  • Amine Maalfi
    Amine Maalfi about 9 years
    but the answer by dr01 also fails on this as \n is counted as two characters where as it is actually only one.
  • jw013
    jw013 about 9 years
    @joeLovick In *nix platforms a newline is a single linefeed character.
  • cuonglm
    cuonglm about 9 years
    @joeLovick: You created file with literal \n, not newline
  • cuonglm
    cuonglm about 9 years
    Note that this won't list hidden file.
  • roaima
    roaima about 9 years
    Use ls -d to avoid expansion of any directory matching /tmp/????
  • roaima
    roaima about 9 years
    This will also match /tmp/somelongpathname/fred, which doesn't match the glob /tmp/????
  • Ulric Eriksson
    Ulric Eriksson about 9 years
    It is supposed to. /tmp/???? only finds files immediately under /tmp.
  • frostschutz
    frostschutz about 9 years
    never parse ls for anything
  • don_crissti
    don_crissti about 9 years
    ls -q | grep -E '^.{4}$' should take care of those file names with funky chars (though they'll appear as question marks in the output).
  • cuonglm
    cuonglm about 9 years
    @Ooker: Yes, try touch a$'\n'b
  • Toby Speight
    Toby Speight about 9 years
    Can use -maxdepth arg to restrict to immediate entries of /tmp. The original question is unclear on the requirement.
  • Toby Speight
    Toby Speight about 9 years
    To include filenames beginning with a dot, set the dotglob shell option before this (e.g. shopt -s dotglob; to unset it afterwards, use shopt -u dotglob).
  • user913
    user913 about 9 years
    @frostschutz. Why is using [find] sub-optimal? +10 votes suggests there's a good reason
  • frostschutz
    frostschutz about 9 years
    @user913 if simple globbing does the same job, find is overkill. nothing wrong with it otherwise