Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    6

    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

  2. #2
    Just Joined!
    Join Date
    Jul 2011
    Posts
    6

    I have something, needs cleaning up

    Hi guys,

    I've written a script that does what i want, but its a bit...messy:

    Code:
    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 
    
    done
    as you can see the for loop seems really unnecessary
    Last edited by chrispepper1989; 07-01-2011 at 10:40 AM. Reason: improvement

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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:
    Code:
    find /usr/src/kernels -maxdepth 1 -type d -name $(uname -r)* -printf '%f\n'|head -n1
    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.

  4. #4
    Just Joined!
    Join Date
    Apr 2006
    Posts
    2
    Quote Originally Posted by chrispepper1989 View Post
    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.
    Maybe try using
    Code:
    uname -r

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •