Desktop productivity for business analysts and programmers

deleting files using linux command

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 318
Accepted Solution

deleting files using linux command

Hello friends,

 

i have below programs that i created to delete few selected entire directories from specific dir (on linux system via EG).

 

this program running fine however, my goal is delete all directories that i have in test2 dataset  (have like 15 dir under test) with "rm -rf" command but its just deleting one dir when i run it,

 

how can i tell SAS that perform all command (rm -rf) for all directory?

 

 

data test (keep=files);

length access $ 20 files $ 200;

infile "cd /test; ls -lt;" pipe firstobs=2;

input access $ nx $ user $ group $ size $ month $ day $ time $ files;

run;

 

/*creating rm -rf command*/

data test2;

set test;

rmcmd=catx(' ','rm -rf'

              ,cats('/test/',files);

run;

 

 

/*rmcmd variable looks like this*/

 

rmcmd 

rm -rf sdfkjqfjnsvv

rm -rf skdfjspiu

rm -rf soivjasivuhqsvjnsvkjnsv

rm -rf sdlvjasvhqqdsdkvjbqsdvhb

 

 

/*trying to execute rm -rf commanf for all*/

data _null_;

set test2;

infile cmd pipe filevar=rmcmd;

input@;

run;

 

 

Thanks a lot...!!!


Accepted Solutions
Solution
‎02-13-2017 05:14 PM
Super User
Super User
Posts: 6,310

Re: deleting files using linux command

The easiest change is to use the FILEVAR option on the INFILE statement to execute the commands you have generated.

 

data test2;
  set test;
  rmcmd=catx(' ','rm -rf',cats('/test/',files));
run;

data _null_;
  set test2 ;
  infile cmd pipe filevar=rmcmd end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;

View solution in original post


All Replies
Grand Advisor
Posts: 10,196

Re: deleting files using linux command

You might try the FDELETE function. You would not send the unix command just the name of the file and/or directory:

 

data _null_;
   set test2;
   rc=fdelete(fname);
   put rc=;
   msg=sysmsg();
   put msg=;
run;

where Fname above is just the name of the directory. The PUT statements show the results.

 

Super Contributor
Super Contributor
Posts: 318

Re: deleting files using linux command

dir name I have to remove is bigger than 8 char and that's why may be I am receiving this error,


NOTE: In a call to the FDELETE routine, the fileref rm -rf
/test/sdfkjqfjnsvv exceeds 8 characters, and will be truncated.
rc=20004
msg=ERROR: No logical assign for filename RM.
Grand Advisor
Posts: 10,196

Re: deleting files using linux command

the length for any character type variable needs to be set long enough to hand the explected values. For the FDELETE you do not want the "rm -rf" as part of the name string as that is not the actual file or directory name.

 

Another approach, would be the X or call system statements, IF you have permissions to run X. Since I'm not running on unix I can't really test what you may be attempting to provide more direct examples.

Something like this perhaps:

data _null_;
   set test2;
   call system(rmcmd);
run;

 

 

Your pipe failing because INFILE is not really an executeable, as in it isnot called for each record in test2.

Solution
‎02-13-2017 05:14 PM
Super User
Super User
Posts: 6,310

Re: deleting files using linux command

The easiest change is to use the FILEVAR option on the INFILE statement to execute the commands you have generated.

 

data test2;
  set test;
  rmcmd=catx(' ','rm -rf',cats('/test/',files));
run;

data _null_;
  set test2 ;
  infile cmd pipe filevar=rmcmd end=eof;
  do while (not eof);
    input;
    put _infile_;
  end;
run;
Super Contributor
Super Contributor
Posts: 318

Re: deleting files using linux command

tried same Tom Smiley Happy - worked fine - thanks a lot...

needed that end=eof + do while statement

Esteemed Advisor
Posts: 6,636

Re: deleting files using linux command

Why not simply use call system to execute the rm commands?

data _null_;
set test2;
call system(rmcmd);
run;

I'd suggest to use absolute path names for the files to be removed.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 9,567

Re: deleting files using linux command


I would like to make a shell file to contain all these command, and execute once for all.

data _null_;
file '/home/temp.sh';
put "rm -rf sdfkjqfjnsvv";
put "rm -rf skdfjspiu";
put "rm -rf soivjasivuhqsvjnsvkjnsv";
put "rm -rf sdlvjasvhqqdsdkvjbqsdvhb";
run;

x "./home/temp.sh";


☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 211 views
  • 2 likes
  • 5 in conversation