5

The question is:

What command would you use to list the text files in your fileAsst directory (using a relative path)?

The previous question was:

Give a command to list the names of those text files, using an absolute path to the fileAsst directory as part of your command.

The answer was:

~/UnixCourse/fileAsst/*.txt

I was wondering how I can list the files in this directory using a relative path. I've tried several commands including:

ls ~/UnixCourse/fileAsst/*.txt|awk -F"/" '{print $NF}'
(cd ~/UnixCourse/fileAsst/*.txt && ls )

and a bunch of others.

But it keeps telling me their invalid. I know it has to be a correct answer because others have gotten past this. But right now I'm stuck and extremely frustrated =(

UPDATE:

After going to the CS lab someone helped me figure out the problem. I needed to be in a certain working directory at first, and I wasn't. After switching to that directory all I needed was the command:

../UnixCourse/fileAsst/*.txt

and that took care of it for me. Thanks to everyone that helped and I hope this helps someone else.

pob21
  • 1,918
  • 2
  • 16
  • 17
  • 1
    A relative path is any path that does not start with `/`, with the caveat that `~` is a shorthand for an absolute path. So, if you are in your home directory, `echo UnixCourse/fileAsst/*.txt` generates a list of relative names; so does `(cd UnixCourse; echo fileAsst/*.txt)` or `(cd UnixCourse/fileAsst; echo *.txt)` — all subject to the caveat that 'text files' are files with a name ending `.txt`. – Jonathan Leffler May 06 '13 at 18:52
  • @JonathanLeffler thanks a lot for your response. Similar to Fredrik your code worked great while testing, but when I tried to get it thru his auto grader, it told me that wasn't what it was looking for. I appreciate the help though. – pob21 May 06 '13 at 18:58
  • How about `ls -1 ~/UnixCourse/fileAsst/*.txt`? – ott-- May 06 '13 at 19:14
  • Thanks @ott-- but that line is returning me the absolute path – pob21 May 06 '13 at 19:16
  • As my previous comment at least hinted, to be able to create a relative path to a file, you have to know the directory the path must be relative to. Look at [Absolute path to relative path in Unix](http://stackoverflow.com/questions/12189961/absolute-path-to-relative-path-in-unix/12190343#12190343) and [`bash`: convert absolute path into relative path given a current directory](http://stackoverflow.com/questions/2564634/bash-convert-absolute-path-into-relative-path-given-a-current-directory/2565049#2565049), to name but two related questions. – Jonathan Leffler May 06 '13 at 20:24

1 Answers1

7

try:

$ cd ~/UnixCourse/fileAsst/
$ find .

as a one-liner (executing in a sub-shell)

$ (cd ~/UnixCourse/fileAsst/ && find .)

another approach

$ (cd ~/UnixCourse && ls fileAsst/*.txt

$ ls ~/UnixCourse/fileAsst/*.txt
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Thanks for the quick reply Fredrik, but I forgot to mention the answers have to be one liners – pob21 May 06 '13 at 18:49
  • see uppdate, placing the expression within brackets executes it in a subshell without changing the parent i.e. in this case changing directory. – Fredrik Pihl May 06 '13 at 18:52
  • Thanks Fredrik, although your code worked perfectly, his auto-grader is still saying that isn't the correct response. I really appreciate the help. – pob21 May 06 '13 at 18:54
  • Even though this works great I'm still getting the say error message while trying to get it thru the auto grader =( – pob21 May 06 '13 at 19:04
  • Answers given by myself and the mighty Jonathan Leffler are correct, I'd suggest you do some permutations of those and see if you can get past the autograder – Fredrik Pihl May 06 '13 at 19:06
  • Yeah I'm gonna go ahead and see what I can do, thanks a lot – pob21 May 06 '13 at 19:08