# Unix Shell

**URL:** <https://www.ruby-forum.com/t/unix-shell/75719>\
**Category:** Ruby\
**Created:** [March 2, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/unix-shell/75719 "2006-03-02T16:22:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 2, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/unix-shell/75719/1 "2006-03-02T16:22:52Z")

</div>

I’m new to RUBY, but am familar with other scripting languages, PHP,  
Perl, Wscript, etc. I’d like to be able to run items from the shell  
such as ls -l \> dirlisting.txt. Just an example. Can I do this?

I would have just used the search on these forums, but unfortuantely  
when I click Search I get “The page cannot be found”. Hope they can  
get this fixed so I won’t have to ask every little question.

Thanks in advance.  
-Greg

---

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 2, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/unix-shell/75719/2 "2006-03-02T16:35:15Z")

</div>

Greg Johnson wrote:

> I’d like to be able to run items from the shell  
> such as ls -l \> dirlisting.txt. Just an example. Can I do this?

Try the Pickaxe book, the first edition is available on-line in pdf  
format. You can pass commands to the shell directly using back ticks `s -l > dirlisting.txt` or the %x expansion %x{echo “me, me, me…”}

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 2, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/unix-shell/75719/3 "2006-03-02T16:36:33Z")

</div>

On Fri, Mar 03, 2006 at 12:23:08AM +0900, Greg Johnson wrote:  
} I’m new to RUBY, but am familar with other scripting languages, PHP,  
} Perl, Wscript, etc. I’d like to be able to run items from the shell  
} such as ls -l \> dirlisting.txt. Just an example. Can I do this?  
}  
} I would have just used the search on these forums, but unfortuantely  
} when I click Search I get “The page cannot be found”. Hope they can  
} get this fixed so I won’t have to ask every little question.

There are two ways. One way:

file\_list = `ls -l`.scan(/^.\*$/)[1…-1]  
file\_list.each { |line|  
puts “permissions for #{line[49…-1]} are #{line[1…9]}”  
}

The other way:

system(‘ls -l \> dirlisting.txt’)

} Thanks in advance.  
} -Greg  
–Greg

---

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 2, 2006, 5:50pm UTC](https://www.ruby-forum.com/t/unix-shell/75719/4 "2006-03-02T17:50:36Z")

</div>

Greg Johnson wrote:

> Thanks for the tips, they work like a charm. Does anyone know why the  
> search functionality doesn’t work?

Not sure, but this search does:  
[http://dir.gmane.org/gmane.comp.lang.ruby.general](http://dir.gmane.org/gmane.comp.lang.ruby.general)

---

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 2, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/unix-shell/75719/5 "2006-03-02T17:21:49Z")

</div>

Thanks for the tips, they work like a charm. Does anyone know why the  
search functionality doesn’t work?

---

<div class="post-metadata">

**Author:** ![gjohnson71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a6a055/32.png) [@gjohnson71](https://www.ruby-forum.com/u/gjohnson71)\
**Post date:** [March 3, 2006, 9:24am UTC](https://www.ruby-forum.com/t/unix-shell/75719/6 "2006-03-03T09:24:23Z")

</div>

Gregory S. wrote:

> file\_list = `ls -l`.scan(/^.\*$/)[1…-1]  
> file\_list.each { |line|  
> puts “permissions for #{line[49…-1]} are #{line[1…9]}”  
> }
> 
> The other way:
> 
> system(‘ls -l \> dirlisting.txt’)

There are plenty of other ways.

system “ls”, “-l”  
system “bash”, “-c”, “ls -l \> dirlisting.txt”  
…  
Dir["\*"].each {|f| File.symlink? f and puts f}  
…

Cheers

```
robert
```
