Category Archives: Linux - Page 2

Send mail from command line

Sending mail directly from a Linux server can often be handy. Here is how I do it

Send mail

cat "Test" | mail -s "Test subject" someone@someplace.com

This will send a mail with the message “Test” and subject line “Test subejct” to email adress: someone@someplace.com

Send attachments

mutt -s "Attachment subject" -a attachment.jpg someone@someplace.com

This will open up your favorite editor so you can create a mail message to your attachment. After you save and exit the editor you just press ‘y’ to send the message with the attachment (you will be inside Mutt then)

Tested on RHEL 3

Remove files in batches of x files

Every now and then I try to do things on a large amount of files – this often ends in a command error about “Too many files” or something similar. One solution to this is to send batches of information to the command instead of sending all at once.

Here is an example on how to delete files in batches of 1000 files:

ls | xargs -n 1000 rm -v

The trick here is the -n 1000 that will send the result from the ls command in batches of a 1000 results per time until the ls list is empty to rm. Neat ah? 🙂

Tested on RHEL 3

Repeat a command every x seconds

Say you wanted to see whenever the content of a folder changes – what to do? Easy! Use the watch command:

watch 'ls -l'

This will clear the screen and output the contents of the command and update it every 2 seconds (clearing screen before each update). The watch command works for most scripts and commands

Tip: Using Tmux and the watch command together can help you create a really cool window with a bunch of “live” updated small windows

Tested on RHEL 3