Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

piping to chmod?

6,485 views
Skip to first unread message

Jens-Peter Olsson

unread,
Jan 12, 1998, 3:00:00 AM1/12/98
to

I want to do something like this:

file * | grep executable | awk -F":" {print $1} | chmod 755

How do I get chmod to interprete the result from the pipe?

I know this can probably be done with find or something, but I've had
several similiar cases before...

J-P

--- Remove "no" from my mail-adress to reply. ---

Mark Mraz

unread,
Jan 12, 1998, 3:00:00 AM1/12/98
to

Jens-Peter Olsson wrote:
>
> I want to do something like this:
>
> file * | grep executable | awk -F":" {print $1} | chmod 755
>
> How do I get chmod to interprete the result from the pipe?
>
> I know this can probably be done with find or something, but I've had
> several similiar cases before...

You can use the xargs command:

file * | grep executable | awk -F":" {print $1} | xargs chmod 755

Mark
--
Real Name: Mark Mraz
Email Address: mm...@rpa.net
Web Page: http://www2.rpa.net/~mmraz

Barry Margolin

unread,
Jan 13, 1998, 3:00:00 AM1/13/98
to

In article <34b92be0...@news.algonet.se>,

Jens-Peter Olsson <nojpo...@algonet.se> wrote:
>I want to do something like this:
>
>file * | grep executable | awk -F":" {print $1} | chmod 755
>
>How do I get chmod to interprete the result from the pipe?

chmod expects the filenames to be in its argument list, not standard
input. You can use xargs or `` substitution:

file * | grep executable | awk -F":" '{print $1}' | xargs chmod 755

chmod 755 `file * | grep executable | awk -F":" '{print $1}'`

BTW, awk has built-in regexp matching, so you don't need to use grep:

file * | awk -F":" '/executable/ {print $1}'

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.

Bob Vickers

unread,
Jan 13, 1998, 3:00:00 AM1/13/98
to Jens-Peter Olsson

Jens-Peter Olsson wrote:
>
> I want to do something like this:
>
> file * | grep executable | awk -F":" {print $1} | chmod 755
>
> How do I get chmod to interprete the result from the pipe?
>

On at least some Unix variants there is an X permission which does
exactly what you want:
chmod -R a+rX,go-w .

So no pipes are needed. Check out the chmod man page.

Bob
--
======================================================================
Bob Vickers R.Vi...@rhbnc.ac.uk
Dept of Computer Science, Royal Holloway College, University of London
WWW: http://www.cs.rhbnc.ac.uk/home/bobv
Phone: +44 1784 443691

Allen Kirby

unread,
Jan 13, 1998, 3:00:00 AM1/13/98
to Jens-Peter Olsson

Jens-Peter Olsson wrote:
>
> I want to do something like this:
>
> file * | grep executable | awk -F":" {print $1} | chmod 755
>
<snip>

Try this:
file * | grep executable | awk -F":" {print $1} | xargs chmod 755

You might also consider just doing chmod a+x which will add execute
permission but won't change the existing read/write permissions (unless,
of course, you actually WANT to change them to 755).
--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.

Jens-Peter Olsson

unread,
Jan 13, 1998, 3:00:00 AM1/13/98
to

Thank you very much for the help!

Surya Avantsa

unread,
Jan 15, 1998, 3:00:00 AM1/15/98
to

Mark Mraz wrote:
>
> Jens-Peter Olsson wrote:
> >
> > I want to do something like this:
> >
> > file * | grep executable | awk -F":" {print $1} | chmod 755
> >
> > How do I get chmod to interprete the result from the pipe?
> >
> > I know this can probably be done with find or something, but I've had
> > several similiar cases before...
>
> You can use the xargs command:
>
> file * | grep executable | awk -F":" {print $1} | xargs chmod 755
>
> Mark
> --
> Real Name: Mark Mraz
> Email Address: mm...@rpa.net
> Web Page: http://www2.rpa.net/~mmraz

U can't really pipe to chmod, because chmod does not expect any input
from the stdin. You may want to use `<unix-cmd>` ie any unix command
within ` characters, would replace itself with the output of that
command.

For eg., echo `pwd`

wud echo the output of the pwd command.

So chmod ... `unix-cmd-to-get-whatever-file-names-u-want'

wud do what u want.

Message has been deleted

James R. Martin

unread,
Jan 17, 1998, 3:00:00 AM1/17/98
to

Jens-Peter Olsson (jpol...@algonet.se) wrote:
: I want to do something like this:
: file * | grep executable | awk -F":" {print $1} | chmod 755
: How do I get chmod to interprete the result from the pipe?
: I know this can probably be done with find or something, but I've had
: several similiar cases before...

#set -x # uncomment to observe evaluation of constructed chmod commands
eval `file * |
sed -n '/executable/{
s/:.*$//
s/^/chmod 755 /
# the following line shouldn't be necessary, but I found
# that my sed doesn't print newlines given the "n" option
# If yours works per manual, the below shouldn't break it
s/$/;/p
}'
`
set +x

You could also have used the "xargs" command (see man page), but
having combined the grep and awk pass into one sed command, one
might as well use the regular expression capabilities of sed to
also construct the entire command line, and evaluate that through
the "eval" builtin.

P.S. Note that, unlike a "find" command, the above will _not_ descend
into subdirectories.

P.P.S. Alert readers will see that since the "chmod" command is
executed once per applicable executable filename, there will be
_more_ processes used than will a single constructed line of one
chmod with multiple file arguments. However; sed is perfectly
capable of storing up multiple lines in its hold buffer, and
essentially turning it into _one_ chmod command, (reinforcing my
contention that minimizing process creation is an important
consideration of the shell programmer) but I wanted to keep the
example code above simple.

P.P.P.S. Is it true that all Swedes speak/write as good or better
English than native speakers? :-) I have never met or known of a
Swede who couldn't converse in English quite respectably.

-James


Jens-Peter Olsson

unread,
Jan 18, 1998, 3:00:00 AM1/18/98
to

Well, better I don't know, but Sweden is a small country so the need
to learn english have always been a priority quite naturally.

J-P

On 17 Jan 1998 21:08:49 GMT, jrma...@rainey.blueneptune.com (James R.
Martin) wrote:

>P.P.P.S. Is it true that all Swedes speak/write as good or better
>English than native speakers? :-) I have never met or known of a
>Swede who couldn't converse in English quite respectably.
>
>-James
>

0 new messages