How to copy between folders and parent folder without full path

This is the main question, but I'm struggling to find a decent solution. This prevents script automation.

I have the following path.

/home/hassan/Dyna/ProjectSimulation 

in modeling the project i have 3 folders

 friction time force 

as

 /home/hassan/Dyna/ProjectSimulation/friction 

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

Is it possible to avoid the full path and just one step down?

Also, if I need to copy this friction1.txt file to a folder, is there a way to avoid the full path.

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

Experts, please call me.

PS: This is part of a 600-line shell.

+6
source share
3 answers

This seems so basic that I must have misunderstood something in your question.

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

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

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

/home/hassan/Dyna/ProjectSimulation/friction

so that the links are in the right place.

+9
source

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

Alternatively, you can use dirname to get the name of the parent directory and use it.

0
source

Change to the root directory of your known directory structure. Then perform copy operations with relative paths. Then go to your directory where you came from.

Your friends:

 cd cd - 

or better:

 pushd popd 

(see man bash)

those.

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

Source: https://habr.com/ru/post/911956/


All Articles