Bash(Linux) Shell Script to zip the files older than 30 days - zip

**Search for *.aud files in a directory for past 30 days and zip it; remove all those files
after successful zip
I used following but it is not removing the files after zip
find . -mtime -30 | xargs tar --no-recursion -czf Audit_Mar_2011.tgz **

#!/bin.bash
#quick and dirty:
FILES=`find . -mtime -30 | xargs`
tar --no-recursion -czf Audit_Mar_2011.tgz "${FILES}"
rm -rf "${FILES}"

Related

Create a empty tar file and then store its name in a variable

I am writing a shell script in which a tar file with today's date and time will be created.
tar -zvcf "log_grabber$(date '+%y-%m-%d_%H%M').tar.gz" --files-from /dev/null
Now, to add more files to this tar files after running the find command. How can I get the name of the tar file and use it in the output of the find command?
find . -type f -name 'local*' -newermt "$user_date" -exec tar -rvf <variable tar file> {} \;
Any help will be very much useful.
Instead of
tar -zvcf "log_grabber$(date '+%y-%m-%d_%H%M').tar.gz" --files-from /dev/null
Create a variable with the name first and use that:
name="log_grabber$(date '+%y-%m-%d_%H%M').tar.gz"
tar -zvcf "$name" --files-from /dev/null
And then:
find . -type f -name 'local*' -newermt "$user_date" -exec tar -rvf "$name" {} +
Note that I changed \; to + so that tar gets multiple files in one invocation, rather than one tar invocation per file.

Linux project: bash script to archive and remove files

I've been set a mini project to run a bash script to archive and remove files that are older than 'x' number of days. The file will be archived in the /nfs/archive directory and they need to be compressed (TAR) or removed... e.g. '/test.sh 15' would remove files older than 15 days. Moreover, I also need to input some validation checking before removing files...
My code so far:
> #!/bin/bash
>
> #ProjectEssentials:
>
> # TAR: allows you to back up files
> # cronjob: schedule taks
> # command: find . -mtime +('x') -exec rm {} \; this will remove files older than 'x' number of days
>
> find /Users/alimohamed/downloads/nfs/CAMERA -type f -name '*.mov'
> -mtime +10 -exec mv {} /Users/limohamed/downloads/nfs/archive/ \;
>
> # TAR: This will allow for the compression
>
> tar -cvzf doc.tar.gz /Users/alimohamed/downloads/nfs/archive/
>
> # Backup before removing files 'cp filename{,.bak}'? find /Users/alimohamed/downloads/nfs/CAMERA -type f name '*.mov' -mtime +30
> -exec rm {} \; ~
Any help would much appreciated!!
Modified script to fix few typos. Note backup file will have a YYYY-MM-DD, to allow for multiple backups (limited to one backup per day).Using TOP to make script generic - work on any account.
X=15 # Number of days
# Move old files (>=X days) to archive, via work folder
TOP=~/downloads/nfs
mkdir -p "$TOP/work"
find $TOP/CAMERA -type f -name '*.mov' -mtime +"$X" -exec mv {} "$WORK/work" \;
# Create daily backup (note YYYY-MM-DD in file name from work folder
tar -cvzf $TOP/archive/doc.$(date +%Y-%m-%d).tar.gz -C "$TOP/work" .
# Remove all files that were backed-up, If needed
find "$TOP/work" -type f -name '*.mov' -exec rm {} \; ~

How to find all tar files in various sub-folders, then extract them in the same folder they were found?

I have lots of sub-folders, with only some containing a tar file. i.e.:
folder1/
folder2/this-is-a.tar
folder3/
folder4/this-is-another.tar
I can find which dirs have the tar by simply doing ls */*.tar.
What I want to achieve is somehow find all .tar files, then extract them in the same directory they are found, then delete the .tars.
I've tried ls */*.tar | xargs -n1 tar xvf but that extracts the tars in in the directory I'm in, not the directory the tars were found.
Any help would be greatly appreciated.
for i in */*.tar ; do pushd `dirname $i` ; tar xf `basename $i` && rm `basename $i` ; popd ; done
Edit: this is probably a better way:
find . -type f -iname "*.tar" -print0 -execdir tar xf {} \; -delete
for file in */*.tar; do
(cd `dirname $file`; tar xvf `basename $file`)
unlink $file
done

Find and tar for each file in Linux

I have a list of files with different modification times, 1_raw,2_raw,3_raw... I want to find files that are modified more than 10 days ago and zip them to release disk space. However, the command:
find . -mtime +10 |xargs tar -cvzf backup.tar.gz
will create a new file backup.tar.gz
What I want is to create a tarball for each file, so that I can easily unzip each of them when needed. After the command, my files should become: 1_raw.tar.gz, 2_raw.tar.gz, 3_raw.tar.gz...
Is there anyway to do this? Thanks!
Something like this is what you are after:
find . -mtime +10 -type f -print0 | while IFS= read -r -d '' file; do
tar -cvzf "${file}.tar.gz" "$file"
done
The -type f was added so that it doesn't also process directories, just files.
This adds a compressed archive of each file that was modified more than 10 days ago, in all subdirectories, and places the compressed archive next to its respective unarchived version (in the same folder). I assume this is what you wanted.
If you didn't need to handle whitespaces in the path, you could do with simply:
for f in $(find . -mtime +10 -type f) ; do
tar -cvzf "${f}.tar.gz" "$f"
done
Simply, try this
$ find . -mtime +10 | xargs -I {} tar czvf {}.tar.gz {}
Here, {} indicates replace-str
-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.
https://linux.die.net/man/1/xargs

create a tar.gz of a directory and clean up the directory except a file?

I would like to know if tar (1.15.1, Linux system) already has something like this before creating a script for it.
I would like to create a dir.tar.gz of a given dir/ that contains a specific dir/myfile.gz. I would like the command to do 2 specific things:
Preserve dir/myfile.gz
Delete everything else in dir/ after the dir.tar.gz is complete.
For example:
tree -if .
.
./dir
./dir/dir2
./dir/dir2/file3
./dir/file1
./dir/file2
./dir/myfile.gz
2 directories, 4 files
I would like the result of the tar command to be:
tree -if
.
./dir
./dir/myfile.gz
./dir.tar.gz
1 directory, 2 files
I have read about the --delete and --extract options, but I am not sure how to apply them in one single command. Any ideas?
This should work:
tar -zcvf dir.tar.gz --exclude="*.gz" ./dir/. && find ./dir/. ! -name "*.gz" -exec rm -rf {} +
Test:
$ tree -if .
.
./dir
./dir/dir2
./dir/dir2/file3
./dir/file1
./dir/file2
./dir/myfile.gz
2 directories, 4 files
$ tar -zcvf dir.tar.gz --exclude="*.gz" ./dir/. && find ./dir/. ! -name "*.gz" -exec rm -rf {} +
./dir/./
./dir/./file1
./dir/./dir2/
./dir/./dir2/file3
./dir/./file2
rm: cannot remove `.' or `..'
$ tree -if .
.
./dir
./dir/myfile.gz
./dir.tar.gz
1 directory, 2 files
You may also be interested in tardy which is a tar post-processor (filter), so you can operate (even on the fly) on your tar archive and remove the offending file.

Resources