Forum

> > CS2D > Scripts > Command for directory listing
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Command for directory listing

17 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Command for directory listing

mozilla1
User Off Offline

Zitieren
Hello guys, i have a problem with the bash (linux command prompt). I made a function that gives me a lua-table list of contents and in a directory in windows-like systems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function read_dir(dir,readFiles,readFolders,showExtension)
	local dir_folders={}
	local dir_files={}
	
	if readFiles then
		os.execute('dir /b /a:-d "'..dir..'" > tmp')
		for line in io.lines("tmp") do
			if showExtension then
				table.insert(dir_files,line)
			else				
				local file=string_split(line,"[^.]*")
				local ext=table.remove(name,#name)
				local name=table.concat(file,".")
				table.insert(dir_files,name)
			end
		end
	end
	if readFolders then
		os.execute('dir /b /a:d "'..dir..'" > tmp')
		for line in io.lines("tmp") do
			table.insert(dir_folders,line)
		end
	end
	os.remove("tmp")
	return dir_files,dir_folders
end

As you can see, the function throws 'dir /b /a:-d "'..dir..' (files) and 'dir /b /a:d "'..dir..'(directory) to the DOS, but it doesn't work with bash.

I'm not well familiarized with the command "ls", so if someone here is a linux user and wanna help me, I would appreciate it.

alt Re: Command for directory listing

Avo
User Off Offline

Zitieren
Here you are Starkkz's functions (that I've found on the forum) reading all files of the given directory.
Linux >

Win >

I don't know if they works.

alt Re: Command for directory listing

MikuAuahDark
User Off Offline

Zitieren
Why someone never use io.popen?
(Windows only)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FileName=35	-- It shows correct filename list on me. If it shows a "space" then set it to 40

function Scandir(dir)
	local temp=io.popen("dir \""..dir.."\"")
	local lnum=0
	local temp_table={}
	for line in temp:lines() do
		lnum=lnum+1
		if lnum>=8 and line:sub(1,1)~=" " then
			local ok={line:sub(FileName),false}
			if line:find("<DIR>") then
				ok[2]=true
			end
			table.insert(temp_table,ok)
		end
	end
	temp:close()
	return temp_table
end

Should work.

alt Re: Command for directory listing

EngiN33R
Moderator Off Offline

Zitieren
user MikuAuahDark hat geschrieben
Why someone never use io.popen?


You answered your own question - because
user MikuAuahDark hat geschrieben
(Windows only)


Considering most servers run Linux, it's a limitation. And considering the OP said "I have a problem with bash (linux command prompt)", your post isn't of much help.

alt Re: Command for directory listing

MikuAuahDark
User Off Offline

Zitieren
user EngiN33R hat geschrieben
Considering most servers run Linux, it's a limitation. And considering the OP said "I have a problem with bash (linux command prompt)", your post isn't of much help.


okay, now i have one question.

Does ls -a1 produce same output like dir?

alt Re: Command for directory listing

KimKat
GAME BANNED Off Offline

Zitieren
A great way to create your own type of command aliases is by doing this.
alias listing="ls -a"

That way you'd be able to create custom commands to do whatever you like, it's like a swizz army knife. You can change existing commands to do other things aswell. Use with caution though. Good luck.

alt Re: Command for directory listing

mozilla1
User Off Offline

Zitieren
So, you guys are ensuring that the command "ls -a" will produce a pure directory list, only with file names? If so, thanks for the helping, I'm gonna testing this on my VPS.

Please moderators don't close this topic until I achieve what I want here.

alt Re: Command for directory listing

MikuAuahDark
User Off Offline

Zitieren
Based from Wikipedia, this is the script to list all directories at linux

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Scandir(dir,scanf)
	local temp={}
	if os.execute("ls -FA \""..dir.."\" > temp.txt")==1 then
		local t=assert(io.open("temp.txt","r"))
		local a=t:read("*a")
		t:close()
		os.remove("temp.txt")
		return nil,a
	end
	if dir:sub(-1)~="/" then
		dir=dir.."/"
	end
	local t=assert(io.open("temp.txt","r"))
	for line in t:lines() do
		if line:sub(-1)=="/" then
			if scanf then
				table.insert(temp,{line,Scandir(dir..line,true)})
			else
				table.insert(temp,line)
			end
		else
			if scanf then
				if line:sub(-1)=="*" then
					table.insert(temp,line:sub(1,-2))
				else
					table.insert(temp,line)
				end
			else
				if line:sub(-1)=="*" then
					table.insert(temp,{line:sub(1,-2),false})
				else
					table.insert(temp,{line,false})
				end
			end
		end
	end
	t:close()
	os.remove("temp.txt")
	return temp
end
Untested because i am using windows

alt Nice filters.

KimKat
GAME BANNED Off Offline

Zitieren
I looked up the help section of ls, find and grep and managed to write a nice script which filters input and gives you proper output. You could then simply parse this further via Lua by just assigning the output to variable. Enjoy!
function Exe(c) -- Execute commands
	os.execute(c)
end

Exe("mkdir dir1 && mkdir dir2") -- Makes two directories within same directory.
print(Exe('ls -oxUSA1')) -- List all types of files.
print(Exe('find -maxdepth 1 -type d | grep "./" | cut -d"/" -f2')) -- List directories only.
print(Exe('find -maxdepth 1 -type f | grep "./" | cut -d"/" -f2')) -- List files only.

Output >

alt Re: Command for directory listing

mozilla1
User Off Offline

Zitieren
user KimKat hat geschrieben
I looked up the help section of ls, find and grep and managed to write a nice script which filters input and gives you proper output. You could then simply parse this further via Lua by just assigning the output to variable. Enjoy!
function Exe(c) -- Execute commands
	os.execute(c)
end

Exe("mkdir dir1 && mkdir dir2") -- Makes two directories within same directory.
print(Exe('ls -oxUSA1')) -- List all types of files.
print(Exe('find -maxdepth 1 -type d | grep "./" | cut -d"/" -f2')) -- List directories only.
print(Exe('find -maxdepth 1 -type f | grep "./" | cut -d"/" -f2')) -- List files only.

Output >


Thanks, this is exactly what I was looking for. You can close this topic now, @user KimKat:.

alt Re: Command for directory listing

Livia
User Off Offline

Zitieren
The function I use >

Output >


Some useful manual pages:
http://unixhelp.ed.ac.uk/CGI/man-cgi?ls
http://unixhelp.ed.ac.uk/CGI/man-cgi?find

Edit:
In order to list only folders, add | egrep "/$" before writing the output to tmp. Parameter -v inverts this operation.

alt Re: Command for directory listing

KimKat
GAME BANNED Off Offline

Zitieren
Yea let's keep it open and we're accepting more commands which are shorter also. Remember there's all kinds of ways to shorten commands.

Thanks to some tips from user Livia I managed to do this in a much shorter way.
function Exe(c)os.execute(c);end
print(Exe('ls -d */ | cut -d"/" -f1')) -- List directories only.
print(Exe('ls -F | grep -v "/"')) -- List files only (By: user:4917)
print(Exe('ls -F | grep -v "/" | cut -d"." -f1-2')) -- List files only (with or without extension via [f1-2]), can be troublesome with multiple file extensions but is kind of neat.
3× editiert, zuletzt 03.10.13 23:50:30
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht