BookmarkSubscribeRSS Feed

List of Directories and Files in Linux

Started ‎08-19-2019 by
Modified ‎08-19-2019 by
Views 7,099

This generates a dataset containing all the files and directories in the selected Linux path.

 

Fields derived include dir, file_mode, number_of_links, owner_name, group_name, bytes, date_last_modified, time_modified, file_name.

As always, YMMV.

 

 


%let directory=//mnt/nfs/;

filename tmp pipe "ls -mlR --full-time -1 &directory";

data want12;
	infile tmp dlm="¬";
	length dir_line $2000;
	input dir_line;
run;

proc sql noprint;
	select max(countw(dir_line, ' ')) into :maxwords trimmed from want12;
quit;

data want;
	set want12;
	format dir $200.;
	retain dir;
	words = countw(dir_line, ' ');

	if find(dir_line,"/") then
		dir=dir_line;
	array parsed_vars(*) $200 new_var1-new_var&maxwords;
	i=1;

	do while(i<words or scan(dir_line, i, " ") ne "" or i=1);
		if find(dir_line,"/")=0 and scan(dir_line, i, " ") ne ""  and i<>1 then
			parsed_vars(i) =scan(dir_line, i, " ");
		else if i=1 then parsed_vars(i)=dir_line;
		i+1;
	end;

	if not find(dir_line,"/") and words>5 then
		do;
			file_mode=new_var1;
			number_of_links=new_var2;
			owner_name=new_var3;
			group_name=new_var4;
			bytes=input(new_var5,comma12.);
			date_last_modified=new_var6;
			time_modified=new_var7;
			
			if length(scan(dir_line, 8, " "))>0 
				and substr(dir_line,1,1)='-' 
					then file_name=substr(dir_line,
					find(dir_line,scan(dir_line, 8, " "))
					+length(scan(dir_line, 8, " "))+1,
					length(dir_line)-find(dir_line,scan(dir_line, 8, " ")));
		end;
run;

 

 

 

 

Comments

Just a quick note that this requires XCMD to be enabled. You can verify if XCMD is enabled using:

 

proc options option=xcmd;
run;

 

If the result in the log is NOXCMD, it is not enabled.

You're welcome.

Here's another approach that is OS independent and doesn't require XCMD:

 

/* import library */
filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
%inc mc;

/* list files */
%mp_dirlist(path=/some/location,outds=myTable);

 

 

 

Except it's missing all the fields that I put mine together for: size, date, ownership.  And doesn't show files in folders.

This is great, Reeza, thanks for sharing. Q: Is there an efficient way to limit the output to the person using this tool?

@tomrvincent - the macro has been upgraded since then.  Use the 'GETATTRS' argument to get size/date/ownership (doesn't have nested subfolders though)

%mp_dirlist(path=/tmp,outds=myTable, getattrs=YES)

 

docs:  https://core.sasjs.io/mp__dirlist_8sas.html

 

AllanBowe_0-1634154884737.png

 

@AllanBowe , thank you so much for the macro %mp_dirlist!

Version history
Last update:
‎08-19-2019 11:49 AM
Updated by:
Contributors

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags