Posts Tagged ‘BASH’

Check how old a Linux password is using chage

I came across a command today that I’ve never really had a use for before.  chage can be used to check when a password was last modified and also shows any password expiration rules etc.  It can also be used to implement password expiration. A very useful tool if password policies are required, although PAM […]

Delete a file older than X days using mtime

List files older than 5 days: find ./ -mtime +5 To count all files older than 5 days find ./ -mtime +5 |wc -l To move files to an archive directory: find ./ -mtime +5 -exec mv {} /var/archive \; -print To delete the files find ./ -mtime +5 -exec rm {} \; -print

Basic AWK usage in BASH

To print a field from a file (-F is the field separator and $ is the number of the field to print): awk -F':’ ‘{ print $1 }’ /etc/passwd or awk -F':’ ‘{ print $1 $3 }’ /etc/passwd or awk -F':’ ‘{ print “username: ” $1 “\t\tuid:” $3″ }’ /etc/passwd

BASH – making tab completion case insensitive

This is very lazy, but comes in handy for example if your browsing a Windows mount with random uppercases in file/folder names. Add: make your tab completion case insensitive To your $HOME/.inputrc file (create the file if it doesn’t exist).  Alternatively you can make this userwide by adding it to /etc/inputrc . You might need […]

Random integers in BASH

Never really had a need for it before in BASH, but the following formula allows you to create random integers between two values: rnumber=$(((RANDOM%(max-min+divisibleBy))/divisibleBy*divisibleBy+min)) Useful for example to create random passwords that have a length within a set range. < /dev/urandom tr -dc A-Za-z0-9_$*?!.:=@#% | head -c$((RANDOM %4 +8))

Adding date/time to BASH history

Useful if you have several admins on a server, so you can figure out who did what when. Edit /etc/bashrc  and add: export HISTTIMEFORMAT=”%h/%d – %H:%M:%S “

BASH create random password

apg -MSNCL  -m8 -x11 -n10 -E\%\\\/\|\`\<\>\+\{\}\”\’ or < /dev/urandom tr -dc A-Za-z0-9_$*?! | head -c10 Another useful implementation thanks to, a sample random password generator (put in your ~/.bashrc): genpasswd() { local l=$1 [ “$l” == “” ] && l=20 tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs } Run it: […]

BASH email test script

#!/bin/bash # script to send simple email # Email To ? echo “Enter Mail To” read EMAIL # Email text/message EMAILMESSAGE=”/tmp/emailmessage.txt” echo “This is an email message test”>>$EMAILMESSAGE echo | date >> $EMAILMESSAGE echo “Test”>>$EMAILMESSAGE echo “Test”>>$EMAILMESSAGE nail -v -r “” -s “Test Email” -S smtp=mysmtpserver $EMAIL < $EMAILMESSAGE #Clean up rm /tmp/emailmessage.txt