Discussion:
The best way to get a file in a directory
(too old to reply)
Peng Yu
2018-11-22 06:15:52 UTC
Permalink
Hi,

I'd like to get a file in a directory. It can be any file in the
directory. I don't care what it is as long as it is in the directory.

I don't want to use * to get all the files and then pick one from them
as this can be slow when there are many files in a directory. Is there
an efficient way in bash to get a file in a directory? Thanks.
--
Regards,
Peng
Eduardo Bustamante
2018-11-22 06:43:17 UTC
Permalink
Post by Peng Yu
Hi,
I'd like to get a file in a directory. It can be any file in the
directory. I don't care what it is as long as it is in the directory.
What do you mean by "get"? You want to read the contents? How do you know
which one to pick? Do you know the full file name? A prefix? Or the first
by modification time? A pattern?
Peng Yu
2018-11-22 11:48:22 UTC
Permalink
Post by Eduardo Bustamante
What do you mean by "get"? You want to read the contents? How do you know
which one to pick? Do you know the full file name? A prefix? Or the first
by modification time? A pattern?
I don’t know the full filename. Any file with name not starting with a
dot is fine. I just need to get the file name. No content is needed. I
don’t care which one to pick. Any file is fine.
--
Regards,
Peng
Dennis Williamson
2018-11-22 14:16:51 UTC
Permalink
Post by Peng Yu
Post by Eduardo Bustamante
What do you mean by "get"? You want to read the contents? How do you know
which one to pick? Do you know the full file name? A prefix? Or the first
by modification time? A pattern?
I don’t know the full filename. Any file with name not starting with a
dot is fine. I just need to get the file name. No content is needed. I
don’t care which one to pick. Any file is fine.
--
Regards,
Peng
https://unix.stackexchange.com/a/62883
Peng Yu
2018-11-22 22:31:42 UTC
Permalink
Post by Dennis Williamson
https://unix.stackexchange.com/a/62883
This involves find. Anything native to bash that can return an arbitrary
file?
Post by Dennis Williamson
<https://unix.stackexchange.com/a/62883>
<https://unix.stackexchange.com/a/62883>
--
Regards,
Peng
Dennis Williamson
2018-11-22 23:20:03 UTC
Permalink
Post by Peng Yu
Post by Dennis Williamson
https://unix.stackexchange.com/a/62883
This involves find. Anything native to bash that can return an arbitrary
file?
Post by Dennis Williamson
<https://unix.stackexchange.com/a/62883>
<https://unix.stackexchange.com/a/62883>
--
Regards,
Peng
# find the first file in a directory
shopt -s nullglob
for file in dir/*
do
if [[ -f $file ]] # skip subdirs
then
break
fi
done

if [[ -n $file ]]
then
echo "Found $file"
fi

If you tell us what you're really trying to do we may be able to offer
better suggestions.
Greg Wooledge
2018-11-23 12:44:53 UTC
Permalink
Post by Dennis Williamson
If you tell us what you're really trying to do we may be able to offer
better suggestions.
It's Peng Yu. I've been telling this person to tell us the goal for
years, and it hasn't worked yet. I hope you have better luck than I've
had.
Pierre Gaston
2018-11-23 07:02:53 UTC
Permalink
Post by Peng Yu
Post by Dennis Williamson
https://unix.stackexchange.com/a/62883
This involves find. Anything native to bash that can return an arbitrary
file?
I'm pretty sure the only way to somehow access the content of a directory
without an external command is with a glob.
Find may be still faster than using a glob in a dir with many many files
since it doesn't do things like sorting
the filenames an probably use less memory.
Chris F.A. Johnson
2018-11-29 18:32:14 UTC
Permalink
Post by Peng Yu
Hi,
I'd like to get a file in a directory. It can be any file in the
directory. I don't care what it is as long as it is in the directory.
I don't want to use * to get all the files and then pick one from them
as this can be slow when there are many files in a directory. Is there
an efficient way in bash to get a file in a directory? Thanks.
Perhaps bash should have an option to not sort filename expansion: 'shopt -s nosortglob'?
--
Chris F.A. Johnson, <http://cfajohnson.com>
Loading...