0

This command was to list only the text files in my fileAsst directory:

  ls ~/UnixCourse/fileAsst/*.txt

now I need to do the same thing but using a relative path instead, what I've tried so far is:

  ls ~/UnixCourse/../*.txt 

but it's saying I'm not getting the right answer, can anyone give me some hints or explain the differences between a relative path and absolute path, because I still dont under it.

thar45
  • 3,518
  • 4
  • 31
  • 48
Joseph Do
  • 23
  • 2
  • 5

4 Answers4

0

Absolute Path: The absolute path is a path that contains the root directory and all other sub directories that contain a file or folder.

  ~/UnixCourse/fileAsst/*.txt

Relative path: The relative path is only a portion of the full path.

  cd ~UnixCourse/fileAsst/ 

  ls *.txt
thar45
  • 3,518
  • 4
  • 31
  • 48
0

Relative is always, well, relative to some existing directory, you are currently "located" in (by means of the cd command, usually). In your question you don't show what the current directory is. So there is no single "correct" answer.

If your current directory is, say, ~ (which is just a shortcut for your home directory, for example, /home/myuser), then you're relative ls command would look like (I'm adding the implied previous cd command for clarity):

 cd ~
 ls UnixCourse/fileAsst/*.txt

likewise if your current directory is ~/UnixCourse, then your relative ls command would look like:

 cd ~/UnixCourse
 ls fileAsst/*.txt

or the most simply case, when you are already in the directory you want to list the contents of:

 cd ~/UnixCourse/fileAsst
 ls *.txt

Get the idea?

Finally, as you have (accidentally, I'd assume) discovered, you can use .. and . in your paths, to imply "one (sub)directory up" or "the current directory".

For example, the following paths are equivalent and all resolve to "UnixCourse/fileAsst":

 UnixCourse/../UnixCourse/fileAsst/
 UnixCourse/SomeOtherDir/../fileAsst/
 UnixCourse/./fileAsst
 UnixCourse/fileAsst/YetAnotherDir/../

Note that this is a orthogonal concept and can be used with both, relative and absolute, paths.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
0

There are two classes of pathname:

  1. Absolute: start with a slash.
  2. Relative: don't start with a slash.

Absolute pathnames have the same meaning regardless of your current working directory. An absolute pathname might contain .. components after the initial slash. An absolute pathname with no .. components that traverses no symlinks is sometimes known as a 'real path', after the system call realpath() that can be used to determine the real path of a name.

Relative pathnames are relative to the current working directory; conceptually, every relative pathname could be deemed to start with ./. A relative name might start with .. to move upwards from the current working directory. A special case of relative pathname is a simple filename — a name with no explicit directory component. That is a file (or other named object) in the current directory, of course.

To determine a relative pathname from an absolute pathname, you also have to know the current working directory.

For more information, see:

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-1

Didn't see a one-lined answer...

ls ../fileAsst/*.txt

Hope that can help someone!