Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

perl help/excel/delete

SOLVED
Go to solution
Shaf_1
Advisor

perl help/excel/delete

I have over 4000k .pdf files that I need to filter for the right type (i.e. Personal, Business, Notes, Report). I have an excel file that lists all the file names and the associated type. What I am trying to do is get all the files that are type ‘Notes’ and ‘Personal’ and delete the others. Once I have types ‘Notes’ and ‘Personal’ from that bunch I want to randomly select (and cut) 1000 files and put them in a new folder. Can this be done using perl?
19 REPLIES
Steven E. Protter
Exalted Contributor
Solution

Re: perl help/excel/delete

use cut and paste and vi to create a simple filelist, one line per file.

put the file inot Linux(we are in linuxland).

while read -r filename
do
rm -f $filename
done < filelist

That will nuke them all and fast.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Shaf_1
Advisor

Re: perl help/excel/delete

Do I have to assign the file name path? I am guessing this is a shell script that you told me to write.
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

Sep said to cut and paste the filenames fro your excel sheet and create a file with it then create the script he posted and let it run in the same dir as your filelist. If you do only have relative filenames than you will have to put both files at the top of your filenames path.

greetings,

Michael
Shaf_1
Advisor

Re: perl help/excel/delete

I tried using the script you told me however I keep getting an error. The error is:

"while: Expression Syntax".

The code I am using is:

#!/bin/csh
while read -r newset
do
rm -f $newset
done < filelist


What am I doing wrong?

Stuart Browne
Honored Contributor

Re: perl help/excel/delete

Steven's example was a bash/sh/ksh example, not a csh example. the language is different.

Change the #!/bin/csh to just #!/bin/sh
One long-haired git at your service...
Shaf_1
Advisor

Re: perl help/excel/delete

That worked just great.

I have another question. From my new list of files I want to select 1000 random files.
How would I select the 1000 random files and delete them from the current folder and put them in another folder?
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

what do mean by random?
You can select with CTRL+mouseclick and number of files in your excel sheet, create the filelist and use mv instead of rm in the script.

greetings,

Michael
Shaf_1
Advisor

Re: perl help/excel/delete

I wanted to know if there is a way to select the files at random by having the computer select the files. I have 1000 files I need to select.
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

from what source and according to what criteria is the computer to select the files?

greetings,

Michael
Shaf_1
Advisor

Re: perl help/excel/delete

The files are stored in a folder on my hard drive. I just need to select the 1000 files with no duplicates. No other criteria.
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

you look for something like:
#!/bin/ksh
cd /tmp
for FILE in `ls | head -1000`
do
mv ${FILE} /pathto
done
?

greetings,

Michael
Shaf_1
Advisor

Re: perl help/excel/delete

Hello,

The program is not selecting 1000 random files. It is selecting the files in order. I am not sure why that is happening. I have over 3000 files and I need to select 1000 random files out of the 3000. Any ideas?

Thanks
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

I've attached a quick perl script.

$ random.pl

should return files in random order from directory .
Shaf_1
Advisor

Re: perl help/excel/delete

I tried using the script but I have a couple of questions. Here is your script with the information you told me to enter. I have a test folder set up with a total of 50 files for testing. I want to extract 20 random files from the 50 and mv them to another folder.

Did I enter the information correctly?

Can the files be moved from one folder to another?

Thanks



#!/usr/bin/perl

if (scalar(@ARGV) != 2) {
die "usage: $0 ~/documents/erule/test/test2 20\n";
}

@files = <$ARGV[0]/*>;
fisher_yates_shuffle(\@files);
$, = "\n";
print splice(@files, 0, $ARGV[1]);
print "\n";

sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

The script will return the names of 20 random files. If you want to move them to a different directory do the following

$ ./random.pl ~/documents/erule/test/test2 20 | while read file
do
echo mv $i
done

If printout is ok remove 'echo' from the last second line (before mv).
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

Oops, 'mv $i' should be 'mv $file'

$ ./random.pl ~/documents/erule/test/test2 20 | while read file
do
echo mv $file
done
Shaf_1
Advisor

Re: perl help/excel/delete

The first problem I am having is that the first script does not return any file names. I only get a line saying 'usage: randomfl.pl ~/documents/erule/test 20'. How will this code return my file names?

#!/usr/bin/perl

if (scalar(@ARGV) != 2) {
die "usage: $0 ~/documents/erule/test 20\n";
}

@files = <$ARGV[0]/*>;
fisher_yates_shuffle(\@files);
$, = "\n";
print splice(@files, 0, $ARGV[1]);
print "\n";

sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}

My second problem is with the second script you told me to use. It is giving me errors. I am sure I am doing something wrong. Here is the script I used for that.

#!/usr/bin/perl
$ ./randomfl.pl ~/documents/erule/test 20 | while read file
do
echo mv $file ~/documents/erule/test/test2
done

What mistake am I making?
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

the second script is actually a shell script and the $ was the prompt from the shell. Replace /usr/bin/perl with /usr/bin/sh
That should make it work.

greetings,

Michael
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

You don't need to make any modifications to the script. On the command line type

./randomfl.pl ~/documents/erule/test 20

That should return list of 20 randomly selected files.

I assume that you are running this on Linux.