Changing directories on the fly inside a script
Hi guys,
I want a user to be able to run my script, which works on a specific file inside a specific directory, from any directory he might be in right now. Fore example, I m currently in: /sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log In order to run the rest of my script, I need to be in: /sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/home/kos/replication Now, I don't know the starting directory. How can I force it always to /replication? Thanks! |
Quote:
|
to know the currnet directory you can run pwd.
to change directories you can use cd. |
Hey again,
I think I didn't explain myself good enough. I work with logs, and every log I extract goes to a dir named extracted<customername><date><time> Now, all I know is that starting from extracted* I have my log file structure. What's before that I don't know (the path before extracted*) and I don't know in which current dir I am. The file I want to work on is in /replication directory, which I want to parse. But I don't know from which dir the user runs the script, so I want it to navigate to / replication, no matter from where it's run AFTER the user has entered some log collection (meaning he's already in /extracted* ). I was thinking using while to check, something like: while [ pwd | grep extracted ] do cd .. done And then when I'm in the directory: /sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52, which is the log structure always start from, I can just cd to /file/home/kos/replication. I wanted to see if there is a more elegant solution. |
Code:
echo "$PWD" Code:
echo "$PWD" just some basic examples |
Quote:
|
Quote:
Thanks Firerat,but I didn't quite get how that works... this is one of my first scrips :( What path do I put instead of "/path/to/somedir"? The first constant in path is the word "extracted". All the folders between it and /sr/ can be random. Quote:
Let's take the path: Code:
/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log Code:
/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52 Code:
/sr/45689/new_logs/blabla/extracted123123/home/kos Code:
/sr/45689/new_logs/extracted123123/ |
Code:
$ x=/sr/42018/2014.07.16.16.13.52/extracted.ALEX-DR_RP.KBox1.2014.07.16.16.13.52/files/var/log Code:
extractDir=$(echo "$PWD" | sed 's:\(extracted[^/]*\).*$:\1:') |
Assuming the directory you want always ends with a digit
Code:
cd "$( grep -Eo "/.*extracted.*[0-9]/" <<<$PWD )/some/extra/subdir" Some links to help you with bash scripting http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ http://www.tldp.org/LDP/abs/html/ http://mywiki.wooledge.org/BashGuide http://www.gnu.org/software/bash/manual/bashref.html The wooledge is especially good, wish i'd known about that much sooner |
Oh right, forgot about grep -o.
Quote:
Code:
cd "$( grep -Eo '^.*extracted[^/]*' <<<"$PWD" )/some/extra/subdir" Quote:
|
Thanks ntubski and Firerat.
I used the grep option. So, for learning purposes: grep -Eo = enable regex and grep only what I wrote "/.*extracted.*[0-9]/" = something-extracted-something-number The only thing that I didn't understand is the triple <<< before $PWD. I understand logically is gives the grep the output of PWD, but why 3 < ? Firerat, regarding the links - thanks! I'm still learning.. that's why I ask questions to actually understand what the grep you gave is doing and not just copy-paste :) |
OK, found what's <<< in one of Firerat's links...
So it's the same as: Code:
"$PWD" | grep -Eo "/.*extracted.*[0-9]/" |
I'm sorry,but you are all fired
If the user runs the script as their own user id, then: cd ~/replication will do it or work on: do-stuff-to ~/replication/this-file |
Quote:
~/ is home directory, what gives you the idea they need to be in home dir? |
Quote:
Code:
echo "$PWD" | grep -Eo "/.*extracted.*[0-9]/" Quote:
Code:
do-stuff-to some/function/of/current/dir/replication/this-file |
All times are GMT -5. The time now is 06:53 PM. |