Absolute path to Relative Path in Unix - 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.

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.

There are two classes of pathname:
Absolute: start with a slash.
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.

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

Didn't see a one-lined answer...
ls ../fileAsst/*.txt
Hope that can help someone!

Related

How to move file from one directory to another (Julia)?

I have two different paths (directory path and file path). I need to move the file from one directory to another. How i should do it?
Use the mv function.
help?> mv
…
mv(src::AbstractString, dst::AbstractString; force::Bool=false)
Move the file, link, or directory from src to dst. force=true will first remove an existing dst. Return dst.
One thing to note is that both src and dest must be complete paths i.e. if your source file path is src = /home/me/file.txt and you want to move that to be under the directory path dstdir = /home/me/.julia, the call should be mv(src, joinpath(dstdir, basename(src)).
(Just to be clear, the arguments can be absolute paths or relative paths, either works. By "complete paths" I only mean that both src and dst have to include the file name, dst can't be just a directory unless you wish you overwrite the directory itself.)

Moving files from current directory to one directory below

I am trying to move 2 files from current directory (/base/level1/level2) to one directory below (/base/level1)
Is there any easier command other than mv file1 /base/level1 ? I'm trying to understand if we have some command that move it to a specific level up or down in the current folder structure.
TIA!
My solution to this one is navigating to the path where I want to move the file and run the following command.
cp ./level2/file1 .
or
cp ./file1 ./level2/file1
Please share other solutions as well
Thanks,
AMK
You could use wildcards if the 2 files have something uniquely in common.
ie. mv file*.ext path/to/new/dest/
This will move all files starting with "file" and ending with the extension ".ext" to the destination. Have a look at this and this which will explain wildcards a bit more
You can always use .. for "one directory up".
And you can give more than 2 arguments to mv, the last always being the destination.
So mv file1 file2 .. would move those 2 files a directory up.
Or mv * .. to move all files.

What is the QHG_LOCATION path relative to for doxygen?

When I set QHG_LOCATION to be an absolute path, I can create a valid doxygen documentation in html and qch. If I put qhelpgenerator and its DLLs into the current directory ./ and set QHG_LOCATION=qhelpgenerator, it also works. But I cannot figure out how to put the qhelpgenerator into a subdirectory qt/bin/qhelpgenerator and have doxygen find it. It always says
error: failed to run qhelpgenerator on index.qhp
which means that it could not find qhelpgenerator.
So the question is: If I want QHG_LOCATION to be a relative path, what directory is it relative to? It is neither relative to ./ nor to the html output directory.
Debugging the problem can be done by outputting the external commands executed:
doxygen -d extcmd and execute the given command 'by hand' outside of doxygen.
From the documentation of QHG_LOCATION:
The QHG_LOCATION tag can be used to specify the location of Qt's qhelpgenerator. If nonempty
doxygen will try to run qhelpgenerator on the generated .qhp file.
and from the documenattion of QCH_FILE:
If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify the file name of
the resulting .qch file. The path specified is relative to the HTML output folder.
Indeed, both QHG_LOCATION and QCH_FILE must be relative to the HTML output directory. Furthermore, you must use backslashes \ as directory separators on Windows. Forward slashes are supported in the rest of the doxyfile but not here.
I find both issues quite irritating, but this is the way it is.
Thanks to albert for a helpful comment!

Setting path in bash_profile

Why does setting a PATH require the :${PATH} at the end?
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
When I append a path to this I would do
PATH=$PATH:...
How do I append a PATH without going to a new line. That is, how would I append a PATH to the original PATH statement.
If I wanted to put the following all in the first line, for instance. How does this interact with the :${PATH} part?
PATH=$PATH:/usr/local/mysql/bin
There's a difference between appending an existing path to the front or the end of the $PATH environment variable. The way bash resolves execution paths is by starting from the front of the list. That means if you have these two directories in your path:
PATH="/dir1/bin:/dir2/bin"
And they both have the executable test.sh in it, then when you run test.sh, it'll execute the one in /dir1/bin/test.sh since that directory appears first in the path.
Additionally, ${PATH} is the same as $PATH.
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
is simply adding /Library/Frameworks/Python.framework/Versions/2.7/bin to the front of the path and
PATH=$PATH:/usr/local/mysql/bin
is simply adding /usr/local/mysql/bin to the end of the path.
So how do you do both in one line? Something like this:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}:/usr/local/mysql/bin"

Unix wildcard selectors? (Asterisks)

In Ryan Bates' Railscast about git, his .gitignore file contains the following line:
tmp/**/*
What is the purpose of using the double asterisks followed by an asterisk as such: **/*?
Would using simply tmp/* instead of tmp/**/* not achieve the exact same result?
Googling the issue, I found an unclear IBM article about it, and I was wondering if someone could clarify the issue.
It says to go into all the subdirectories below tmp, as well as just the content of tmp.
e.g. I have the following:
$ find tmp
tmp
tmp/a
tmp/a/b
tmp/a/b/file1
tmp/b
tmp/b/c
tmp/b/c/file2
matched output:
$ echo tmp/*
tmp/a tmp/b
matched output:
$ echo tmp/**/*
tmp/a tmp/a/b tmp/a/b/file1 tmp/b tmp/b/c tmp/b/c/file2
It is a default feature of zsh, to get it to work in bash 4, you perform:
shopt -s globstar
From http://blog.privateergroup.com/2010/03/gitignore-file-for-android-development/:
(kwoods)
"The double asterisk (**) is not a git thing per say, it’s really a linux / Mac shell thing.
It would match on everything including any sub folders that had been created.
You can see the effect in the shell like so:
# ls ./tmp/* = should show you the contents of ./tmp (files and folders)
# ls ./tmp/** = same as above, but it would also go into each sub-folder and show the contents there as well."
According to the documentation of gitignore, this syntax is supported since git version 1.8.2.
Here is the relevant section:
Two consecutive asterisks (**) in patterns matched against full pathname may have special meaning:
A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the
same as pattern foo. **/foo/bar matches file or directory bar
anywhere that is directly under directory foo.
A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of
the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/**/b matches a/b,
a/x/b, a/x/y/b and so on.
Other consecutive asterisks are considered invalid.

Resources