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 '????'
Related videos on Youtube
Author by
Mike
Updated on September 18, 2022Comments
-
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 about 9 years+1 for not using
find
-
Mike about 9 yearsThanks It works! I have another question how to find the file in sub-directory without /tmp
-
dr_ about 9 yearsPlease open a separate question for this.
-
cuonglm about 9 yearsThis won't work if filename contains newline.
-
cuonglm about 9 yearsThis won't work if filename contains newline
-
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 about 9 yearsI mention this in all answers which won't handle newline in filename. Globbing doesn't have this trouble.
-
Amine Maalfi about 9 yearsbut the answer by dr01 also fails on this as \n is counted as two characters where as it is actually only one.
-
jw013 about 9 years@joeLovick In *nix platforms a newline is a single linefeed character.
-
cuonglm about 9 years@joeLovick: You created file with literal
\n
, not newline -
cuonglm about 9 yearsNote that this won't list hidden file.
-
roaima about 9 yearsUse
ls -d
to avoid expansion of any directory matching/tmp/????
-
roaima about 9 yearsThis will also match
/tmp/somelongpathname/fred
, which doesn't match the glob/tmp/????
-
Ulric Eriksson about 9 yearsIt is supposed to. /tmp/???? only finds files immediately under /tmp.
-
frostschutz about 9 yearsnever parse
ls
for anything -
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 about 9 years@Ooker: Yes, try
touch a$'\n'b
-
Toby Speight about 9 yearsCan use
-maxdepth
arg to restrict to immediate entries of/tmp
. The original question is unclear on the requirement. -
Toby Speight about 9 yearsTo include filenames beginning with a dot, set the
dotglob
shell option before this (e.g.shopt -s dotglob
; to unset it afterwards, useshopt -u dotglob
). -
user913 about 9 years@frostschutz. Why is using [find] sub-optimal? +10 votes suggests there's a good reason
-
frostschutz about 9 years@user913 if simple globbing does the same job, find is overkill. nothing wrong with it otherwise