Results 1 to 4 of 4
Hi Everyone,
I'm new to linux so i apologize if this is a stupid question. basically im trying to be a little lazy.
im wondering if its possible to get ...
- 07-01-2011 #1Just Joined!
- Join Date
- Jul 2011
- Posts
- 5
Getting the folder name, when im not in it
Hi Everyone,
I'm new to linux so i apologize if this is a stupid question. basically im trying to be a little lazy.
im wondering if its possible to get the name of the first folder in a directory so basically when i go to usr/src/kernels i can grab the name of whatever folder is in there e.g. "2.6.37.2-8.27" without knowing what its called in the first place.
details:
the reason for this i currently have a bashrc function where i go:
"ensettar 2.6.37.2-8.27"
what this does is it set up a bunch of environment variables including one that is the kernel directory and kernel version of a remote computer.
im basically trying to eliminate the need to put the version number. Especially as my targets will usually only have one folder in the src/kernels directory. So whereas now i have:
ensettar()
{
...
export KERNELDIR=/target/usr/src/kernels/$1;
export KERNELVER=$1;
...
}
i would like something like:
ensettar()
{
cd /target/usr/src/kernels/
*magic*
...
export KERNELDIR=/target/usr/src/kernels/$FOLDER_1;
export KERNELVER=$FOLDER_1;
...
}
again i apologize if this is actually completely impossible and silly
I appreciate any help
- 07-01-2011 #2Just Joined!
- Join Date
- Jul 2011
- Posts
- 5
I have something, needs cleaning up
Hi guys,
I've written a script that does what i want, but its a bit...messy:
as you can see the for loop seems really unnecessaryCode:FILES=/target/usr/src/kernels/* #cycle through all "files" in that folder for f in $FILES do echo "getting $f file..." #change to dir pushd $f #set using reg expre ensettar ${PWD##*/} #ensure we return to original dir popd #break out of loop...ugly break doneLast edited by chrispepper1989; 07-01-2011 at 10:40 AM. Reason: improvement
- 07-02-2011 #3Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
i think this might give you what you want. it will find all dirs in /usr/src/kernels/ that start with the name of the currently running kernel on target system:
The 'head -n1' portion ensures that just the first matching directory name is returned, if multiple kernel dirs match the output of the 'uname -r' command - but if that is the case, you might want the 2nd one, etc.Code:find /usr/src/kernels -maxdepth 1 -type d -name $(uname -r)* -printf '%f\n'|head -n1
- 07-02-2011 #4Just Joined!
- Join Date
- Apr 2006
- Posts
- 2


Reply With Quote
