LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-31-2008, 09:58 PM   #1
dgermann
Member
 
Registered: Aug 2004
Distribution: Ubuntu 16.04 lts desk; Ubuntu 14.04 server
Posts: 366

Rep: Reputation: 31
Question date stamp file or directory name


Hi--

How can this be done?

I want to set up a script to copy files and directory structures and have the new files or at least the directories named with a date and time stamp in the file/directory name itself.

I have a crontab file which does something similar by creating an hourly10.tgz file and an hourly11.tgz file, etc., but I do not know how to do this with whole directories, so that it automatically adds time and date.

The script file for creating those files says:
Code:
set $(date)
  filename="/backups2/hourly`date +'%H'`.tgz"
But I do not know how to read what it is doing, nor even what to search for to find the right syntax.

I think what I need it to do is to use the cp command with something like what is in that script. I see that doing "date --help" produces a list of these things--am I in the correct area?

Can anybody point me in the right direction?

Many thanks!
 
Old 01-31-2008, 11:09 PM   #2
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
To copy a whole directory you would probably want to use '/usr/bin/cp -a'.

Try 'man cp'
 
Old 01-31-2008, 11:19 PM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by dgermann View Post

The script file for creating those files says:
Code:
set $(date)
  filename="/backups2/hourly`date +'%H'`.tgz"
This is just MY PREFERENCE (not saying that you are wrong) but I wouldn't use back tics for the command...I would do something like this...

Code:
filename="/backups2/hourly$(date +'%H').tgz"
...Unless it's in sh then you would have to use back tics...

Last edited by custangro; 01-31-2008 at 11:20 PM.
 
Old 02-01-2008, 10:22 AM   #4
dgermann
Member
 
Registered: Aug 2004
Distribution: Ubuntu 16.04 lts desk; Ubuntu 14.04 server
Posts: 366

Original Poster
Rep: Reputation: 31
Question

dive & custangro--

Thanks for your quick responses.

I really don't understand ticks and quotes and all that, so I am not sure.... (Somebody else set up the script files for me years ago.) Why is that better?

I like the idea about cp -a: that is not something I have used before.

What I do want to do is provide a different name for the resulting copies, so that they have the same name as the original plus the time and date stamp. So I do not see anything in man cp that helps me with that. Or perhaps I just don't know where to look.

Thanks!

:- Doug.
 
Old 02-01-2008, 10:44 AM   #5
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
Code:
for d in *
do
DIR="$d"`date +%Y%m%d`
cp -a "$d" $DIR
done
For example.
 
Old 02-02-2008, 11:54 AM   #6
juanctes
Member
 
Registered: Dec 2004
Location: Argentina, corrientes (far from buenos aires, to the north)
Distribution: Ubuntu :(
Posts: 74

Rep: Reputation: 15
Smile here is how i do this

this makes a bkp with timestamp of an entire dierctory tree of a NetBeans project. (and optionaly the database)
Code:
#!/bin/bash
if [ -z $2 ]; then
    tar -cjf "$1-`date +%a-%d-%m-%y_[%H-%M]`.tar.bz2" "$1"
else
    bkpData="$2-`date +%a-%d-%m-%y_[%H-%M]`.sql"
    mysqldump -u root -p30359651 $2 > $bkpData
    tar -cjf "$1-`date +%a-%d-%m-%y_[%H-%M]`.tar.bz2" "$1" $bkpData
    rm $bkpData
resulting in somthing like:
NetBeans_Projects-Mon-28-01-08_[21-40].tar.bz2
the next script I made for transpassing the entire directory structure of a NEtBEans project WITHOUT the configurations files that wouldn't let me load the project on a diferent machine.
Code:
#!/bin/bash

# La idea es importar los *.java a otro arbol igual y ver si el netbeans puede utilizarlo

# $1 debe ser el arbol importado.
# $2 el destino. si esta vacio (lo crea sina archivos de configuracion) si no deberia tener una estructura de directorios igual.

find "$1" -type d > Temp_Project_Dirs
grep -v /nbproject Temp_Project_Dirs | grep -v /dist | grep -v /build > Project_Dirs
Num_dirs=`wc -l Project_Dirs | cut -d" " -f1`
for i in `seq 1 $Num_dirs` ; do
    head -n $i Project_Dirs | tail -1 > Project_Dirs_linea
    linea=`cat Project_Dirs_linea`
        find "$linea" -type f -iname "*.java" -maxdepth 1 > Project_Dirs_content
        find "$linea" -type f -iname "*.form" -maxdepth 1 > Project_Dirs_forms
        find "$linea" -type f -iname "*.gif" -maxdepth 1 > Project_Dirs_images
        find "$linea" -type f -iname "*.jpg" -maxdepth 1 > Project_Dirs_images
        find "$linea" -type f -iname "*.jar" -maxdepth 1 > Project_Dirs_libs
        mkdir -p "import/$linea"

        Num_files=`wc -l Project_Dirs_content | cut -d" " -f1`
        for j in `seq 1 $Num_files` ; do
            file=`head -n $j Project_Dirs_content | tail -1`
            cp "$file" "import/$linea/"
        done

        Num_forms=`wc -l Project_Dirs_forms | cut -d" " -f1`
        for j in `seq 1 $Num_forms` ; do
            file=`head -n $j Project_Dirs_forms | tail -1`
            cp "$file" "import/$linea/"
        done

        Num_images=`wc -l Project_Dirs_images | cut -d" " -f1`
        for j in `seq 1 $Num_images` ; do
            file=`head -n $j Project_Dirs_images | tail -1`
            cp "$file" "import/$linea/"
        done

        Num_libs=`wc -l Project_Dirs_libs | cut -d" " -f1`
        for j in `seq 1 $Num_libs` ; do
            file=`head -n $j Project_Dirs_libs | tail -1`
            cp "$file" "import/$linea/"
        done

done
rm Project_Dirs_forms Temp_Project_Dirs Project_Dirs_linea Project_Dirs Project_Dirs_content Project_Dirs_images
the important parts for you may be:
Code:
find "$1" -type d > Temp_Project_Dirs
grep -v /nbproject Temp_Project_Dirs | grep -v /dist | grep -v /build > Project_Dirs # IF YOU WANT TO EXCLUDE SOME DIRS TOO
Num_dirs=`wc -l Project_Dirs | cut -d" " -f1`
for i in `seq 1 $Num_dirs` ; do
    head -n $i Project_Dirs | tail -1 > Project_Dirs_linea
    linea=`cat Project_Dirs_linea`
    find "$linea" -type f -iname "*.java" -maxdepth 1 > Project_Dirs_content #ONLY WHAT YOU WANT -> "*.JAVA" for me
    Num_files=`wc -l Project_Dirs_content | cut -d" " -f1`
        for j in `seq 1 $Num_files` ; do
            file=`head -n $j Project_Dirs_content | tail -1`
            cp "$file" "import/$linea/"
        done
done
rm Temp_Project_Dirs Project_Dirs_linea Project_Dirs Project_Dirs_content
the duplicated tree will reside in ./import/<duplicated tree>
you should mix them to add the timestam thing..
also you could relace:
Quote:
find "$linea" -type f -iname "*.java" -maxdepth 1 > Project_Dirs_content #ONLY WHAT YOU WANT -> "*.JAVA" for me
Num_files=`wc -l Project_Dirs_content | cut -d" " -f1`
for j in `seq 1 $Num_files` ; do
file=`head -n $j Project_Dirs_content | tail -1`
cp "$file" "import/$linea/"
done
for
Quote:
find "$linea" -type f -iname "*.java" -maxdepth 1 -exec cp {} "import/$linea/" \; #ONLY WHAT YOU WANT -> "*.JAVA" for me

Last edited by juanctes; 02-02-2008 at 12:00 PM. Reason: to add an alternative to some code
 
Old 02-02-2008, 05:53 PM   #7
dgermann
Member
 
Registered: Aug 2004
Distribution: Ubuntu 16.04 lts desk; Ubuntu 14.04 server
Posts: 366

Original Poster
Rep: Reputation: 31
Question

dive and juanctes--

Many thanks!

I am working on it and testing it out. Back to you shortly.

Thanks to each of you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic date stamp in text document gemini728 Linux - Newbie 3 02-25-2009 11:15 AM
Add time stamp to file minus one day dolphs Linux - General 3 11-07-2007 11:17 AM
Rename file based on its own date/time stamp airman99 Linux - General 19 09-05-2006 08:52 AM
time stamp for file modification youngstorm Linux - Newbie 2 11-01-2005 04:46 PM
Rename file with date stamp MacSob Linux - General 6 09-13-2005 02:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration