Absolute path to Relative Path in Unix

六月ゝ 毕业季﹏ 提交于 2019-12-08 09:54:32

问题


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.


回答1:


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.




回答2:


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:

  • bash — Convert absolute path into relative path given a current directory
  • How to get relative path from absolute path — tagged .net and applying to Windows rather than Unix.



回答3:


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



回答4:


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

ls ../fileAsst/*.txt

Hope that can help someone!



来源:https://stackoverflow.com/questions/12189961/absolute-path-to-relative-path-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!