Skip to main content

Bash script to archive different files and folders into a single folder which will be named using current timestamp

I use this script to back up files that are created in different locations. I feel easy to collect all the related files and folders to compress and save them as a single zip archive which is named as per the current date and time.


#!/bin/bash

# this program creates a zip archive of myFolder(containing files and folders), file1 , file2 , file3 and file4 and puts the archive in myOpDir. The name of the final zip archive will be given by the current timestamp. For eg : final_20111220_103015.zip

#create a directory for putting all the files

myFolder="/home/user/Desktop/myFolderWithContents"

myOpDir="/home/user/Desktop/myFinalDir"

file1="/home/user/Desktop/test.tcl"
file2="/home/user/Desktop/test.dump.xml"
file3="/home/user/Desktop/myDir/test.txt"
file4="/home/user/Desktop/myDir/test.txt.xml"

mkdir $myOpDir/temp

# copy the files and folder to to temp dir
cp -r $myFolder $myOpDir/temp/`

cp $file1 $myOpDir/temp/test.tcl
cp $file2 $myOpDir/temp/test.dump.xml

cp $file3 $myOpDir/temp/test.txt
cp $file4 $myOpDir/temp/test.txt.xml

#Now compress all files in a single archive

cd $myOpDir/temp/

for FILE in `ls`;do
zip -r final_`date +%Y%m%d_%H%M%S`.zip $FILE
done

cp final_`date +%Y%m%d_%H%M%S`.zip ../

cd ..

rm -r $myOpDir/temp

Comments

foss said…
Bash Script to rename all files in the current folder:

for i in *; do mv "$i" "Unix_$i"; done

Popular posts from this blog

Python : split any file (binary) to different pieces and join them

This python program takes an input file and then splits it into different smaller chunks. Next it can collect all the different chunks and join it to get the original file. -------------------------------------------------------- # define the function to split the file into smaller chunks def splitFile(inputFile,chunkSize): #read the contents of the file f = open(inputFile, 'rb') data = f.read() # read the entire content of the file f.close() # get the length of data, ie size of the input file in bytes bytes = len(data) #calculate the number of chunks to be created noOfChunks= bytes/chunkSize if(bytes%chunkSize): noOfChunks+=1 #create a info.txt file for writing metadata f = open('info.txt', 'w') f.write(inputFile+','+'chunk,'+str(noOfChunks)+','+str(chunkSize)) f.close() chunkNames = [] for i in range(0, bytes+1, chunkSize): fn1 = "chunk%s...

Installing NS-3 in Ubuntu

Installing testing it and running NS-3 in Ubuntu 10.04 NS-3 is a discrete-event network simulator basically for simulating Internet systems. #The list of Dependency for ns 3.12(sorry if something is missing): sudo apt-get install gcc g++ python sudo apt-get install gcc g++ python python-dev sudo apt-get install mercurial sudo apt-get install bzr sudo apt-get install gdb valgrind sudo apt-get install gsl-bin libgsl0-dev libgsl0ldbl sudo apt-get install flex bison sudo apt-get install g++-3.4 gcc-3.4 sudo apt-get install tcpdump sudo apt-get install sqlite sqlite3 libsqlite3-dev sudo apt-get install libxml2 libxml2-dev sudo apt-get install libgtk2.0-0 libgtk2.0-dev sudo apt-get install libgtk2.0-0 libgtk2.0-dev sudo apt-get install vtun lxc sudo apt-get install uncrustify sudo apt-get install doxygen graphviz imagemagick sudo apt-get install texlive texlive-pdf texlive-latex-extra texlive-generic-extra texlive-generic-recommended sudo apt-get install python-sphinx dia t...

calculate hop count in a graph using python

The following program finds out the hop count of the nodes in a connected network. Hop Count means the number of point to point connections in a graph of network. For example, in the graph shown below the hop count from node a to node b is one, node b to node c is two, etc. This program assumes the graph given above. usage : getHopCount(dictNeigh, node1,node2) Where:   dictNeigh : dictionary of list of neighbor of each node in the network node1 and node2 are the nodes for which hop count is calculated by the program Exception: if the nodes are not connected then it returns Zero import copy dictNeigh = dict() # dictNeigh defines the neighbors dictNeigh['a'] = ['b','c','d'] # neighbors of 'a' dictNeigh['b'] = ['a']# neighbors of 'b' dictNeigh['c'] = ['a','d'] dictNeigh['d'] = ['a','c','e'] dictNeigh['e'] = ['d','f'] dictN...