Use perl to port ls commands in linux to windows

Source: Internet
Author: User
Tags print format
Use perl to port ls commands in linux to windows-Linux general technology-Linux programming and kernel information. For more information, see the following. File: ls.rar

#! /Usr/bin/perl

Use strict;
# Use Win32: File qw (GetAttributes );

My $ dir = $ ARGV [1];
My $ count = 0;
My @ stack = ();
My @ ref = ();
My @ size = ();
My @ time = ();
My $ size;
My $ datestr;
My $ year;
My $ mon;
My $ day;
My $ hh;
My $ mm;
My $ ss;





If (defined (my $ opt = $ ARGV [0]) {
If ($ opt !~ /^ \-/){
Print "you shoshould add'-'before your argment. \ n"
} Else {
$ Opt = ~ S/^ \-// o;
Last if $ opt eq '-';

If ($ opt eq '-help '){
& Usage;
Exit;
}

If ($ opt eq '-version '){
& Ver;
Exit;
}

If ($ opt = ~ /[A-z]/) {
& A if $ opt eq 'a ';
& D if $ opt eq 'd ';
& L if $ opt eq 'l ';
& R if $ opt eq 'R ';
& S if $ opt eq's ';
& Re if $ opt eq 'E ';
& Sz if $ opt eq 'C ';
& St if $ opt eq 'T ';
& Lsf if $ opt eq 'F ';
& S if $ opt = ~ /\ W {2 ,}/;
# & T if $ opt eq 'T ';
} Elsif ($ opt = ~ /[0-9] {0 ,}/){
# Deal with like: ls-1000
& Dir;
While (<*> ){
$ Count ++;
My $ stp = & size ($ _);
My $ size = & tpe ($ stp );
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
& Fmt ($ file, $ size, $ date );
If ($ count> $ opt-1 ){
Last;
}
}
}
}
} Else {
& L;
}


Sub usage {
Print <
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default ).


Mandatory arguments to long options are mandatory for short options too.
-A, list all.
-D, list directory.
-N, like-l, but list numer of input
-R, reverse order while sorting
-L, use a long listing format
-R, reverse order while sorting
-E, list subdirectories recursively
-S, sort list.
-C, list by size.
-T, list by time.
-- Help display this help and exit
-- Version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, M 1024*1024
* 1024.
Report bugs
EOF
}

Sub ver {
Print < Ls: 0.0.1
Written by irror .

Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
Warranty; not even for MERCHANTABILITY or fitness for a particle PURPOSE.
EOF
}

Sub dir {
If (defined ($ dir )){
Chdir ("$ dir ");
} Else {
Chdir (".");
}
}

Sub {
& Dir;
While (<*> ){
If (-d $ _){
Print "directory: |==>$ _". "\ n ";
} Else {
Print "file: | --- <$ _". "\ n ";
}
}
}

Sub d {
& Dir;
While (<*> ){
If (-d $ _){
Print "directory: |==>$ _". "\ n ";
}
}
}

##########-L options #################
Sub l {
& Dir;
While (<*> ){
My $ stp = & size ($ _);
My $ size = & tpe ($ stp );
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
& Fmt ($ file, $ size, $ date );
}
}

########## Output print format #############
Sub fmt {
My ($ file, $ size, $ date) = @_;
Write ();
Format STDOUT_TOP =
Filename filesize (B/K/M/G) filedate
---------------------------------------------------------------------
.
Format STDOUT =
@ <<@ <
$ File, $ size, $ date
.
}

########### Reverse sort files #############
Sub r {
My @ tmp1 = ();
My @ tmp2 = ();
My $ cc;
& Dir;
While (<*> ){
If (-d $ _){
$ _ = ~ S; \ s ;?; G;
Push (@ tmp1, $ _);
} Else {
$ _ = ~ S; \ s ;?; G;
Push (@ tmp2, $ _);
}
}
My @ tp = sort ("@ tmp1 @ tmp2 ");
Foreach (reverse <@ tp> ){
$ _ = ~ S ;\?; ; G;
My $ stp = & size ($ _);
My $ size = & tpe ($ stp );
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
& Fmt ($ file, $ size, $ date );
}

}
############ Sort files ################
Sub s {
My @ tmp1 = ();
My @ tmp2 = ();
My $ cc;
& Dir;
While (<*> ){
If (-d $ _){
$ _ = ~ S; \ s ;?; G; # Replace spaces or tabs in special file names with spaces '? '? .
Push (@ tmp1, $ _);
} Else {
$ _ = ~ S; \ s ;?; G;
Push (@ tmp2, $ _);
}
}
My @ tp = sort ("@ tmp1 @ tmp2"); # @ tmp1 and @ tmp2 spaces are required, otherwise, the last element of @ tmp1 joins with the first element of @ tmp2 to form an element, causing the file to not exist.
Foreach (<@ tp> ){
$ _ = ~ S ;\?; ; G;
My $ stp = & size ($ _);
My $ size = & tpe ($ stp );
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
& Fmt ($ file, $ size, $ date );
}

}

############ File modification time ##############
Sub gmdate {
My $ rev = shift;
$ Datestr = (lstat $ rev) [9];
& Ttime ($ datestr );
}

############ Identify directories and files #############
Sub jdf {
My $ rr = shift;
My $ fe;
If (-d $ rr ){
$ Fe = 'd #=>'. $ rr;
} Else {
$ Fe = 'f #=< <'. $ rr;
}
Return $ fe;
}

############ Specify a proper suffix #################
Sub tpe {
My $ bb = shift;
My $ type;
If ($ bb <1048576 & $ bb> = 1024 ){
$ Type = (eval "$ bb/1024"). "K ";
} Elsif ($ bb >=1048576 & $ bb <1073741824 ){
$ Type = (eval "$ bb/(1024*1024)"). "M ";
} Elsif ($ bb >=1073741824 ){
$ Type = (eval "$ bb/(1024*1024*1024)"). "G ";
} Else {
$ Type = $ bb. "B ";
}
$ Type = ~ S; ^ (\ d + \. \ d {2}) (\ d +) ([B | K | M | G]); $1 $3; g; # $ type now is a array.
Scalar ($ type );
}

########## File size #################
Sub size {
My $ civ = shift;
My $ sze = (stat $ civ) [7];
}

######### Recursively print the file name #########
Sub re {
& Dir;
While (<*> ){
& Pr_f ($ _);
}
}

Sub pr_f {

If (-d ){
My $ tmp = '|-'. "$ _";
Push (@ stack, $ tmp );
Print join ('', @ stack)." \ n ";
Chdir $ _;
Foreach (<*> ){
& Pr_f ($ _);
}
Pop @ stack;
Chdir "..";
} Else {
Print join ('', @ stack)." |-$ _ "." \ n ";
}
}

#### Sort by file size ##############

Sub sz {
& Dir;
Open (TMP, '> c: \ ptmp') | die "can't create file: $! \ N ";
While (<*> ){
My $ size = & size ($ _);
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
$ File = ~ S; \ s ;?; G;
Print TMP $ file. ''. $ size.''. $ date. "\ n ";
}
Close (TMP );
Open (FH, 'c: \ ptmp ') | die "can't open file: $! \ N ";;
While ( ){
My @ row = split (/\ s +/, $ _);
My $ ref = [[$ row [0], [$ row [1], [[$ row [2], [$ row [3], [$ row [4];
Push (@ ref, $ ref );
}
Close (FH );
Foreach (@ ref ){
Push (@ size, $ _-> [1] [0]. '| '. $ _-> [0] [0]. '| '. $ _-> [2] [0] [0]. ''. $ _-> [2] [1] [0]. ''. $ _-> [2] [2] [0]);
}

For (my $ I = 0; $ I <@ size; $ I ++ ){
For (my $ j = $ I + 1; $ j <@ size; $ j ++ ){
My $ tmp1 = $ size [$ I];
My $ tmp2 = $ size [$ j];
$ Tmp1 = ~ S/(\ d +) [\ |]. */$1 /;
$ Tmp2 = ~ S/(\ d +) [\ |]. */$1 /;
If ($ tmp1> $ tmp2 ){
($ Size [$ I], $ size [$ j]) = ($ size [$ j], $ size [$ I]);
}
}
}
Foreach (@ size ){
$ _ = ~ /^ (\ D +) \ | (. *) \ | (. *) $ /;
My $ file = $2;
My $ size = & tpe ($1 );
My $ date = $3;
$ File = ~ S ;\?; ; G; # process objects whose names contain spaces.
& Fmt ($ file, $ size, $ date );
}
Unlink 'C: \ ptmp ';

}

######## Sort by file modification time ####################

Sub st {
& Dir;
Open (TMP, '> c: \ ptmp') | die "can't create file: $! \ N ";
While (<*> ){
My $ size = & size ($ _);
My $ date = (stat $ _) [9];
My $ file = & jdf ($ _);
$ File = ~ S; \ s ;?; G;
Print TMP $ file. ''. $ size.''. $ date. "\ n ";
}
Close (TMP );

Open (FH, 'c: \ ptmp ') | die "can't open file: $! \ N ";;
While ( ){
My @ row = split (/\ s +/, $ _);
My $ ref = [[$ row [0], [$ row [1], [$ row [2];
Push (@ ref, $ ref );
}
Close (FH );
Foreach (@ ref ){
Push (@ time, $ _-> [1] [0]. '| '. $ _-> [0] [0]. '| '. $ _-> [2] [0]);
}

For (my $ I = 0; $ I <@ time; $ I ++ ){
For (my $ j = $ I + 1; $ j <@ time; $ j ++ ){
My ($ tmp1, $ tmp2) = ($ time [$ I], $ time [$ j]);
$ Tmp1 = ~ S/(\ d +) [\ |]. * [\ |] (. *)/$2 /;
$ Tmp2 = ~ S/(\ d +) [\ |]. * [\ |] (. *)/$2 /;
If ($ tmp1> $ tmp2 ){
($ Time [$ I], $ time [$ j]) = ($ time [$ j], $ time [$ I]);
}
}
}
Foreach (@ time ){
$ _ = ~ /^ (\ D +) \ | (. *) \ | (. *) $ /;
My $ file = $2;
My $ size = & tpe ($1 );
My $ date = & ttime ($3 );
$ File = ~ S ;\?; ; G;
& Fmt ($ file, $ size, $ date );
}
Unlink 'C: \ ptmp' | die "delete file failed: $! \ N ";


}

########### For need #############
Sub ttime {
My $ ttime = shift;
$ Year = (gmtime $ ttime) [5] + 1900;
$ Mon = (gmtime $ ttime) [4] + 1;
$ Day = (gmtime $ ttime) [3];
$ Hh = (gmtime $ ttime) [2];
$ Mm = sprintf ("% 02d", (gmtime $ ttime) [1]);
$ Ss = sprintf ("% 02d", (gmtime $ ttime) [0]);
Return $ year. ''." $ mon \/$ day ".''. "$ hh: $ mm: $ ss ";
}

########## List file attributes ####################
Sub lsf {
My $ stp = & size (<$ dir> );
My $ size = & tpe ($ stp );
My $ date = & gmdate ($ _);
My $ file = & jdf ($ _);
& Fmt ($ file, $ size, $ date );
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.