Ram Patidar
Ram Patidar

Reputation: 666

How to check sublink file is present

I saved a file outside of rails projet and created sublink of that file in rails project but when I tried to check File.exists(sublink_file_path) it is returning false.

Eg:

 file_path = "shared/test.xls" #original file outside rails project.
 ln -nfs shared/test.xls current/tmp/test.xls #created a sublink in rails tmp folder. here current is my project folder.

Now fetching file path in controller like this

file_path = Rails.root + "tmp/test.xls"
File.exists?(file_path) #it return false but it should return true.

also check File.exist?(file_path) also return false.

How can I check sublink file is exist in ruby?

my directory structure:

 Work
   Project
     app
     config
     db
     tmp
     and so on...
  Shared

here project and shared in same level inside work folder. I am sure I am corrected with file path and directory structure. My concern is we can't check sublink path as file.

File.exist?(file_path) it will return true because file path is actual file path.

File.exist?(sublink_file_path) I think sublink file path is not a file that's way it's returning false

Upvotes: 2

Views: 578

Answers (2)

Ram Patidar
Ram Patidar

Reputation: 666

I have to set Absolute path in my case it is not working with relative path. I used :

ln -nsf /home/work/shared/test.xls /home/work/current/tmp/test.xls

check symbolic link is creating properly or not. Use below command

file "your_symbolic_link_path"

Now it is workign fine.

Upvotes: 0

xdazz
xdazz

Reputation: 160883

That is not the problem of File.exists?, you just create a wrong symbolic link (with wrong link path).

Use the absolute path:

ln -nfs `pwd`/shared/test.xls current/tmp/test.xls

Or use the relative path:

ln -nfs ../../shared/test.xls current/tmp/test.xls

Then try again.

Upvotes: 1

Related Questions