Restructuring the filetree – moving files from multiple directories

I am considering changing the structure of my /home directory, maybe completely changing my data organisation habbits.

I found this post on ubuntuforums very useful:

You need to use find

For this example I`m going to assume that all the .txt files are located in directories and subdirectories of your Documents folder and you want to move them to a directory in your home called scripts
Code:
find ~/Documents -name '*.txt' -exec mv '{}' ~/scripts \;

Advertisement

Passing an external variable to AWK

Confronted with a heap of colon separated text files which had to be merged and cleaned of unrelated lines and columns, i tryed my luck inside Excel and spend a lot of time doing it manually, but finally got fed up.

So I decided to use AWK on the task.

A FOR-loop lists the files in the folder into the UNIX pipe.

AWK selects the non-empty observations and adds the name of the file as a classifier to the beginning of the line (the result is a repeated measure dataset).

This is the code:

for CSV in `ls`
do
cat $CSV | awk -F ";" '{
if ($2 ~ /[0-9]+/) {print CSV , FS , $0;}}'
done

Remark: -F ";" option specifies how to distinguish the columns/fields of the lines/records in the file(default is ” ” or empty space).

BUT: The variable CSV gets not passed to AWK by default it has to be fed into AWK.

Solution:
The
-v CATEGORY=$CSV
option feeds the external variable CSV into the AWK-variable CATEGORY.

This gives:
for CSV in `ls`
do
cat $CSV | awk -F ";" -v CATEGORY=$CSV '{
if ($2 ~ /[0-9]+/) {print CATEGORY , FS , $0;}}'
done

.. and works 🙂

Hat tip:fpmurphy

Mount a CD Image on Harddisk

A copy of a CD containing e.g. installation software or a game (so called iso image) can be mounted directly from the harddrive. This might be useful to mimic a CD without inserting the CD into the (very slow and power consuming) CD/DVD-drive.

1. Create a mountpoint e.g. in the /media directory:
sudo mkdir /media/iso

2. Mount the image MyCDimage.iso as a CD:
sudo mount -t iso9660 -o loop MyCDimage.iso /media/iso

3. Display all filesystems to check its mounted:
df -h

In order to play e.g. Windows games with wine the mountpoint /media/iso has to be added as a drive under wine.
winecfg
choose the drive tab and add /media/iso