How to zip 90days old files and move it to a specific folder using bash in linux - linux

I have lots of files in my FILES folder. I want to zip the files that are 90days old then remove it from the FILES folder and move it to the ARCHIVES folder using bash in linux.
This is my folder structure:
root#user:/var/FILES
root#user:/var/ARCHIVES
I have created a script to zip a file but don't know how to specify the age of the file
zip -r zipped.zip *.*
so i coded something like
FILE=find *.* -mtime +90
zip -r zipped.zip $FILE
but only returns error. Thanks

You can use:
find . -mtime +90 -exec zip zipped.zip '{}' +
EDIT If you want move zipped file to an archive folder then you can do:
find . -mtime +90 -exec zip zipped.zip '{}' + && mv zipped.zip /var/ARCHIVES

you can try find
find /var/FILES/ -type f -mtime +90 -exec zip -r zipped.zip {} \; -exec mv {} /var/ARCHIVES \;
Not sure whether I understand you correct, if you want to save zipped.zip in /var/ARCHIVES
just use this:
find /var/FILES/ -type f -mtime +90 -exec zip -r /var/ARCHIVES/zipped.zip {} \;

Related

How do I recover files that disappeared after wrong MV command format?

I'm trying to move files from the current directory to another directory by date, but I accidentally used the wrong target format:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
instead of
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "../folder" \;
Then my files just disappeared.
I can't seem to find it anywhere. I've tried on both target & source directories and even the non existent directory that I have accidentally sent the files to.
I would just like to know if I can still recover the files.
They're all gone. When you run:
find . -maxdepth 1 -mtime +365 -type f -exec mv "{}" "..\folder" \;
You are executing, for every file, the command:
mv filename ..folder
In other words, you renamed every file to the name ..folder. Each file overwrote the next one. The contents of the ..folder file are whatever file was last processed by your command, and all the rest are gone.

How to delete multiple type of files?

I can delete .zip files using following command.
find . -type f -name '*.log.*.zip' -exec rm \{\} \;
Is it possible to delete .zip and .gz file at the same time ?
find . -type f -name '*.log.*.zip' | '*.log.*.gz' -exec rm \{\} \;
You can try like this using brace expansion:
$ rm -rf log.{zip,gz}

Recursively recode all project files excluding some directories and preserving permissions

How to recursively recode all project files excluding some directories and preserving permissions?
Based on this question, but its solution does not preserve permissions, so I had to modify it.
WARNING: since the recursive removal is a part of the solution, use it on your own risk
Task:
Recursively recode all project files (iso8859-8 -> utf-8) excluding '.git' and '.idea' dirs and preserving permissions.
Solution (worked well in my case):
Backup your project's dir, then cd there. Run:
find . -not -path "./.git/*" -not -path "./.idea/*" -type f -print -exec iconv -f iso8859-8 -t utf-8 -o {}.converted {} \; -exec sh -c 'cat {}.converted > {}' \; -exec rm {}.converted \;
Binary and image files will fail to recode since they aren't text, so files like 'image.jpeg.converted' will be left along with 'image.jpeg'. To clean up this mess:
find . -not -path "./.git/*" -not -path "./.idea/*" -type f -regex '.*\.converted' -exec rm {} \;
Before you do that, you may want just print (without rm) to see that there are only those files listed that you'd really like to remove.

Zip files from a directory

I need to find files i directory and zip them under the same name.
i am trying the following
find . -name "ABC_*.txt" -mtime +30 -exec sh -c zip '{}' '{}' \;"
But something is wrong.
basically if find command finds 3 files say:
./ABC_1.txt
./ABC_2.txt
./ABC_3.txt
I will need 3 zip files:
./ABC_1.txt.zip
./ABC_2.txt.zip
./ABC_3.txt.zip
thanks in advance.
Try this:
find . -name "ABC_*.txt" -mtime +30 -exec zip "{}.zip" "{}" \;
You are likely overwriting your original file and will need to supply an extension to your ZIP.
You can use execdir option:
find . -name "ABC_*.txt" -mtime +30 -execdir sh -c 'zip "$1.zip" "$1"' - '{}' \;

unzip specific extension only

I have a a directory with zip archives containing .jpg, .png, .gif images. I want to unzip each archive taking the images only and putting them in a folder with the name of the archive.
So:
files/archive1.zip
files/archive2.zip
files/archive3.zip
files/archive4.zip
Open archive1.zip - take sunflower.jpg, rose_sun.gif. Make a folder files/archive1/ and add the images to that folder, so files/archive1/folder1.jpg, files/archive1/rose_sun.gif. Do this to each archive.
I really don't know how this can be done, all suggestions are welcome. I have over 600 archives and an automatic solution would be a lifesaver, preferably a linux solution.
In Short
You can do this with a one-liner find + unzip.
find . -name "*.zip" -type f -exec unzip -jd "images/{}" "{}" "*.jpg" "*.png" "*.gif" \;
In Detail
unzip allows you to specify the files you want:
unzip archive.zip "*.jpg" "*.png" "*.gif"
And -d a target directory:
unzip -d images/ archive.zip "*.jpg" "*.png" "*.gif"
Combine that with a find, and you can extract all the images in all zips:
find . -name "*.zip" -type f -exec unzip -d images/ {} "*.jpg" "*.png" "*.gif" \;
Using unzip -j to junk the extraction of the zip's internal directory structure, we can do it all in one command. This gives you the flat image list separated by zip name that you desire as a one-liner.
find . -name "*.zip" -type f -exec unzip -jd "images/{}" "{}" "*.jpg" "*.png" "*.gif" \;
A limitation is that unzip -d won't create more than one new level of directories, so just mkdir images first. Enjoy.
7zip can do this, and has a Linux version.
mkdir files/archive1
7z e -ofiles/archive1/ files/archive1.zip *.jpg *.png *.gif
(Just tested it, it works.)
Something along the lines of:
#!/bin/bash
cd ~/basedir/files
for file in *.zip ; do
newfile=$(echo "${file}" | sed -e 's/^files.//' -e 's/.zip$//')
echo ":${newfile}:"
mkdir tmp
rm -rf "${newfile}"
mkdir "${newfile}"
cp "${newfile}.zip" tmp
cd tmp
unzip "${newfile}.zip"
find . -name '*.jpg' -exec cp {} "../${newfile}" ';'
find . -name '*.gif' -exec cp {} "../${newfile}" ';'
cd ..
rm -rf tmp
done
This is tested and will handle spaces in filenames (both the zip files and the extracted files). You may have collisions if the zip file has the same file name in different directories (you can't avoid this if you're going to flatten the directory structure).
You can write a program using a zip library. If you do Mono, you can use DotNetZip.
The code would look like this:
foreach (var archive in listOfZips)
{
using (var zip = ZipFile.Read(archive)
{
foreach (ZipEntry e in zip)
{
if (IsImageFile(e.FileName))
{
e.FileName = System.IO.Path.Combine(archive.Replace(".zip",""),
System.IO.Path.GetFileName(e.FileName));
e.Extract("files");
}
}
}
}
Perl's Archive-Zip is a good library for zipping/unzipping.
Here's my take on the first answer...
#!/bin/bash
cd files
for zip_name in *.zip ; do
dir_name=$(echo "${zip_name}" | sed -e 's/^files.//' -e 's/.zip$//')
mkdir ${dir_name}
7z e -o${dir_name}/ ${zip_name} *.jpg *.png *.gif
done
or, if you'd just like to use the regular unzip command...
unzip -d ${dir_name}/ ${zip_name} *.jpg *.png *.gif
I haven't tested this, but it should work... or something along these lines. Definitely more efficient than the first solution. :)
Hope this helps!

Resources