I need to use the ls command to list files that begin with the letter 'r'.
Asked
Active
Viewed 7,371 times
-3
-
1Watch 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 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 do `ls /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](http://unix.stackexchange.com/questions/62660/the-result-of-ls-ls-and-ls) 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