-3

I need to use the ls command to list files that begin with the letter 'r'.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Logan P
  • 23
  • 1
    Watch out that is a trick question. ls cannot be used to look in the contents of the file to see if it starts with the letter r you would need head and grep for that or alternatively awk, ls only works with filenames. – Anthon Oct 05 '15 at 18:02

1 Answers1

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 do ls /bin | grep -e ^r the ^ character indicates beginning of line. – Joseph Glover Oct 05 '15 at 16:58
  • 3
    Joseph, RegEx are completely irrelevant here – Chris Davies Oct 05 '15 at 17:08
  • 1
    Oh, 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
    Try: echo $(cd /bin; ls r*). No grep needed. –  Oct 05 '15 at 22:34