One-liner to recursively list directories in Ruby?
Dir.glob("**/*/") # for directories
Dir.glob("**/*") # for all files
Instead of Dir.glob(foo)
you can also write Dir[foo]
(however Dir.glob
can also take a block, in which case it will yield each path instead of creating an array).
Ruby Glob Docs
Find directory path up in hierarchy recursively on Ruby
Dir.glob(Pathname('/root').join('**/**/dirPathIWantToFind'))
See Dir.glob. Be warned though - this can take quite a bit of time depending on the size of your file system.
Iterate over directories and subdirectories recursively showing 'path/file' in ruby
Basically the reason is that you are running File.basename
which gives you the 'basename' of the File and not the relative path.
Additionally the .glob("**/*")
also includes directories and as such you need to take that into account.
This is how I would do it...
Dir.glob("**/*").each do |file|
next if File.directory?(file) # skip the loop if the file is a directory
puts file
output = `git log -1 -r -n 1 --pretty=format:"%cd [%h]" -- #{file}`
puts output
end
Let me know if you want me to explain any line in the above code...
ruby: copy directories recursively with link dereferencing
Here's my implementation of find -follow
in ruby:
https://gist.github.com/akostadinov/05c2a976dc16ffee9cac
I could have isolated it into a class or monkey patch Find but I decided to do it as a self-contained method. There might be room for improvement because it doesn't work with jruby. If anybody has an idea, it will be welcome.
Update: found out why not working with jruby - https://github.com/jruby/jruby/issues/1895I'll try to workaround. I implemented a workaround.
Update 2: now cp_r_dereference
method ready - https://gist.github.com/akostadinov/fc688feba7669a4eb784
find a file in a nested directory structure
You can use Dir.glob, for example:
Dir.glob(File.join("**","*.rb"))
It will recursively look for "*.rb" files in your current directory.
Get names of all files from a folder with Ruby
You also have the shortcut option of
Dir["/path/to/search/*"]
and if you want to find all Ruby files in any folder or sub-folder:
Dir["/path/to/search/**/*.rb"]
How to recursively change file permissions only in a ruby script
You could do something like below - this will change permissions of the list of files matched by Dir.glob
.
FileUtils.chmod 0400, Dir.glob('/path/to/dir/**/*')
As mentioned in this thread,
Dir.glob("**/*/") # will return list of all directories
Dir.glob("**/*") # will return list of all files
Ruby: how do I recursively find and remove empty directories?
In ruby:
Dir['**/*'] \
.select { |d| File.directory? d } \
.select { |d| (Dir.entries(d) - %w[ . .. ]).empty? } \
.each { |d| Dir.rmdir d }
Related Topics
How to Use Bundler Behind a Proxy
Ruby Regular Expression to Match a Url
Iterate Through Every File in One Directory
Undefined Method 'Visit' When Using Rspec and Capybara in Rails
Convert Array of 2-Element Arrays into a Hash, Where Duplicate Keys Append Additional Values
Getting a "Bad Interpreter" Error When Using Brew
How to Update Ruby to 1.9.X on Mac
Rufus Scheduler Not Running in Production
How to Dynamically Call a Math Operator in Ruby
Counter_Cache With Has_Many :Through
Read Binary File as String in Ruby
Devise Custom Routes and Login Pages
Ruby Gem For Finding Timezone of Location