Skip to content

Snippets

Retain X amount of files

I encountered an issue today where someone requested that we keep the 10 newest backup files, while the rest of them (the oldest) were deleted. So I made this little script:

#!/bin/bash

FILE_DIR="/home/jorge/backup/"
NUMBER_OF_FILES="$(ls -t $FILE_DIR | wc -l)"
RETAIN=10

if [ "$NUMBER_OF_FILES" -ge "$RETAIN" ]; then
    cd $FILE_DIR
    ls -t | sed -e "1,$RETAIN d" | xargs rm -f
fi

This can be modified to match with any type of file or filename.

Delete files between two dates

To delete all files in a directory, between two dates, use the find command:

find /path/to/dir/ -type f -newermt 2022-01-01 \! -newermt 2023-01-01 -exec rm -fv '{}' +

Scan for newly added disks

To scan for newly added/modified disks, run the following command as root:

for f in /sys/class/scsci_host/*; do echo "---" > $f/scan; done

Compare the output of two commands

..without creating a temporary file:

diff -y <(ls First_directory) <(ls Second_directory)

The -y is just to show a side-by-side comparison. Very handy! And the result?

[jorge@j-laptop temp]$ diff -y <(ls First_directory) <(ls Second_directory)
common.txt                          common.txt
first_file.txt                            | second_file.txt

Thanks to my colleague Ingvar for showing me this!

Vim – Commenting out multiple lines

There are two ways to do this without a vim plugin:

  1. Select your lines with VISUAL BLOCK (CTRL-V), then press I to insert before all highlighted lines. Next type your comment character (#, ;, depending on the language), then press ESC twice.
s/^/#

In other words, substitute the selected start of lines with the # character.

Select browser from variable DISPLAY

I have two monitors at work, which I like to use in a separate X-screen setup. Meaning that each monitor has an own X-server running on it. Thing is though, I like to use Firefox on Monitor 1, while I use Google Chrome on Monitor 2. So with this script, right-clicking a link in Monitor 1 will open it up in Firefox, while if I do so on Monitor 2, it will open it up in Google Chrome. Very nifty. Just go to System -> Preferences -> Preferred Applications (if you’re using GNOME like moi) and select this script instead of a regular browser.

#!/bin/bash
# A little script that selects a browser
# to show an URL on, based on the variable
# DISPLAY on a separate X-screen setup.
# In other words, the active monitor.
#
# Jorge Enrique Barrera 

if [ "$DISPLAY" = ":0.0" ]; then
    /usr/bin/firefox $1
elif [ "$DISPLAY" = ":0.1" ]; then
    /usr/bin/google-chrome $1
fi

Rapidshare Linux script revisited

Update: I just added a new variable in the script, DAYS, which specifies how many days the cookie you create is valid for.
Update 2011-09-13: Seems Rapidshare have been tampering with their API, which made the cookie-generating script useless. I basically changed the getaccountdetails_v1 with getaccountdetails, and the script is working fine now. I’ve fixed the script below to reflect this.

The code to get the Rapidshare-cookie in my previous post about this subject, Rapidshare Linux Script, unfortunately doesn’t work anymore. It seems that Rapidshare has moved from using https://ssl.rapidshare.com/premzone.html to https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi, so things just don’t work as they used to. This is for people with a premium account.

However, there’s a solution. First off, log in to your Rapidshare-account, go to Settings and make sure to check the box named Direct downloads, requested files are saved without redirection via RapidShare.

To grab the cookie you need, place the piece of code below in a file, save it, do a chmod +x on it. Just remember to replace MyUserName and MyPassword with your username and password before you run the script:

Note: The script requires curl to be installed.

#!/bin/bash
# Script to get your Rapidshare-cookie, nom nom
# Jorge Enrique Barrera

# Variables you can change
USERNAME="MyUserName" # Username to your Rapidshare premium-account
PASSWORD="MyPassword" # Password
COOKIEJAR="/home/jorge/.cookies" # Where the cookies are to be placed
DAYS="30" # The number of days the cookie is valid for

## Do not change anything below here ##

DATA="sub=getaccountdetails&withcookie;=1&type;=prem&login;=$USERNAME&password;=$PASSWORD"
COOKIE_STRING=$(curl -s --data "$DATA" "https://api.rapidshare.com/cgi-bin/rsapi.cgi" | grep cookie | cut -c 8-)
COOKIE=".rapidshare.com TRUE / FALSE $(($(date +%s)+24*60*60*$DAYS)) enc $COOKIE_STRING"

if [ ! -d $COOKIEJAR ]; then
        echo "Creating the directory $COOKIEJAR."
        mkdir $COOKIEJAR
        echo "Done."
fi

echo "$COOKIE" | tr ' ' '\t' > $COOKIEJAR/rapidshare

Now that the cookie is present, you can proceed by downloading the files you want. Which way you do that, is up to you.

Either way I will paste two scripts, one with wget and the other one with aria2, and you can then make your pick. Just place the URL of the files you want to download in /home/YourUsername/Downloads/.url, one in each line:

wget

#!/bin/bash
LIST="/home/jorge/Downloads/.url"
cd ~/Downloads
for url in `cat $LIST`
do
    wget -c --load-cookies /home/jorge/.cookies/rapidshare $url
done

Save this file as wrsd.sh and make it executable by doing a:

chmod +x wrsd.sh

aria2

#!/bin/bash
cd ~/Downloads
aria2c -j 5 -c --load-cookies=/home/jorge/.cookies/rapidshare -i /home/jorge/Downloads/.url

Enjoy. :)

Python password generator

#!/usr/bin/env python
# Filename: passgen.py

import random, string

mystr = string.ascii_letters+string.digits
random_string = []

while len(random_string) <= 8:
    random_string.append(random.choice(mystr))
print "".join(random_string)