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

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.

Related

Change file extension, but keep original file

I have a set of .txt files I want to change the extension of, but keeping the original.
I'm trying to cp each file to a "temp", then mv that "temp" to a new file with a new extension.
for file in $#; do
cp ${file} temp; mv temp ${file}.fas
done
I can't find out why it's not working, but nothing happens. I'm new to bash, so any help appreciated.
Read about substring replacement in bash variables.
This will copy all files like foo.txt to foo.fas:
for file in *.txt; do
cp "$file" "${file/%.txt/.fas}"
done

Bash script to extract and re-compress contents from a list of ZIP files

I have about 500+ ZIP files which I need to re-compress using the best compression possible.
I looking at creating a script (one liner if possible) to extract the contents from each ZIP files (Some of them contain multiple files inside, but no more folders), pipe the output so that 7zip can re-compress them achieving a maximum compression ZIP file.
The script that I created below uses an intermediate stage by extracting into a temporary folder and then re-compressing, but I would like to skip that stage if possible.
Could anyone help? Thanks in advance.
This is my script at the moment, which works OK. But I am sure there must be a more efficient manner to achieve this.
predir=$(pwd)
for i in *.zip; do
dirname='echo "$i" | cut -d'.' -f1'
mkdir "$dirname"
7z x "$i" -o"$dirname"/ && rm "$i"
7za a -mm=Deflate -mfb=258 -mpass=15 -r "$i" "$predir"/"$dirname"/"*" && rm -rf "$dirname"/
done

Inotifywait subfolder zip creation

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

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