nova nova - 2 years ago 97
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:


In this directory is my script 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):


#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 Source
  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 *

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download