LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Display files/directories by letter (https://www.linuxquestions.org/questions/linux-newbie-8/display-files-directories-by-letter-4175463229/)

yumito 05-23-2013 06:23 PM

Display files/directories by letter
 
Question:
Enter the command to list files and directories in the coffees directory that start with the letter n using a relative pathname.

Background:
i know if i type "ls n*" it will display it for the current directory but how would i do it for another without physically changing the directory you are in.

The directory looks like:
/home/eric/dir1/coffees/nuts

Attempts:
i tried
ls ./dir1/coffees n*
ls ./dir1/coffees | n*
ls n* ./dir1/coffees
ls n* | ./dir1/coffees

i get the error:
ls: cannot access n*: no such file or directory

chrism01 05-23-2013 06:31 PM

Your first attempt is close
Code:

ls ../dir1/coffees/n*
NB: one '.' means current dir, 2 '..' means parent dir, which is what you need to move around
You can use as many dirs as it takes eg
Code:

ls ../../dira/dir1/coffees/n*
Useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

yumito 05-23-2013 06:41 PM

thanks!

David the H. 05-24-2013 12:03 PM

Also take some time to learn more about how globbing works. The shell will expand the wildcards in the given string into a list of matching filenames, if there are any, before the command is run, as a list of arguments for it to process.

For that reason, you also don't need to use ls, unless you want it's advanced file listing features. If you just want to print out a list of items, a simple built-in printf will do just as well.

Code:

printf '%s\n' ../../dira/dir1/coffees/n*


All times are GMT -5. The time now is 11:53 AM.