Working with multiple home directories.

I need to rename a directory in every home directory on a given workstation. I am a newb to scripting so maybe thats why I cant exactly figure out how to correctly do this.

The first thing I need to be able to do to write this script is figure out how to list all the directorys (these are not actual home directorys).

Im using solaris so that may complicate things for me.

ls -d only lists "." so that command is worthless.. for this.

find /path/to/users -type d ; somewhat works but it also lists every directory past users. I cant seem to find a way to tell it to only look in the one directory.the output is the following:

/path/to/user1
/path/to/user1/dir1
/path/to/user1/dir2
/path/to/user2/
/path/to/user2/dir1
/path/to/user2/dir2

I need to only see /path/to/user1 and /path/to/user2.

Anyone have any ideas on else I could try? Also, if it matters, Im using solaris to work on/write this script.. but the actual script will be for OSX, a post install script for an application upgrade.

edited for a little more info. :slight_smile:

To list all the directories ,

In solaris , u can use dir command.

Even u can use this : ls -ltr | grep "^d"

Thanks
Penchal

awesome penchal!

ls -ltr | grep "^d" | cut -c55-75

will work! Ill see if I can get what I want with dir without having to crep and cut as well :slight_smile:

edit: On second though.. is there a way to just print everything from the 55 characters spot to the last character on the line? I think my variables with cut -c55-75 would include spaces tailed at the end? Im thinking tr could be used to do that.. but it would just make the command that much longer?

ls -ltr | grep "^d" | cut -c55-75 | tr -d ' '

??? Is that too long of a command to throw in a script as a vairable? I dont think so.. but im still newbish to this :frowning:

Hi,

Use this :

ls -ltr | grep "^d" | awk -F" " '{print $NF}'

Thanks
Penchal

awesome, Ill man awk now to learn what all it can do :slight_smile:

Thanks for your help!

find . -type d 

lists all directories as well.

it does but it lists all directories recursivly which I did not want. I was just looking for directories in the current directory

penchal,

I changed your line a bit awk can parse, whatever it is called, the ^d so grep is not needed.

ls -ltr | awk '/^d/ {print $NF}'

Im still reading up on awk to figure out what exactly the $NF does though :smiley:

ok, $NF is just a variable for the number of fields in the file.. so whenI call it it just automagically calls the last field to be printed? Is that correct?

Yes that is what $NF refers to.

awesome, thanks everyone. I didnt realize this scripting stuff wasnt so hard to figure out. I wish I would of jumped on learning this stuff a few years ago :slight_smile: