LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2013, 07:23 PM   #1
yumito
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
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

Last edited by yumito; 05-23-2013 at 07:28 PM.
 
Old 05-23-2013, 07:31 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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/
 
Old 05-23-2013, 07:41 PM   #3
yumito
LQ Newbie
 
Registered: May 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
thanks!
 
Old 05-24-2013, 01:03 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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*
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set the kde to display the directories and files in list view instead of icon vitalstrike82 Slackware 2 02-01-2009 02:45 PM
putty display loops on listing directories with many files, through expect script vineet.k Linux - General 5 03-13-2007 10:44 AM
Script to move directories based on first letter to a new directory of that letter tworkemon Linux - Newbie 8 01-30-2007 08:18 PM
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 04:22 PM
Display wide listing of files and directories only? northfield Linux - General 3 08-21-2002 07:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration