Linux 如何在没有完整路径的情况下在文件夹和父文件夹之间复制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9928575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:29:27  来源:igfitidea点击:

how to copy between folders and parent folder without complete path

linuxcopyparentlocalcp

提问by hamad khan

This is a basic question but I am struggling to find a decent solution. This is hindering my script from automation.

这是一个基本问题,但我正在努力寻找合适的解决方案。这阻碍了我的脚本自动化。

I have the following path.

我有以下路径。

/home/hassan/Dyna/ProjectSimulation

in project simulation I have 3 folders

在项目模拟中,我有 3 个文件夹

friction time force

like

喜欢

/home/hassan/Dyna/ProjectSimulation/friction

Now I have a file friction1.txt in this friction folder and I want to copy it to ProjectSimulation.

现在我在这个摩擦文件夹中有一个文件摩擦 1.txt,我想将它复制到 ProjectSimulation。

is it possible to avoid complete path and just one step down?

是否有可能避免完整的路径和仅一步之遥?

Also if I have to copy this friction1.txt to folder force, is there anyway to avoid the complete path.

另外,如果我必须将此摩擦1.txt 复制到文件夹 force,是否有任何方法可以避免完整路径。

I mean I have a subroutine but this is path dependent , whenever I run it , I have to run in the same folder and then copy my results so I can run only one instance of my simulation.

我的意思是我有一个子程序,但这是依赖于路径的,每当我运行它时,我都必须在同一个文件夹中运行,然后复制我的结果,这样我只能运行一个模拟实例。

Experts please guide me.

请高手指导我。

PS: This is part of a 600 lines shell.

PS:这是 600 行 shell 的一部分。

采纳答案by HonkyTonk

This comes across as so basic that I must have misunderstood something in your question.

这太基本了,以至于我一定误解了您的问题中的某些内容。

If you want to refer to a parent directory, ..is the way to do that. So, if you want to copy friction1.txt to two places you just do

如果你想引用一个父目录,..就是这样做的方法。因此,如果您想将摩擦1.txt 复制到两个地方,您只需执行

cp friction1.txt ..
cp friction1.txt ../force

All you need to take care of is making sure that CWD is

您需要做的就是确保 CWD 是

/home/hassan/Dyna/ProjectSimulation/friction

/home/hassan/Dyna/ProjectSimulation/friction

so that the references point at the right place.

以便引用指向正确的位置。

回答by Attila

You can temprarily change the current directory to ProjectSimulation, copy the file (cp friction/friction1.txt .), then change the path back to the original (so the rest of the script works as before)

您可以临时将当前目录更改为 ProjectSimulation,复制文件 ( cp friction/friction1.txt .),然后将路径更改回原始路径(因此脚本的其余部分像以前一样工作)

Alternatively, you can use dirnameto get he name of the parent directory and use that.

或者,您可以使用dirname来获取父目录的名称并使用它。

回答by stolzem

Change to the root dir of your known directory structure. Then do the copy operations with relative paths. Then change back to your dir where you came from.

切换到已知目录结构的根目录。然后使用相对路径进行复制操作。然后改回您来自的目录。

Your friends are:

你的朋友是:

cd
cd -

or better:

或更好:

pushd
popd

(see man bash)

(见男人 bash)

I.e.

IE

pushd /home/hassan/Dyna/ProjectSimulation
cp friction/friction1.txt .
cp friction/friction1.txt force
popd