Showing posts with label bash scripts. Show all posts
Showing posts with label bash scripts. Show all posts

Sunday, 15 April 2012

bash scrupt to copy all songs in an m3u list into a folder

This is not the first time I thought about it and it took a lot of digging and googling in order to get the right command line to do it. So, all credit goes to the thefekete.net who is the initial poster and my only contribution is turning the initial command line into a script.

#!/bin/bash

# ------------------------------------------------------------------------------------
# Copy all files from a playlist to a dest folder
# ------------------------------------------------------------------------------------
if [ "$#" != 2 ]; then
 echo usage $0 playlist.m3u dest-dir
 exit 1
fi

if [ ! -d "$2" ]; then
    mkdir $2
    echo Creating directory `pwd`"/"$2
fi

if [ -z "$1" ]; then
 echo  $1 is not a valid file
 exit 2
fi

cat "$1" | grep -v '#' | while read i; do cp "${i}" "$2" ; echo "${i}"; done

Thank you the thefekete

Friday, 12 June 2009

Linux: Safely deleting .rpmnew files

Just got on my hands on the new Fedora 11 today. I have to admit that I didn't get a chance to explore all the new things that Leonidas has brought along.

For starters, the feature that amazed me was the preupgrade script that magically downloaded everything, installed it and even changed my repos to point to correct ones for Fedora 11. And when I say repos, I do not mean only the basic ones, but also livna and RPM Fusion.

So the next thing that I had in my mind was to check all the .rpmnew files installed in my system and determine what was needed, so I started doing a search with a command like find / -name *.rpmnew -print, I ended up using diff and deleting the .rpmnew file that was identical to the original.

After comparing /usr/share/config/colors/Royal.colors.rpmnew, /usr/share/config/colors/40.colors.rpmnew, /usr/share/config/colors/Web.colors.rpmnew and /usr/share/config/colors/Rainbow.colors.rpmnew with their original versions and deleting all four of them, I decided that a little script could save me quit a lot of trouble. So after a little bit of digging I ended up with the following code:

#!/bin/bash

# Locate all *.rpmnew files in your system and compares them with the 
# original files without the rpmnew extention. 
# Files are then compared using diff. If their contents are the same, then
# the .rpmnew version is removed.

RPM_NEW_LIST=`find / -name "*.rpmnew" -print 2>/dev/null`

for RPMNEW_FILE in $RPM_NEW_LIST
do
    # Get the file without the .rpmnew extention
    ORIGINAL_FILE=${RPMNEW_FILE%".rpmnew"}
    # Compare it with the original
    DIFFERENT=`diff $RPMNEW_FILE $ORIGINAL_FILE`
    # If diff's answer is not empty ...
    if [ -n "$DIFFERENT" ]; then
        echo "Please examnine files $ORIGINAL_FILE and $RPMNEW_FILE "
    else
        # File is safe to remove
        # rm -f -v $RPMNEW_FILE
        echo "$RPMNEW_FILE file can safely be removed." 
    fi
done

I run this script as root on both my Fedora and CentOS 5.3 machines The simple version I have here will show you the files that are safe to delete and the files need your attention. If you like, you can uncomment the rm -fv line at the end of the script, but I would strongly advice against it.

Note: Sometimes rpm leaves out .rpmsave files as well so, if you are really into cleaning up your system, it is wise to search for these files also.

Saturday, 31 March 2007

Linux: How many songs do I have

I have always wanted to know the exact number of music files that I own. Of course Amarok appears to have the correct answer, still I always wanted to write something like that
#!/bin/bash
# Count the music files starting form MUSIC_BASE
# Thanassis Bakalidis
# 31-03-2007

# This is the directory containing all my music
MUSIC_BASE=/mnt/Culture/My\ Mjavascript:void(0)usic
# Add more music file extentions here
MUSIC_EXTENTIONS="mp3 wma ogg flac"

echo Base directory is $MUSIC_BASE

TOTAL_FILES=0
for ext in $MUSIC_EXTENTIONS
 do 
   echo ""
   echo Saerching for $ext  files
   current=`find "$MUSIC_BASE" -type f -name "*.$ext" -print | wc -l`
   echo Located $current $ext files
   TOTAL_FILES=`expr $TOTAL_FILES + $current`
done

echo ""
echo A total of $TOTAL_FILES music files were accounted.
exit

Wednesday, 21 March 2007

Linux: A simple backup script

Here is the script I use for backing up my home directory onto the hard drive an other machine (mounted using samba). The script can be configured by changing the values of the top variables

#!/bin/bash
# Script to back up my home directory
# Created 21-03-2007
# Thanassis Bakalidis

BKP_FILE="/mnt/KomUsers/bakalidi/LinuxBackups/my_home.tar.bz2"
BKP_ROOT="/home/thanassis"
LOG_FILE="/home/thanassis/backup_output.txt"
EXCLUDE_FILES_FILE="/home/thanassis/.files_to_exclude"
ERROR_FILE="/home/thanassis/backup_errors.txt"

EXCLUDE_DIRS="/home/thanassis/mp3 /home/thanassis/java"

# remove file if existing
if [ -f $EXCLUDE_FILES_FILE ]; then
 rm $EXCLUDE_FILES_FILE
fi

# get a list of all file patterns that do not need be backed up
for path in $EXCLUDE_DIRS
do
 echo $path/\* >> $EXCLUDE_FILES_FILE
done

# finally append the log file in the list of excluded files
echo $LOG_FILE >> $EXCLUDE_FILES_FILE
echo $EXCLUDE_FILES_FILE >> $EXCLUDE_FILES_FILE

# start the log File
echo Backup of $BKP_ROOT Starting at `date` > $LOG_FILE

# after creating the list of files to be excluded perform the actual backup
/bin/tar -jcvf $BKP_FILE -X $EXCLUDE_FILES_FILE --exclude-caches  $BKP_ROOT  1>> $LOG_FILE 2>$ERROR_FILE

# mark the end time stamp on the log file and exit
echo Backup of $BKP_ROOT finished at  `date` >> $LOG_FILE
exit 0

It is true that one could directly edit the $EXCLUDE_FILES_FILE and add the patterns of the files that should not be backed up. Personally, I prefer the approach shown above, because that way I keep all my information regarding backups in one place and thus have less configuration files to worry about-- Of course there is always more than one way to do things right.

Finally I have added the following in my crontab config :

thanassis@lxbakalidis:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXhnhNTc installed on Wed Mar 21 11:52:39 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
30 18 * * * /home/thanassis/bin/tarbackup.sh

The /mnt/KomUsers speciffied as target in script file directory points to the users folder of a Windows 2003 server on my local area network. The company administrator runs tape backup jobs at exactly 19:00 everyday, so starting my backup at 18:30 allows me to keep a copy of my data every day in a separate tape following the day, week, month company schedule.

Saturday, 10 February 2007

Linux: Changing file and dirctory permisions for an entire directory tree

Suppose you have a directory named /mydir and you need to change the permissions on all the files for the entire directory tree. Using a command like chmod -R 644 * might get you into trouble since this may change execute (i.e. traversal) access even for existing directories.

A friend from Germany (thanks Antoni) gave me the fhis solution :

First the files

find /mydir -type f -exec chmod 0644 {} \;

... and then the directories

find /mydir -type d -exec chmod 0755 {} \;

Note: The previous commands reset the file permissions completely. If you want to just add read access, while preserving the rest, you might prefer a u+r or g+r command line to chmod as in the following example which adds read and execute access to /mydir and all it's subdirectories.

find /mydir -type d -exec chmod u+rx {} \;

By the way, since I have lately found to need this more and more - as I move data to and from USB sticks -- I created a simple script that given a directory gives all files permissions 0644 and all directories 0755

#!/bin/bash
# File resetPerms.sh

if [ -d $1 ]
then
  echo Reseting file and directory access on $1
  find $1 -type f -exec chmod 0644 {} \;
  find $1 -type d -exec chmod 0755 {} \;
fi