forked from kurokirasama/nushell_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.nu
More file actions
860 lines (750 loc) · 20.5 KB
/
Copy pathfiles.nu
File metadata and controls
860 lines (750 loc) · 20.5 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
use string_manipulation.nu *
######################################################
# here because they are needed in this file
######################################################
#generate error output
export def return-error [msg] {
error make -u {msg: $"(echo-r $msg)"}
}
#green echo
export def echo-g [text:string] {
echo $"(ansi -e { fg: '#00ff00' attr: b })($text)(ansi reset)"
}
#red echo
export def echo-r [text:string] {
echo $"(ansi -e { fg: '#ff0000' attr: b })($text)(ansi reset)"
}
#yellow echo
export def echo-y [text:string] {
echo $"(ansi -e { fg: '#ffff00' attr: b })($text)(ansi reset)"
}
#custom color echo
export def echo-c [
text:string #text to print
color:string #color of text
--bold(-b) #print in bold
] {
if $bold {
echo $"(ansi -e { fg: ($color) attr: b })($text)(ansi reset)"
} else {
echo $"(ansi -e { fg: ($color)})($text)(ansi reset)"
}
}
#verify if a column exist within a table
export def is-column [name] {
$name in ($in | columns)
}
######################################################
######################################################
#wrapper for describe
export def typeof [
--full(-f)
--list-of-tables(-l) #for list of records that should have been a table
] {
let inp = $in
mut type = $inp | describe
if not $full {
$type = $inp | describe | split row '<' | get 0
}
if $list_of_tables {
if $type == "list" {
# Check if the list has column names, indicating it's effectively a table
if ($inp | first | describe) starts-with "record" {
return "table"
}
}
}
return $type
}
#open text file
export def op [
file? # filename
--raw(-r) # open in raw mode if using defaul open
--open(-o) # use default open
--sublime(-s) # use sublime text
--zed(-z) # use zed text editor
] {
let file = get-input $in $file -n
let extension = $file | path parse | get extension
if $open {
open $file
} else if $raw {
open --raw $file
} else if $sublime {
subl $file
} else if $zed {
zed $file
} else {
match $extension {
"md"|"Rmd" => {glow $file},
"nu" => {open --raw $file | nu-highlight | bat --paging auto},
"R"|"c"|"m"|"py"|"sh" => {bat --paging auto $file},
"csv"|"json"|"sqlite"|"xls"|"xlsx"|"tsv|toml" => {open $file},
"doc"|"docx"|"pdf"|"png"|"jpg" => {openf $file},
_ => {bat --paging auto $file}
}
}
}
#open file
export def --env openf [file?] {
let file = get-input $in $file
let file = (
match ($file | typeof) {
"record" => {
$file
| get name
| ansi strip
},
"table" => {
$file
| get name
| get 0
| ansi strip
},
_ => {$file}
}
)
if "GDK_PIXBUF_MODULE_FILE" in $env {
hide-env GDK_PIXBUF_MODULE_FILE
}
job spawn {xdg-open $file} | ignore
}
#open last file
export def openl [] {
lt | last | openf
}
#accumulate a list of files into the same table
#
#Example
#ls *.json | openm
#let list = ls *.json; openm $list
export def openm [
list? #list of files
] {
let list = get-input $in $list
$list
| get name
| reduce -f [] {|it, acc|
$acc | append (open ($it | path expand))
}
}
#send to printer
export def print-file [file?,--n_copies(-n):int] {
let file = get-input $in $file -n | ansi strip
match ($file | typeof) {
"string" => {
if ($n_copies | is-empty) {
lp $file
} else {
lp -n $n_copies $file
}
},
"list" => {
$file
| each {|name|
print-file $name
}
},
_ => {return-error $"($file | typeof) not allowed!"}
}
}
#compress all folders into a separate file and delete them
export def "7z folders" [
--not_delete(-n)
] {
if $not_delete {
bash -c "find . -maxdepth 1 -mindepth 1 -type d -print0 | parallel -0 --eta 7z a -t7z -bso0 -bsp0 -m0=lzma2 -mx=9 -ms=on -mmt=on {}.7z {}"
return
}
bash -c "find . -maxdepth 1 -mindepth 1 -type d -print0 | parallel -0 --eta 7z a -t7z -sdel -bso0 -bsp0 -m0=lzma2 -mx=9 -ms=on -mmt=on {}.7z {}"
}
#compress to 7z using max compression
#
# Example:
# compress all files in current directory and delete them
# 7z max filename * -d
# compress all files in current directory and split into pieces of 3Gb (b|k|m|g)
# 7z max filename * "-v3g"
# both
# 7z max filename * "-v3g -sdel"
export def --wrapped "7z max" [
filename: string #existing or not existing 7z filename
...rest #files to compress and extra flags for 7z
--delete(-d) #delete files after compression
] {
if ($rest | is-empty) {
return-error "no files to compress!!"
}
if $delete {
7z a -t7z -sdel -m0=lzma2 -mx=9 -ms=on -mmt=on $"($filename | path parse | get stem).7z" ...$rest
return
}
7z a -t7z -m0=lzma2 -mx=9 -ms=on -mmt=on $"($filename | path parse | get stem).7z" ...$rest
}
#rm trough pipe
#
#Example
#ls *.txt | first 5 | rm-pipe
export def rm-pipe [] {
let files = $in
if ($files | is-empty) {return "no files provided!"}
let files = if ($files | typeof) == "record" {
$files | transpose | transpose -r
} else {
$files
} | get name | ansi strip
let number = ($files | length) - 1
for i in 0..$number {
progress_bar ($i + 1) ($number + 1)
rm -rf ($files | get $i) | ignore
}
}
#cp trough pipe to same dir
#
#Example
#ls *.txt | first 5 | cp-pipe ~/temp
export def cp-pipe [
to: string #target directory
--force(-f) #force copy
] {
let files = $in
let files = if ($files | typeof) == "record" {
$files | transpose | transpose -r
} else {
$files
} | get name | ansi strip
let number = ($files | length) - 1
for i in 0..$number {
progress_bar ($i + 1) ($number + 1)
if $force {
cp -fr ($files | get $i) ($to | path expand)
continue
}
cp -ur ($files | get $i) ($to | path expand)
}
}
#mv trough pipe to same dir
#
#Example
#ls *.txt | first 5 | mv-pipe ~/temp
export def mv-pipe [
to: string #target directory
--force(-f) #force rewrite of file
] {
let files = $in
let files = if ($files | typeof) == "record" {
$files | transpose | transpose -r
} else {
$files
} | get name | ansi strip
let number = ($files | length) - 1
for i in 0..$number {
progress_bar ($i + 1) ($number + 1)
if $force {
mv -f ($files | get $i) ($to | path expand)
continue
}
mv -u ($files | get $i) ($to | path expand)
}
}
#ls by date (newer last)
export def lt [
--reverse(-r) #reverse order
] {
if ($reverse | is-empty) or (not $reverse) {
ls | sort-by modified
} else {
ls | sort-by modified -r
}
}
#ls in text grid
export def lg [
--date(-t) #sort by date
--reverse(-r) #reverse order
] {
match $date {
true => {
match $reverse {
true => {
ls | sort-by -r modified | grid -ci
},
false => {
ls | sort-by modified | grid -ci
}
}
},
false => {
match $reverse {
true => {
ls | sort-by -i -r type name | grid -ci
},
false => {
ls | sort-by -i type name | grid -ci
}
}
}
}
}
#ls sorted by name
export def l [--du(-d),--sort_by_size(-s)] {
if $du {
if $sort_by_size {
ls --du | sort-by -i type name | sort-by size
} else {
ls --du | sort-by -i type name
}
} else {
ls | sort-by -i type name
}
}
export def lc [] {
l | grid -ci
}
#ls only name
export def lo [] {
ls
| sort-by -i type name
| reject type size modified
}
#ls sorted by extension
export def le [] {
ls
| sort-by -i type name
| insert "ext" {|in|
$in.name
| path parse
| get extension
}
| sort-by ext
}
#get list of files recursively
export def get-files [
dir?
--recursive(-f)
--full_paths(-F)
--sort_by_date(-t)
] {
let dir = $dir | default "."
let pattern = if $recursive { "**/*" } else { "*" } | into glob
cd $dir
ls --full-paths=$full_paths $pattern
| where type == file
| if $sort_by_date {
sort-by -i modified
} else {
sort-by -i name
}
}
#find file in dir recursively
export def find-file [search,--directory(-d):string] {
if ($directory | is-empty) {
get-files -f | find -n $search
} else {
get-files $directory -f | find -n $search
}
}
#get list of directories in current path
export def get-dirs [dir?, --full(-f),--all(-a)] {
try {
if $all {
if ($dir | is-empty) {
if $full {
ls -a **/*
} else {
ls -a
}
| where type == dir
| sort-by -i name
} else {
ls -a $dir
| where type == dir
| sort-by -i name
}
} else {
if ($dir | is-empty) {
if $full {
ls **/*
} else {
ls
}
| where type == dir
| sort-by -i name
} else {
ls $dir
| where type == dir
| sort-by -i name
}
}
} catch {
{name: ""}
}
}
#join multiple pdfs
export def "pdf join" [
...rest: #list of pdf files to concatenate
] {
let rest = if ($rest | is-empty) {$in | get name} else {$rest}
if ($rest | is-empty) {
return-error "no pdf provided!"
}
pdftk ...$rest cat output output.pdf
print (echo-g "pdf merged in output.pdf")
}
#split a pdf by page
export def "pdf split" [
file?: #pdf file name
] {
let file = get-input $in $file -n
if ($file | is-empty) {
return-error "no pdf provided!"
}
pdftk $file burst
}
#create media database for downloads and all mounted disks
export def autolister [user?] {
let user = get-input $env.USER $user
let host = $env.HOST
print (echo-g "listing Downloads...")
cd ~/Downloads
lister ("Downloads" + "_" + $host)
let drives = sys disks | where mount like $"/media/($user)"
if ($drives | length) > 0 {
$drives
| get mount
| each { |drive|
print (echo-g $"listing ($drive)...")
cd $drive
lister ($drive | path parse | get stem | split row " " | str join _)
}
}
}
#list all files and save it to json in Dropbox/Directorios
export def lister [file:string] {
let file = (["~/Dropbox/Directorios" $"($file).json"] | path join | path expand)
let df = (try {
get-files -f -F
} catch {
[]
}
)
if ($df | length) == 0 {
if $file like "Downloads" and ($file | path expand | path exists) {
rm $file
}
return
}
let last = $df | reject name | update size {into int} | polars into-df
let df = (
$df
| each {|file|
$file
| get name
| parse $"{origin}/($env.USER)/{location}/{rest}"
}
| flatten
)
let first = $df | select origin location | polars into-df
let second = (
$df
| select rest
| each {|file|
$file
| get rest
| path parse -e ''
}
| polars into-df
| polars drop extension
| polars rename [parent stem] [path file]
)
$first
| polars append $second
| polars append $last
| polars into-nu
| update size {into filesize}
| save -f $file
}
#create anime dirs according to files
export def mk-anime [--wzf] {
if $wzf {
try {
get-files
} catch {
return-error "no files found"
}
| get name
| parse "[{fansub}]{name}_Capitulo{rest}"
| get name
| uniq
| each {|dir|
if not ($dir | ansi strip | path expand | path exists) {
mkdir $dir
}
get-files
| find -i $dir
| mv-pipe $dir
| ignore
}
return
}
try {
get-files
} catch {
return-error "no files found"
}
| get name
| parse "{fansub} {name} - {chapter}"
| get name
| uniq
| each {|dir|
if not ($dir | path expand | path exists) {
mkdir $dir
}
get-files
| find -i $dir
| mv-pipe $dir
| ignore
}
}
#create manga dirs according to files
export def mk-manga [] {
try {
get-files
} catch {
return-error "no files found"
}
| get name
| path parse
| get stem
| parse --regex '(?P<chars>.*) (?P<num>\d+)(?P<suffix>.*)$'
| get chars
| uniq
| parse --regex '^(?P<chars>.*?)(?: (?P<num>\d+))?$'
| get chars
| uniq
| each {|dir|
if not ($dir | path expand | path exists) {
mkdir $dir
}
get-files
| find -i $dir
| mv-pipe $dir
| ignore
}
}
#open google analytics csv file
export def open-analytics [file?] {
let file = get-input $in $file -n
open $file --raw
| lines
| find -v "#"
| drop nth 0
| str join "\n"
| from csv
}
#delete empty google analytics csv files
export def clean-analytics [file?] {
ls | find Analytics | where size <= 151B | rm-pipe
}
#fix green dirs
export def fix-green-dirs [] {
get-dirs | each {|dir| chmod o-w $dir.name}
}
#delete empty dirs recursively
export def rm-empty-dirs [] {
ls --du **/*
| where type == dir
| where size <= 4.0Kib
| rm-pipe
}
#replicate directory structure to a new location
export def replicate-tree [to:string] {
get-dirs -f
| each {|dir|
let new_dir = ($dir | get name | str prepend $"($to | path expand)/")
if not ($new_dir | path exists) {
print (echo-g $"creating ($new_dir)...")
mkdir $new_dir | ignore
}
}
}
#rename all files starting with certain prefix, enumerating them
export def re-enamerate [prefix] {
let files = get-files | where name like $"^($prefix)" | sort-by modified
let n_files = $files | length
let n_digits = ($n_files | math log 10) + 1 | math floor | into int
mut index = 0
mut not_move = []
mut new_name = ""
for i in 0..($n_files - 1) {
let name = ($files | get $i | get name)
let info = ($name | path parse)
$new_name = [$prefix "_" ($index | into string | fill -a r -c "0" -w $n_digits) "." ($info | get extension)] | str join
if not ($name in $not_move) {
while ($new_name | path expand | path exists) {
$not_move = $not_move | append $new_name
$index = $index + 1
$new_name = [$prefix "_" ($index | into string | fill -a r -c "0" -w $n_digits) "." ($info | get extension)] | str join
}
mv $name $new_name | ignore
}
}
}
#concatenate all files in current directory
#
#asummes all are text files
export def join-text-files [
extension:string #extension of files to concatenate
output:string #output filename (without extension)
] {
open ("*." + $extension | into glob) | save -f ($output + "." + $extension)
}
#manually rename files in a directory
export def rename-all [] {
let files = ls -s | where type == file | get name
let temp_file = mktemp -t --suffix .txt
$files | to text | save -f $temp_file
^$env.VISUAL $temp_file
let new_files = open $temp_file | str trim | lines
rm --permanent $temp_file
if $new_files != $files and ($new_files | length) == ($files | length) {
let file_table = $files | wrap old_name | merge ($new_files | wrap new_name) | where {$in.old_name != $in.new_name}
$file_table | each { mv $in.old_name $in.new_name }
$file_table
} else {
echo-g "No files renamed"
}
}
#convert svg image into a pdf file
export def svg2pdf [
svg_file #include extension
pdf_output? #include extension
] {
let pdf_output = get-input ($svg_file | path parse | get stem) $pdf_output
rsvg-convert -f pdf -o $pdf_output $svg_file
}
#rename file via pattern replace
export def rename-file [
old_pattern:string #pattern to replace
new_pattern:string #new pattern
files_pattern:string #pattern for listing target files or dirs
--regex(-r) #use regular expression for patter substitution
] {
let files = ls ($files_pattern | into glob)
let total = $files | length
$files
| get name
| enumerate
| each {|file|
let new_name = if $regex {
$file.item | str replace -r $old_pattern $new_pattern
} else {
$file.item | str replace $old_pattern $new_pattern
}
mv $file.item $new_name
progress_bar ($file.index + 1) $total
}
}
# Renames subtitles files according to tv shows names found in a directory
# Accepted syntaxes for season/episode are: 304, s3e04, s03e04, 3x04 (case insensitive)
export def subtitle-renamer [] {
# Get all subtitle files in current directory
let subtitle_files = [
(glob *.srt)
(glob *.ssa)
(glob *.sub)
] | flatten | where { |f| $f | path exists }
# Get all movie files in current directory
let movie_files = [
(glob *.avi)
(glob *.mp4)
(glob *.mkv)
] | flatten | where { |f| $f | path exists }
# Process each subtitle file
for subtitle in $subtitle_files {
let subtitle_name = $subtitle | path basename
# Try to match season and episode patterns (case insensitive)
let season_episode = match $subtitle_name {
# s01e02 pattern
$s if ($s | str downcase | find -r "s([0-9]+)e([0-9]+)" | is-not-empty) => {
$subtitle_name
| str downcase
| parse --regex '^(?P<title>.+?)\s+s(?P<season>[0-9]+)e(?P<episode>[0-9]+)\.\w+$'
| update season { into int }
| get 0
| reject title
},
# 1x02 pattern
$s if ($s | str downcase | find -r "([0-9]+)x([0-9]+)" | is-not-empty) => {
$subtitle_name
| str downcase
| parse --regex '^(?P<title>.*?) (?P<season>\d+)x(?P<episode>\d+)\.\w+$'
| update season { into int }
| get 0
| reject title
},
# 102 pattern (1=season, 02=episode)
$s if ($s | str downcase | find -r "([0-9]+)([0-9][0-9])" | is-not-empty) => {
$subtitle_name
| str downcase
| parse --regex '^(?P<title>.+?)\s+(?P<season>\d+)(?P<episode>\d{2})\.\w+$'
| update season { into int }
| get 0
| reject title
},
_ => null
}
if $season_episode != null {
print (echo-g $"Found '($subtitle_name)'")
let season = $season_episode.season
let episode = $season_episode.episode
# Look for matching movie file
for movie in $movie_files {
let movie_name = $movie | path basename
let movie_base = $movie | path basename | path parse | get stem
let subtitle_ext = $subtitle | path parse | get extension
# Check if movie name contains the season/episode pattern (case insensitive)
let is_match = (
($movie_name | str downcase | find -r $"($season)($episode)" | is-not-empty ) or
($movie_name | str downcase | find -r $"s0?($season)e($episode)" | is-not-empty ) or
($movie_name | str downcase | find -r $"($season)x($episode)" | is-not-empty )
)
if $is_match {
let new_name = $"($movie_base).($subtitle_ext)"
if $subtitle_name == $new_name {
print (echo-c "Already ok" "green")
} else if ($new_name | path exists) {
print (echo-c $"A file named '($new_name)' already exists, skipping" "green")
} else {
mv $subtitle $new_name
print (echo-c $"Renamed '($subtitle_name)' to '($new_name)'" "green")
}
break
}
}
}
}
}
# Download file with nu
export def nuwget [
url: string
--directory (-d): path # Base dir
--output (-o): path # File name
--force (-f) # Overwrite file
--silent (-s) # Don't print anything
] {
if ($directory | is-not-empty) { cd $directory }
let $file_name = $output | default { $url | url parse | get path | split row '/' | url decode | last }
if not $force and ($file_name | path exists) { error make -u {msg: "File already exists"} }
let $time = timeit { http get $url | save --progress --force=$force $file_name }
if not $silent {
print "Download results:"
{
url: $url
file: ($file_name | path basename)
cwd: ($file_name | path expand | path dirname)
time: $time
speed: $"((ls $file_name | get 0.size) / ($time | into int | $in / 10 ** 9))/s"
}
| print
}
}