0

I was wondering if there is any way to redirect created files from a .sh script to a specific directory without modifying the script itself but straight from command line.

Let's say:

I've a script prove.sh that creates some file in the current directory:

cat prove.sh


#! /bin/bash

echo "try 1" > try.1.txt

echo "try 2" > try.2.txt

and I would like to redirect those created files not in the current directory but in another directory (let's say ~/Documents/prove/) straight from my:

sh prove.sh <...something to redirect files to directory...>

without modifying the script itself.

Do you think is it possible?

Anyone has got a clue?

EDITED: More specifically, what I need to do is to redirect the output of a program that runs on a server and that creates various file (not stdout nothing) in a specified directory in the --out field, to another computer directly (without writing anything in the original server where the program runs).

I've only host access to this server so I can not install or sudo anything. Listening a port in the second server and redirect the output to this port in nectat did not work due to firewall in the server where the program runs that I cannot change.

Some idea?

  • What do you mean by in a specified directory in the --out field? Your added "EDITED" bit changes the original question quite a bit. Could you maybe combine the two so that it reads as a single uniform question? – Kusalananda Jan 25 at 16:13
1

Assuming the script looks exactly like what you have shown:

#! /bin/bash
echo "try 1" > try.1.txt
echo "try 2" > try.2.txt

The files would be created in the current directory, regardless of what the current directory was.

Therefore, to create the files in ~/Documents/prove/, you would be able to do

( cd ~/Documents/prove/ && bash /some/location/prove.sh )

where /some/location/prove.sh is the pathname of the script itself.

This would cd to the given directory first, and if that went well (the directory exists and you have permission to be there) then bash would be invoked to run the script and the files would be created there, assuming you had permissions to create files in that directory.

I put the commands within ( ... ), a subshell, so that the cd would not affect the original working directory. This means that you should find yourself in the same directory where you started after executing that line.

Without the ( ... ), you would have changed directory to ~/Documents/prove/.

  • Thanks Kusalananda, but I was wondering if there is a way to call the script BEFORE and then redirect it (because I need to pipe in SSH so I have to call the script before CD) – cccnrc Jan 25 at 14:32
  • @ccc.nrc There is absolutely nothing preventing you from running both a cd and the script in the same ssh call. Please update your question with the requirements that you have. – Kusalananda Jan 25 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.