Find and tar Files

Preso da https://www.baeldung.com/linux/find-tar-files

2.1. Using Command Substitution
First, let’s consider the simplest possible solution, which uses command substitution to execute our find and supply its output to tar:

freestar
$ tar -czf archive.tar.gz `find . -type f`

Although this is a pretty straightforward approach, this command doesn’t handle files with spaces in the name. Suppose our find command returns a file named “hello world.txt” in its output. Since find doesn’t add quotes around the filenames or escape whitespace characters within filenames in its output, the tar command will see this as two file arguments: hello and world.txt. Therefore, it won’t add the file to our archive.

2.2. Using xargs
Another easy option would be using xargs to convert the find output to a tar argument:

$ find . -type f | xargs tar -czf archive.tar.gz

Unluckily, this has the same shortcoming as the previous one since spaces are the default delimiter that xargs uses to split arguments. However, this time we can easily fix it by changing the delimiter to another character with the -d option:

$ find . -type f | xargs -d “\n” tar -czf archive.tar.gz

2.3. Using find -print0
Although our last example works, it’s usually a better practice to use a null character separator to avoid unexpected results. This can be easily achieved using find -print0, which replaces the default separator with null. We’ll also use tar –null to split files with this character instead of a newline:

$ find . -type f -print0 | tar -czf archive.tar.gz –null -T –

One thing to keep in mind, though, is that -print0 applies only to the last condition, so, if we have more than one, we need to wrap them with parentheses:

freestar
$ find . \( -type f -o -name ‘*.c’ \) -print0 | tar -czf archive.tar.gz –null -T –

Note that here we’re using tar -T – to compress the files listed in stdin instead of the xargs equivalent.

2.4. Using a File
The next approach we’re going to consider involves using an intermediate file to store find output and then use the same file as an input for tar. This solution comes in handy if we want to keep track of the files we’re compressing for logs or further processing:

$ find . -type f > archiveFileList && tar -czf archive.tar.gz -T archiveFileList

2.5. Using find -exec
Lastly, let’s consider a slightly different scenario in which we want to create multiple archives, one for each file found. In order to do that, we can use find -exec, which will run a given command per matched file. Furthermore, we can use the current file name by referencing it with a pair of curly braces:

$ find . -type f -exec tar -czf {}.tar.gz {} \;

Installazione AWS CLI senza modifiche al sistema

Per utilizzare AWS CLI su macchina linux bastano questi semplici comandi (in presenza dei prerequisiti naturalmente):

mkdir bin
cd bin/
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
cd awscli-bundle/
./install -b ~/bin/aws
~/bin/aws --version

Enjoy!

 

Su macchine legacy:

mkdir aws
cd aws/
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle-1.19.112.zip" -o "awscli-bundle.zip"
unzip awscli-bundle.zip
cd awscli-bundle/
./install -b ~/aws/aws
~/aws/aws --version

 

Come uscire dal labirinto di link simbolici (ln -s) senza impazzire

Ovvero come trovare il vero path di un file o cartella velocemente, quando ci sono di mezzo più link simbolici.

Il comando che ci può aiutare è

readlink

Esempio:

Sto lavorando su /var/www/dominio.it/htdocs/stats e voglio capire se la modifica può avere impatti in altri percorsi.

Basta eseguire:

readlink -f /var/www/dominio.it/htdocs/stats

e l’output ci dirà dove è la cartella al netto di link simbolici:

diego@BEETLE:~$ readlink -f /var/www/dominio.it/htdocs/stats
/var/www/vhosts/dominio.it/stats

Esempio di file di logrotate

Visto che mi serve spesso, ma non me lo ricordo mai, pubblico un semplice esempio di file di logrotate con le opzioni che preferisco.

Spero che possa servire a chi non ha voglia di leggere “tfm” 🙂

Il file va messo poi (di solito) in /etc/logrotate.d/

/var/log/miadir/*.log
{

rotate 4
monthly
missingok
notifempty
compress
delaycompress

}

Consigli

Per verificarne la sintassi e controllare che sia quello che effettivamente vogliamo fare utilizzare il comando

Ricerca nella history alla pressione di un tasto

Ogni volta che reinstallo un PC, fatico sempre a ritrovare tutte le personalizzazioni impostate nel corso del tempo.

Una di quelle di cui ormai non riesco a fare a meno è la ricerca automatica nella history dei comandi bash.

La posto in modo da faticare meno a trovarla, spero che possa servire anche a voi!

Per mapparla sui tasti page up e page down è sufficiente modificare o inserire se mancano le seguenti righe in /etc/inputrc:

“\e[5~”: history-search-backward
“\e[6~”: history-search-forward