-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
filebytype
executable file
·102 lines (93 loc) · 2.92 KB
/
filebytype
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/perl
# abstract: move files to dirs compelling to actual file-type
=zup?
12-a.mp3 => audio/mpeg
12-e.mp3 => audio/mpeg
12-j.mp3 => audio/mpeg
12-s.mp3 => audio/mpeg
a.out => application/x-executable
.bashrc => text/plain
bubble.pl => text/plain
cobalt.pl => text/plain
fileident.pl => text/plain
filemagic.pl => text/x-perl
.fonts.conf => application/xml
foobar => text/plain
foo.c => text/x-c
foo.sh => text/x-shellscript
.gtkrc-2.0 => text/plain
menu.pl => text/plain
menu.pl.tar => application/x-tar
menu.pl.tar.gz => application/x-gzip
menu.png => image/png
.muttrc => text/plain
.procmailrc => text/plain
.ratpoisonrc => text/x-c++; charset=us-ascii
.rtorrent.rc => text/plain
.sbclrc => text/plain
.screenrc => text/plain
.screenrc-dvdc => text/plain
.stumpwmrc => text/x-lisp
.toprc => text/plain
.urlview => text/plain
.vimperatorrc => text/plain
.vimrc => text/plain
.xinitrc => text/plain
.Xresources => text/x-c
0 files were skipped.
=cut
use strict;
use File::LibMagic;
use File::Copy;
use File::Path qw(make_path);
use File::Basename;
use Getopt::Long;
use Data::Dumper;
use Carp;
use Cwd 'abs_path';
my $magic = File::LibMagic->new;
our($opt_symlink,$opt_copy,$opt_move);
GetOptions(
'symlink|ln' => \$opt_symlink,
'copy|cp' => \$opt_copy,
'move|mv' => \$opt_move,
);
my @files = @ARGV or yell("I need files!") and die;
checkft(@files);
sub checkft {
my @files = @_;
my $unwanted = 0;
for my $file(@files) {
my $ft = $magic->checktype_filename($file);
$ft =~ s/([a-z\/-]+);.+/$1/;
if($ft eq 'appliaction/x-directory') {
$unwanted++;
next;
}
if(!-d $ft) {
if(!$opt_symlink and !$opt_copy and !$opt_move) {
print "make_path($ft)\n";
next;
}
# if the mimetype does not exist as a possibly arbitary level of subdirs,
# We'll have to create it (mostly ./foo/bar)
make_path($ft);
}
if($opt_symlink) {
symlink(abs_path($file), "$ft/$file") or die $!;
print "symlink($file, $ft/$file)\n";
}
elsif($opt_move) {
move($file, $ft);
print "move($file, $ft)\n";
}
elsif($opt_copy) {
copy($file, $ft) or die $!;
print "copy($file, $ft)\n";
}
}
}
sub yell {
my $msg = shift;
print "\033[1m$msg\033[0m\n";
}