List files

Hi,

which command can we use for list files (shell script) in Julia Terminal Windows?

Best!

readdir

 readdir()
8-element Array{String,1}:
 ".gitignore"
 ".travis.yml"
 "docs"
 "LICENSE"
 "README.md"
 "REQUIRE"
 "src"
 "test"

I cannot find the name of folder and more infos.

I recommend you to read https://docs.julialang.org/en/latest/base/file/ part of the Julia manual.

I understand you want something like:

print("Current directory: ", pwd())
foreach(readdir()) do f
       println("\nObject: ", f)
       dump(stat(f)) # you can customize what you want to print
end

You can get the name of the folder you are currently working in with

pwd() 

in ubuntu terminal, if we use “ls” command in a folder , it shows all names of file and sub-folders in this folder.

Run:

run(`ls`)

or

read(`ls`, String)

to fetch the output

src and test are subfolders.

I have a problem with this. I have a folder with files named as “file_1”, “file_2” and so on until “file_1000”. When I use readdir i get a list of all the files in the order:
["file_1", "file_10", "file_100",..., "file_1000"]
whereas I want them in the ascendant normal order:
["file_1", "file_2", "file_3",..., "file_1000"]
Is there another way to read the files in a directory that gives a list like the one I want?

julia> ls = ["file_1", "file_2", "file_10", "file_3"]
4-element Array{String,1}:
 "file_1" 
 "file_2" 
 "file_10"
 "file_3" 

julia> sort(ls, by=x->parse(Int, match(r"[0-9].*", x).match))
4-element Array{String,1}:
 "file_1" 
 "file_2" 
 "file_3" 
 "file_10"

btw, next time use ‘```’ for better readability.

Check out this link

If you are generating the files and you can choose a file name my suggestion is to pad the number with zeros. The following function helps with that:

numstring(x, n=3) = string(x + 10^n)[2:end]

So in your case,

julia> fnames = "file_" .*  numstring.(1:13, 3)
13-element Array{String,1}:
 "file_001"
 "file_002"
 "file_003"
 "file_004"
 "file_005"
 "file_006"
 "file_007"
 "file_008"
 "file_009"
 "file_010"
 "file_011"
 "file_012"
 "file_013"

In the link above, Palli suggests the following procedure:

julia> split(read(`ls -1v`, String))
19-element Array{SubString{String},1}:
 "file_1" 
 "file_2" 
 "file_3" 
 "file_4" 
 "file_5" 
 "file_6" 
 "file_7" 
 "file_8" 
 "file_9" 
 "file_10"
 "file_11"
 "file_12"
 "file_13"
 "file_14"
 "file_15"
 "file_16"
 "file_18"
 "file_20"
 "file_25"

Here’s a compact way to list files and directories separately:

Print files,then directories

dir = readdir()
println(‘\n’,“Files: \n”, [elm for elm in dir if !isdir(elm)])
println(‘\n’,“Directories: \n”, [elm for elm in dir if isdir(elm)])

You could also do:

d = readdir();
d[isdir.(d)]
d[.!isdir.(d)]