# Pathname#realpath over each entry in a directory

**URL:** <https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461>\
**Category:** Ruby\
**Created:** [January 11, 2014, 9:02pm UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461 "2014-01-11T21:02:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 11, 2014, 9:02pm UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/1 "2014-01-11T21:02:21Z")

</div>

tldr: Having trouble getting the target of symlinks within a specified  
directory.

My apologies for a very newbish question here.  
Background: As a learning exercise I want to write a program that will  
sync folders between my desktop and my Android phone using `adb push`.  
I know there are already apps that do this, but it seems a nice starter  
project to learn something from.

I have a script that will create a hidden desktop directory for each  
type of file to be synced (music, pictures, movies, etc). I have a  
script that will provide an entry on right click menus for “sync to  
android” with sub-entries for each category. This creates a symlink to  
the selected file or directory in the “sync” directory.

Problem: adb push does not work with symlinks. I need my “sync” script  
to read over the symlinks in the sync directories and return the target  
paths to `adb push`. I have tried any number of ways to make this  
happen and come up short. Any assistance is needed. Examples of things  
that do not work below:

Pathname("/#{Dir.home}/.ANDROIDMUSIC").each\_entry.realpath =\> ‘foreach’:  
no block given (LocalJumpError)

Dir.foreach("/#{Dir.home}/.ANDROIDMUSIC") { |entry|  
Pathname(entry).realpath } =\> in ‘realpath’: No such file or directory -  
/current/directory/with\_file\_from\_specified\_directory\_appended.mp3

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [January 11, 2014, 9:28pm UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/2 "2014-01-11T21:28:42Z")

</div>

# At the first example you should pass a block to the iterator

Pathname(Dir.home).each\_entry {|entry| p entry.realpath }

# The second, it’s probably what it’s telling you. The (actual) file

doesn’t exist.  
Only the symlink.

# I did

ln -s non-existing-dir/ my-symlink

# And now I have a symlink called “my-symlink” pointing to a

“non-existing-dir”.

# Now if I try:

Dir.foreach("/#{Dir.home}/") { |entry| p Pathname(entry).realpath }

# It gives me

Errno::ENOENT: No such file or directory -  
/Users/abinoam/non-existing-dir  
from (irb):35:in `realpath' from (irb):35:in`realpath’  
from (irb):35:in `block in irb_binding' from (irb):35:in`foreach’  
from (irb):35  
from /Users/abinoam/.rvm/rubies/ruby-1.9.3-p429/bin/irb:16:in `’

# If I just create the non-existing-dir

mkdir non-existing-dir

# It just pass ok.

You’re going well!

Keep going,  
Abinoam Jr.

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [January 11, 2014, 9:42pm UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/3 "2014-01-11T21:42:33Z")

</div>

Thank you so much for your assistance. The target of the symlink  
definitely exists. In fact:

Pathname.new("/path/to/symlink/file.mp3").realpath =\>  
/path/to/target/of/symlink.mp3

With either:  
Pathname("/#{Dir.home}/.ANDROIDMUSIC").each\_entry {|entry| p  
entry.realdirpath }

or

Dir.foreach("#{Dir.home}/.ANDROIDMUSIC/") { |entry| puts  
Pathname(entry).realdirpath }

No error is returned but the output is unexpected. What is returned is  
a path that does not exist. In essence it gives the path of the  
directory from which I am running the script in the terminal, that path  
with each filename appended, and the path of the parent directory. The  
middle entry does not really exist. So you’re correct that the path  
does not exist, but the script is somehow making it up, while the  
symlink actually points to a real path. Thanks again for your help.

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [January 11, 2014, 10:00pm UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/4 "2014-01-11T22:00:01Z")

</div>

Almost there. It seems that using File.realdirpath almost works.

Dir.foreach("#{Dir.home}/.ANDROIDMUSIC/") { |entry| puts  
File.realdirpath(entry, “#{music\_source}”) }

returns the correct path for each target of a symlink in the specified  
directory, but it also returns the path of that directory and the path  
of the directory from which the script is executed. Anyone have  
suggestions on how I can exclude those two entries? I certainly don’t  
want to end up copying my entire home directory onto my phone by  
accident.

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [January 12, 2014, 4:15am UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/5 "2014-01-12T04:15:55Z")

</div>

The following seems to do what I want except for including the directory  
from which the script is run (in this case my home directory).

def link\_targets  
music\_destination = “/sdcard/Music/”  
music\_source = “#{Dir.home}/.ANDROIDMUSIC”  
def push(source, destination)  
`adb push #{source} #{destination}`  
end  
Dir.foreach("#{music\_source}") { |entry| push  
“#{File.realdirpath(entry, “#{music\_source}”).to\_s }”,  
“#{music\_destination}”}  
end

link\_targets

If anyone can suggest a way to modify the File.realdirpath command so  
that it does not in fact return the home directory I would appreciate  
it. I cannot, in fact, understand why it is doing this to begin with.

Also, if the symlink is to a directory instead of a file, will  
File.realdirpath still work? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![notmatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f6c823/32.png) [@notmatt](https://www.ruby-forum.com/u/notmatt)\
**Post date:** [January 12, 2014, 5:18am UTC](https://www.ruby-forum.com/t/pathname-realpath-over-each-entry-in-a-directory/238461/6 "2014-01-12T05:18:55Z")

</div>

I’ve managed to get it working based on file extension, but that seems  
inelegant. I don’t want to have to specify every possible audio format  
out there just for the purpose of excluding these two directories. If  
someone can let me know just why those two directories are included in  
the output, I’d appreciate it. Finally, this does not work if an entire  
directory is linked. That is, the directory is treated as “not a music  
file” and the files within that directory are ignored rather than being  
pushed. Suggestions for how to dig not only the specified directory,  
but any subdirectories encountered? Thanks again. (Last response to my  
own  
question of the night, promise.)
