nova nova - 1 month ago 13
Bash Question

How to work in other directories via a bash script?

I already searched stack overflow, but there was nothing suitable for my problem.
What I wanted to do:

I have a directory:

~/data/project/bin


In this directory is my script firstScript.sh. I want it to do 3 things:


  1. get into directory: ~/mytest

  2. take every file in there

  3. and use a command on it (for example cat)



To realize the first Step I found this question and tried this (used ls -l as command to see if the right directory was found):

#!/bin/bash

#step 1 get into the directory
#setp 2 loop through all files
#step 3 add the command

cd /mytest ls -l

#also tried:
# cd ~/mytest ls -l


nothing happened, so I tried the path and command by myself in the console/terminal and it is the correct path for sure. Any Idea what is wrong with this? Also tried this with ./relativ/path, but this didnt work as well.

Answer
  1. get into directory: ~/mytest cd ~/mytest
  2. and use a command on it (for example cat) cat
  3. take every file in there cat *

cd ~/mytest; cat *