Scripting Galore – Copy list of files from bigger repository into another location

November 29th, 2009

I don’t consider myself a script guru, but sometimes I like to create small pieces of bash code to ease operations on my linux box… and I guess it’s good idea to note here some of the recent solutions I’ve found for later remembering.

I’ll try to comment them, so that readers will be able to readapt them to their needs.

Need: I have a list of files in subdirectories under main one and I want to copy only some of them to new location. I have the list of files I need in a text file.

#!/bin/bash
#
DIR=root # Location where the list I want to extract is located
FILE=wantedlist # with the name of the file
DIR_VIDEO=”/media/bigdisk” # Master repository containing iles in subdirectories
COPY_DEST=”/media/externalHDD/backup” # where I want to put my files
y=`cat /$DIR/$FILE|wc -l` # counting how many files I want to copy
for i in `seq 1 $y` # starting the cycle
do
# I’ll copy file mentioned in every line in the text list in new location
cp -R /$DIR_VIDEO/”`cat /$DIR/$FILE | tail -$i|head -1`” $COPY_DEST
done

Please note the usage of head and tail to go line by line in reverse order ;) , while the cycle allows me to repeat this for each line in the text list.

#!/bin/bash
#
DIR=root
FILE=listarmando
DIR_VIDEO=”/media/video”
COPY_DEST=”/media/roarinnas/secondodisco”
y=`cat /$DIR/$FILE|wc -l`
for i in `seq 1 $y`
do
cp -R /$DIR_VIDEO/”`cat /$DIR/$FILE | tail -$i|head -1`” $COPY_DEST
d

Email this post Email this post

Extending an ext3 partition on Linux (without losing data)

November 3rd, 2009

Here’s another successful operation done on fantastic Linux OS that is a pure dream for other operating systems.

Situation: I have a virtual machine with 20 GB HDD, partitioned as follows:

95% ext3 on primary partition

5% swap on first partition of extended partition

I certainly can expand my hard disk, by setting the new size in VMware configuration… so I brought it to 60 GB… but then I have a problem, since I cannot use tools like gparted to extend the main partition since the swap is in the middle between the two.

What to do? Well, again Google has been a best friend.

Well done, well written, clear and… working!


Email this post Email this post

Tunneling Apps in SSH

July 4th, 2009

Yesterday I found a very handy functionality in Putty: tunneling apps in SSH.

Not that I did not know that this technique exist ;) but for the first time I tried it and worked out of the box.

The idea is to enable tunneling of insecure applications inside an established and authenticated SSH encrypted session, using Putty as a client.

Scenario in my case is that I have few web based appliances at home acting as a media center, a NAS, etc… each of them being manageable by a web based interface on various ports.

I could certainly open destination PAT on my router, but it would increase the risk… and I don’t trust level of security implemented in such systems.

Therefore I’ve done something represented in picture below

ssh tunnel

How to configure it in Putty? Well, when you launch the session to connect to SSHD Server, check in SSH options – Tunnels.

There you find the chance to add the port forwarding parameters to be set as follows:

putty-tunnels

Enjoy!


Email this post Email this post

Enabling SPDIF sound on Ubuntu with Dell Docking station

April 13th, 2009

Should you come across the same need, I just found a working solution, even if quite odd.

The trick to get it working is to go into sound settings and enable IEC958 Playback AC97-SPSA, checking that volume is set to… 0.

Magically sound will begin to flow out of the SPDIF interface.


Email this post Email this post

Bulk File Rename

March 24th, 2009

Situation:

I have multiple files with extension .sh.modified and I need to rename them all into .sh extension.

Solution:

for file in *.sh.modified; do mv ${file} ${file%sh.modified}.sh; done


Email this post Email this post

How to check for new files in a directory

March 21st, 2009

Made this small script for this purpose…

#!/bin/bash
#
# Script to check a directory and write in file the new files
# since last check.
#
# Written by RoarinPenguin (
roarinpenguin@rottigni.net) on
#
21 march 2009
# Released under GPL License
#
# You need to create a file called lastcheck.time in same
# directory of this script
#

ADMIN="change to administrator email address"
DIR2MON=/var/www/dir-clienti

> ./newfiles.list
echo "Last check for new files, done on "`date` >> ./newfiles.list
find $DIR2MON -maxdepth 5 -newer ./lastcheck.time >> ./newfiles.list
touch ./lastcheck.time


Email this post Email this post

Using variables in sed

March 10th, 2009

In a world of Perl, sed seems to be an archaic method of doing things reserved to the real brave geeks!

But sometimes sed does perfectly fantastic job, like it did for me few minutes ago… with some caveats.

My goal was to replace $i with the value of the variable, determined by the first part of the script:

for i in `seq 1 50`;

the problem was that I made rest of the cycle as follows:

do cat newtest.xml|sed –e ‘s/variable/$i/’ >> multifirewallimport.xml; done

and the result was that through all my file the word variable was replaced with the word $i.

What was wrong? Well… Googling for it I discovered the issue was in usage of single quote instead of double quote.

The correct form is:

for i in `seq 1 50`; do cat newtest.xml|sed -e "s/variable/$i/" >> multifirewallimport.xml; done


Email this post Email this post

Enabling root access to vsftpd

March 6th, 2009

It took me a while to figure out due to misleading information over the vast Internet suggesting to search for non existing /etc/vsftpd.users or something like this.

Thing you have to do is to edit /etc/ftpusers and remove root from there.


Email this post Email this post

How to check if LDAPS is really running on a server?

February 9th, 2009

Sometimes this is necessary since the server “appears” to be running (netstat -an|grep 636 returns port in LISTEN state, but the daemon behind is not operative because (for instance) the certificate has not been installed.

If this is the case, grab an openssl client and issue the following command:

openssl s_client host <address of the target host> –port 636 (this is LDAPS standard port)

If server does have valid certificate you should get answer like:

If it does not, you’ll get something like this:

image


Email this post Email this post

Assigning privileges to a user on MySQL DB

February 3rd, 2009

Quick and dirty commands from commandline:

mysql -u <user> -p<password>
mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO ‘wpuser’@'localhost’ IDENTIFIED BY ‘dbpassword’;
mysql> SET PASSWORD FOR ‘wpuser’@'localhost’ = OLD_PASSWORD(‘dbpassword’);
mysql> FLUSH PRIVILEGES;
mysql> quit;


Email this post Email this post