View Full Version : [all variants] Quick Bash Script help
shironeko
May 16th, 2008, 03:05 PM
Hello to everyone,
there's some quick bash script help I would need.
What I want to do is that if a file exists in a directory, the scripts changes the directory to where that file was found.
if [ -f */file.x ] #checks that file exists
then
cd ......... #here it should change to the directory of the file
rm [^file.x] #Delete everything but the file
cd .. # go back to the parent directory.
fi
As you can see what I'm missing is a way to move to the directory of a given file.
Thank you.
|{urse
May 16th, 2008, 03:08 PM
As you can see what I'm missing is a way to move to the directory of a given file.
A little confused, cd /where/you/want/to/be is the way to move to the directory.
|{urse
May 16th, 2008, 03:12 PM
or do you mean you want it to change to the directory of the given file as it finds it?
shironeko
May 16th, 2008, 03:13 PM
or do you mean you want it to change to the directory of the given file as it finds it?
Exactly.
It should change to the directoy where the file is.
Imagine we've got these directories
1
2
3
4
5
And the file is hidden somewhere under those 5 directories.
If it is, it should change into the one where it is.
|{urse
May 16th, 2008, 03:18 PM
cd ~
find . -name "filename.extension"
will search recursively starting from you home folder for the file filename.extension and return back the path
if you want the full path of the files the start searching from the root folder /
you could make the output of that a variable $whatever and cd $whatever
shironeko
May 16th, 2008, 03:20 PM
The point is that I want to create a script, so that the script automatically operates inside the directory where it found the file.
|{urse
May 16th, 2008, 03:43 PM
if you use find . -name you cand send its results to a variable with a > $whateveryouwanttocallit then cd $whateveryoucalledit,
Monicker
May 16th, 2008, 03:49 PM
find also has a -execdir option, for doing operations in the directory where a file is found.
shironeko
May 16th, 2008, 04:02 PM
Thank you!
That is what i needed
|{urse
May 16th, 2008, 04:07 PM
.
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.