1826446 Members
4064 Online
109692 Solutions
New Discussion

Re: Scripting Novice

 
SOLVED
Go to solution
Acxiom Unix Team
Frequent Advisor

Scripting Novice

Hello,

I am still a scripting novice and so it still cause me headaches doing the easiest stuff. I wonder if you can help.

I want to pull out an long listing of a directory (not my current working dir) but filter out anything that is a directory. So i thought about using the command;

ls -lp | grep -v /

This works fine on the current working directory, but i wonder how i can integrate this into a script to get this listing from another directory.

Or please can you offer any other ways of getting a listing of just the files only in a directory, without it being your current working directory.

PS: Dont know if it makes any difference but it will have to be issued by sudo, so the command will be something like;

sudo -u root
Where is my beer...??
11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: Scripting Novice

>but filter out anything that is a directory.

Try this instead:
ll | grep -v "^d"

>but it will have to be issued by sudo

It shouldn't make a difference, unless /usr/bin isn't in root's path. (No /sbin/ll.)
Mark McDonald_2
Trusted Contributor
Solution

Re: Scripting Novice

Try this instead:
ll | grep -v "^d"

Just to help - this is using the fact that directories have a 'd' at the start of the permissions. So this is removing newline followed by 'd'
smatador
Honored Contributor

Re: Scripting Novice

HI,
if you want all the file in a directory you could do
find $DIRECTORY -type f
Ganesan R
Honored Contributor

Re: Scripting Novice

Hi Andrew,

If you want to long list only files on specific directory, use this command

#ll |grep -v ^d

Ex: ll /tmp |grep -v ^d

With sudo

#sudo -u root ll /tmp |grep -v ^d
Best wishes,

Ganesh.
Acxiom Unix Team
Frequent Advisor

Re: Scripting Novice

Thanks guys.

And thanks for the explanation Mark of what it actually does.

But one more quick question.

If i wanted to run that command ll | grep -v "^d" on a directory that was not my current working directory could that be done? I am guessing so but i just can figure it out as simply doing that command followed by a directory doesnt work.



Where is my beer...??
Acxiom Unix Team
Frequent Advisor

Re: Scripting Novice

Sorry, forget last posting. Ganesan R has answered my question and shown how daft i am being...

I will assign points and then close the call.

Thanks Again everyone......
Where is my beer...??
Acxiom Unix Team
Frequent Advisor

Re: Scripting Novice

I was given the answer by various people...Ta
Where is my beer...??
Steven E. Protter
Exalted Contributor

Re: Scripting Novice

Shalom,

cd into the directory before running the command.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
dirk dierickx
Honored Contributor

Re: Scripting Novice

hue.. you need to spend some time getting to know the basic unix commands. sure you can do an ls and then filter out anything which is not a file, but why would you do that when 'find' does those things for you?

find -type f <- shows only files
find -type f <- will show only dirs

and find has many options which will get you were you want to go next.
Dennis Handly
Acclaimed Contributor

Re: Scripting Novice

>Dirk: but why would you do that when 'find' does those things for you?

Because find(1) just gives names and you would have to feed it back to ll(1) anyway? Because find will descend directories and the stinkin' documentation doesn't tell you how to to use -prune?
But you could be right. Andrew asked how to use grep to get something but perhaps not what he really wanted.

>find -type f <- will show only dirs

That's: -type d
dirk dierickx
Honored Contributor

Re: Scripting Novice

that last one was a typo ofcourse :|

after rereading his post i noticed he wanted a 'long listing' of a directory, which probably means he needs the ownership and rights as well, grepping ll could be the fastest way to get there.