I need to use the ls command to list files that begin with the letter 'r'.
Asked
Active
Viewed 7,736 times
-3
1 Answers
4
ls r*
Explanation: The *
is a special character that does Filename Expansion when run in a shell, essentially expanding out the r
character to anything in that directory that starts with an r
. See this for a full explanation. Filename Expansion
For questions on command lines commands, type
man <command>
To see the manual pages for that command, so
man ls

Joseph Glover
- 353
- 1
- 2
- 9
-
This didn't work, I am trying to list files of the bin directory so I did
ls /bin r*
and it told me that there is no such file or directory. – Logan P Oct 05 '15 at 16:55 -
Try
ls /bin/r*
This will still list the /bin part, so if you just want the bin names you can dols /bin | grep -e ^r
the^
character indicates beginning of line. – Joseph Glover Oct 05 '15 at 16:58 -
3
-
1Oh, I see, it has to do with globbing. Logan, see this for a better explanation of what is going on.
grep -e [PATTERN]
does use regex though, so if you pipe to grep you can do more complex patterns like that. – Joseph Glover Oct 05 '15 at 17:22 -
1
ls
cannot be used to look in the contents of the file to see if it starts with the letterr
you would needhead
andgrep
for that or alternativelyawk
,ls
only works with filenames. – Anthon Oct 05 '15 at 18:02