Joseph Do
Joseph Do

Reputation: 23

Absolute path to Relative Path in Unix

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.

Upvotes: 0

Views: 8910

Answers (4)

Hamilton Flood
Hamilton Flood

Reputation: 1

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

ls ../fileAsst/*.txt

Hope that can help someone!

Upvotes: -1

thar45
thar45

Reputation: 3560

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

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754530

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:

Upvotes: 0

Christian.K
Christian.K

Reputation: 49280

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.

Upvotes: 0

Related Questions