Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a file exists in the user's home directory

How would I, say, determine if the file ~/.my_proj_config exists on any OS in Ruby?

like image 858
RyanScottLewis Avatar asked Apr 29 '11 11:04

RyanScottLewis


People also ask

How do you check if a file exists in a specific folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists. Noe that this answer returns false if the user does not have permission to read the file. So it does more than just checkinf if the file exists in a folder.

How do you check if a file exists in a directory in Linux?

In Linux everything is a file. You can use the test command followed by the operator -f to test if a file exists and if it's a regular file. In the same way the test command followed by the operator -d allows to test if a file exists and if it's a directory.

How do you check if a file exists in a directory Bash?

In order to check if a directory exists in Bash using shorter forms, specify the “-d” option in brackets and append the command that you want to run if it succeeds. [[ -d <directory> ]] && echo "This directory exists!" [ -d <directory> ] && echo "This directory exists!"


1 Answers

A call to Dir.home is a OS independent way to get to the home directory for the user. You can then use it like

File.exists?(File.join(Dir.home, ".my_proj_config"))
like image 146
Rob Di Marco Avatar answered Sep 28 '22 07:09

Rob Di Marco