-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.nu
666 lines (580 loc) · 14.2 KB
/
files.nu
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
######################################################
# here because they are needed in this file
######################################################
#green echo
export def echo-g [string:string] {
echo $"(ansi -e { fg: '#00ff00' attr: b })($string)(ansi reset)"
}
#red echo
export def echo-r [string:string] {
echo $"(ansi -e { fg: '#ff0000' attr: b })($string)(ansi reset)"
}
#custom color echo
export def echo-c [string:string,color:string,--bold(-b)] {
if $bold {
echo $"(ansi -e { fg: ($color) attr: b })($string)(ansi reset)"
} else {
echo $"(ansi -e { fg: ($color)})($string)(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)] {
let inp = $in
let type = $inp
| describe
| if not $full {
split row '<' | get 0
} else {
$in
}
if $type == list and ($in | columns | is-not-empty) {
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
] {
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 {
match $extension {
"md"|"Rmd" => {glow $file},
"nu" => {open --raw $file | nu-highlight | bat},
"R"|"c"|"m"|"py"|"sh" => {bat $file},
"csv"|"json"|"sqlite"|"xls"|"xlsx" => {open $file},
_ => {bat $file}
}
}
}
#open file
export def 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}
}
)
bash -c $'xdg-open "($file)" 2>/dev/null &'
}
#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 | length) {
1 => {
if ($n_copies | is-empty) {
lp $file
} else {
lp -n $n_copies $file
}
},
_ => {
$file
| each {|name|
print-file $name
}
}
}
}
#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 "7z max" [
filename: string #existing or not existing 7z filename
...rest: string #files to compress and extra flags for 7z (add flags between quotes)
--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 | get name | ansi-strip-table
if ($files | is-empty) {return}
let number = ($files | length) - 1
for i in 0..$number {
^rm -rf ($files | get $i) | ignore
progress_bar ($i + 1) ($number + 1)
}
}
#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 | get name | ansi-strip-table
let number = ($files | length) - 1
for i in 0..$number {
let file = $files | get $i
if $force {
cp -fr $file ($to | path expand)
} else {
cp -ur $file ($to | path expand)
}
progress_bar ($i + 1) ($number + 1)
}
}
#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 | get name | ansi-strip-table
let number = ($files | length) - 1
for i in 0..$number {
let file = $files | get $i
if $force {
mv -f $file ($to | path expand)
} else {
mv -u $file ($to | path expand)
}
progress_bar ($i + 1) ($number + 1)
}
}
#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 -c
},
false => {
ls | sort-by modified | grid -c
}
}
},
false => {
match $reverse {
true => {
ls | sort-by -i -r type name | grid -c
},
false => {
ls | sort-by -i type name | grid -c
}
}
}
}
}
#ls sorted by name
export def ln [--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
}
}
#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 =~ $search
} else {
get-files $directory -f | find =~ $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 =~ $"/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 " " | get 0)
}
}
}
#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 =~ "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
| each {|file|
$file
| parse "[{fansub}]{name}_Capitulo{rest}"
}
| flatten
| 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
| each {|file|
$file
| parse "{fansub} {name} - {chapter}"
}
| flatten
| get name
| 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 =~ $"^($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
] {
let files = ls ($files_pattern | into glob)
let total = $files | length
$files
| get name
| enumerate
| each {|file|
mv $file.item ($file.item | str replace $old_pattern $new_pattern)
progress_bar ($file.index + 1) $total
}
}