hamad khan
hamad khan

Reputation: 369

how to copy between folders and parent folder without complete path

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

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.

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.

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.

Upvotes: 12

Views: 42027

Answers (3)

HonkyTonk
HonkyTonk

Reputation: 2039

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

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

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

/home/hassan/Dyna/ProjectSimulation/friction

so that the references point at the right place.

Upvotes: 17

stolzem
stolzem

Reputation: 364

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)

I.e.

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

Upvotes: 0

Attila
Attila

Reputation: 28762

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)

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

Upvotes: 0

Related Questions