Tag Archive for: Bash

Renaming Files in a folder to Sequential Numbers with a Prefix

First of all, we have to order our files using ls -v and concatenate them to a standar output with cat -n. From this ouput we read the sequence (n) and the name of each file (f). Finally, we rename the files using mv, adding a prefix and the sequence (n)

ls -v | cat -n | while read n f; do mv "$f" "prefix$n.jpg"; done

If we want to add a zero pad (p) to our sequence (n) we can do it easily with printf.

ls -v | cat -n | while read n f; do printf -v p %03d $n; mv "$f" "prefix$p.jpg"; done

How to Resize Multiple Images at Once

mogrify *.jpg -scale 50% *

Prevent SSH from disconnecting

If you are working with your server, ex: sending some file now and then with SSH, you can experience some annoying connection losses. Actually, if SSH has been idle for a while the connection will be closed.

You can prevent this easily adding a config file with just 2 lines of code to your .ssh folder in your home.

nano ~/.ssh/config
Host *
    ServerAliveInterval 30

Note: in Ubuntu TCPKeepAlive is active by default.

Best way to update Node

Make things easy with package n.

n latest

Simulate different times/dates in your app

If you want to test your app with different times you don’t need to change your computer time (what can lead to unexpected consequences), instead you can simple open a terminal and launch your app with faketime.

faketime -f "@2049-01-11 17:50:00" firefox