Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[script] spostare file da molte cartelle a una sola usando regex #175

Closed
pigreco opened this issue Jan 25, 2021 · 6 comments
Closed

[script] spostare file da molte cartelle a una sola usando regex #175

pigreco opened this issue Jan 25, 2021 · 6 comments

Comments

@pigreco
Copy link
Contributor

pigreco commented Jan 25, 2021

In una cartella ho tre sottocartelle e per ognuna ho n sottocartelle; per ogni cartelle , a livello più basso, ho molti file con nomi e estensioni diversi (csv, txt, json): tramite l'utility fd ho individuato i file che mi interessano:

cartella/
│
├── cartella1/
│   ├── sottocartella1
│   │   ├── file1.csv
│   │   ├── file2.txt
│   │   ├── file1.json
│   │   ├── ...
│   ├── sottocartella2
│   │   ├── file10.csv
│   │   ├── file20.txt
│   │   ├── file10.json
│   │   ├── ...
│   ├── sottocartella3
│   │   ├── ...
├── cartella2/
│   ├── sottocartella1
│   │   ├── file100.csv
│   │   ├── file200.txt
│   │   ├── file100.json
│   │   ├── ...
│   ├── sottocartella2
│   │   ├── file1000.csv
│   │   ├── file2000.txt
│   │   ├── file1000.json
│   │   ├── ...
│   ├── sottocartella3
│   │   ├── ...
├── cartella3/
│   ├── sottocartella1
│   │   ├── fileA.csv
│   │   ├── fileB.txt
│   │   ├── fileC.json
│   │   ├── ...
│   ├── sottocartella2
│   │   ├── file10W.csv
│   │   ├── file20R.txt
│   │   ├── file10A.json
│   │   ├── ...
│   ├── sottocartella3
│   │   ├── ...

fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$"

questi file vorrei spostarli in un'altra cartella e per farlo ho provato a usare il comando mv:

mv fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$" miaCartella

ma restituisce un errore:
image

come potrei risolvere?

@aborruso
Copy link
Member

aborruso commented Jan 25, 2021

@pigreco hai guardato la guida di fd?

Devi usarlo in questa modalità https://github.com/sharkdp/fd#parallel-command-execution

Fai dei test su una cartella di test con qualche file. E se va, poi applica.
Te lo dico perché mv è un comando distruttivo.

@aborruso
Copy link
Member

Ok, visto che mi hai scritto che non è chiaro quanto riportato, aggiungo qualche elemento.

Due cose:

  • nella sezione che ti ho indicato si cita l'opzione -x, per eseguire comandi in parallelo per ogni risultato della ricerca;
  • e che {/} corrisponde al nome del singolo file, senza percorso.

Se ad esempio ai una cartella con dei CSV, e in questa hai la sottocartella out, per copiare i file di output delle ricerca in out il comando sarà:

fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$" -x cp {/} ./out

@pigreco
Copy link
Contributor Author

pigreco commented Jan 25, 2021

usando il comando suggerito fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$" ho verificato che i file ci sono, ma quando gli dico di copiarli mi segnala:
image
la sottocartella out esiste

@aborruso
Copy link
Member

aborruso commented Jan 25, 2021

Perché nel tuo caso hai cartelle e sotto cartelle, quindi gli stai dicendo, dalla cartella in cui sei cp ./file.sv ./out.

Ma non c'è ./file.sv nella cartella in cui sei.

Se leggi la guida, c'è anche {}, il percorso completo. Cambialo in qualcosa come

fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$" -x cp {} ./out/{/}

@pigreco
Copy link
Contributor Author

pigreco commented Jan 25, 2021

@aborruso quello che mi serve è

fd "^.+([A-Z]+[0-9]+|[A-Z])-particelle.csv$" -x cp {} ./out

Funziona!!! :-) Grazie!

@pigreco
Copy link
Contributor Author

pigreco commented Jan 28, 2021

@aborruso grazie
ricetta pubblicata
https://tansignari.opendatasicilia.it/ricette/bash/spostare_molti_file/

@pigreco pigreco closed this as completed Jan 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants