Inotifywait subfolder zip creation - bash

I'm trying to make a bash script that monitors a folder and it's subfolders.
Basicly I have other app that makes subfolders and adds files to them.
I want to add the files that come into these subfolders into a zip named after the subfolder. Upon which the file in the subfolder is to be deleted.
I'm new to linux and bash scripts, so I'm sorta flunking it :s
#!/bin/bash
inotifywait -mr -e create /home/user/files |
while read filename eventlist eventfile
do
IFS='/' array=($eventfile)
zip /home/user/zips/$(array[1]) $(array[2])
done
So I have a folder /home/usr/files/ in which the app creates subfolders for isntance ...files/files1/. After which the app places the files in the files1 subfolder.
I want those files to be zipped in a folder called files1.zip.
I don't want to have the subfolder in there as well, just the files.
Is also possible to zip to another extension (ofc still being zipped) by simply adding the extension to the zip command?

There are several problems in your script. For one, you should access array elements with ${array[1]} and not $(array[1]). I did a few modifications, and this seems to work on my system:
#!/bin/bash
inotifywait -mr -e create files |
while read -r path eventlist eventfile
do
[[ $eventlist == *ISDIR* ]] && continue;
folder=$(basename "$path")
zip -j "$folder.zip" "$path/$eventfile"
done

Related

Script to move files

I used a script to create sub-directories based on the file names of all my mp4 files. My files and newly created sub directories, of the same name, are located in the smae sub directory. Now I need a script to move the mp4 files into each of the files corresponding sub directories. I hope this makes sense. ex: I would like to move "crank (2006).mp4" to the sub directory named "crank (2006)". I have about 1200 of these files to move to their already created sub directories. Please help.
Removing the .mp4 suffix uses %% to delete the sub-string .mp4 from the end of the $f variable.
The mkdir statement ensures that the sub-directory does exist before the mv command.
for f in *.mp4
do
subdir="${f%%.mp4}"
mkdir -p "$subdir"
mv "$f" "$subdir"
done
mmv 'smae/*.mp4' 'smae/#1/#1.mp4'
This is much safer than (noddy) scripts as mmv will check for loops, name collisions, possible problems in the move before moving any file etc.
Following code will,
find the .mp4 files in current directory,
create sub-directories based on the file names of all mp4 files,
move each of the files to corresponding sub directories
for f in *.mp4; do path=$(ls $f | rev | cut -c 5- | rev); mkdir $path; mv $f $path/. ; done
Ex: if "crank (2006).mp4" is available into current directory than new sub directory named "crank (2006)" will be created into current directory and "crank (2006).mp4" file will be moved into that sub-directory.
NOTE: instead of "mv" you can also use "cp" for copy files

BASH: Copy all files and directories into another directory in the same parent directory

I'm trying to make a simple script that copies all of my $HOME into another folder in $HOME called Backup/. This includes all hidden files and folders, and excludes Backup/ itself. What I have right now for the copying part is the following:
shopt -s dotglob
for file in $HOME/*
do
cp -r $file $HOME/Backup/
done
Bash tells me that it cannot copy Backup/ into itself. However, when I check the contents of $HOME/Backup/ I see that $HOME/Backup/Backup/ exists.
The copy of Backup/ in itself is useless. How can I get bash to copy over all the folders except Backup/. I tried using extglob and using cp -r $HOME/!(Backup)/ but it didn't copy over the hidden files that I need.
try rsync. you can exclude file/directories .
this is a good reference
http://www.maclife.com/article/columns/terminal_101_using_rsync_locally
Hugo,
A script like this is good, but you could try this:
cp -r * Backup/;
cp -r .* Backup/;
Another tool used with backups is tar. This compresses your backup to save disk space.
Also note, the * does not cover . hidden files.
I agree that using rsync would be a better solution, but there is an easy way to skip a directory in bash:
for file in "$HOME/"*
do
[[ $file = $HOME/Backup ]] && continue
cp -r "$file" "$HOME/Backup/"
done
This doesn't answer your question directly (the other answers already did that), but try cp -ua when you want to use cp to make a backup. This recurses directories, copies rather than follows links, preserves permissions and only copies a file if it is newer than the copy at the destination.

move multiple folders to new folder and zip the folder using tar

I want a shell script to move multiple folders to a new folder and zip or compress that folder. Can anybody please help me out?
I have a file called dirfile where directory names are stored which are to be moved to a new directory.
#!/bin/usr/sh
while read line; do
Unix_Array[$counter]=$line;
let counter=counter+1;
echo $counter;
done < dirfile
echo {Unix_Array[0]}
for line in [Unix_Array]
do
tar -czvf "$x.tar.gz" "$x"
done
The code you have looks more or less OK (though I wonder why you need two loops--it seems you could do it all at once without the intermediate array). But you're using tar -c when you should be using tar -r to add content to the file rather than creating a new tar file each time.

How to unzip to the same directory in bash

I have hundreds of directories, each containing several zip files. I would like to iterate over each directory and unzip all zip files, placing the contents of the zip files into the same directory as the zip files themselves (without creating new sub-directories). Here's the bash script I have:
#!/bin/bash
src="/path/to/directories"
for dir in `ls "$src/"`
do
unzip "$src/$dir/*"
done
This script does the unzipping, but it creates thousands of sub-directories and dumps them on my desktop! How can I get the desired behavior? I'm on Mac OSX if that makes a difference.
#!/bin/bash
src=/path/to/directories
for dir in "$src"/*
do
(cd "$dir" && unzip '*')
done

Automated unzipping of files

I have a folder full of zipped files (about 200). I would like to transform this into a folder consisting only of unzipped files. What would be the easiest and quickest way to do this?
Please note that I would like to remove the zipped file from the folder once it us unzipped.
Also, I'm on a Mac.
Thanks!
You can do something like:
for file in `ls *.zip`; do unzip -f $file; rm $file; done
We are looping through all the zip files in the directory, unzipping it and then deleting it.
Note that the -f option of zip will overwrite any file without prompting if it finds a duplicate.
You need to run the above one-line command on the command line from the directory that has the all the zip files. That one line is equivalent to:
for file in `ls *.zip` # ls *.zip gets the list of all zip file..iterate through that list one by one.
do # for each file in the list do the following:
unzip -f $file # unzip the file.
rm $file # delete it.
done
I found this answer which is a simple one liner to gunzip all .gz compressed files within a folder.
Basically you cd to the folder and then run
gunzip *.gz
If you want to only unzip files with a certain prefix you put that before the *
gunzip example*.gz
Easy as cake!

Resources