Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending dates on files

Status
Not open for further replies.

segment

ISP
Jun 15, 2004
225
US
I have a directory that flows as follows:

$ ls
20060701 20060707 20060713 20060719 20060725 20060731
20060702 20060708 20060714 20060720 20060726
20060703 20060709 20060715 20060721 20060727
20060704 20060710 20060716 20060722 20060728
20060705 20060711 20060717 20060723 20060729
20060706 20060712 20060718 20060724 20060730

This is what the inside of the directory looks like:

$ ls 20060701
CHKSUM.SHA1 mysql-20060701.sql.bz2 rt3-20060701.sql.bz2
cti-20060701.sql.bz2 ns1.tar.bz2 term-20060701.sql.bz2
configure.tar.bz2 ns3.tar.bz2 von-20060701.sql.bz2
xterm-20060701.sql.bz2 prod1.tar.bz2

All across the dates, the structure is the same. I'm looking for a way to append the date to files in those folders which don't have the date on them. Example prod1.tar.bz needs to be prod1-20060701.tar.bz
 
I guess something like...

dir="200607*"

cd $dir
ls|grep -v 200607 >> files
for i in `cat files` ; do sed 's/./ /g' ; awk '{print $1"$THIS_DATE",$2,$3}'
done

Or something
 
So again... Down and dirty...

dir="200607*"

# Go to that directory
cd $dir
# Grep out anything that has a date to a file
ls|grep -v 200607 >> files

# Take that file split the periods to insert the date of that directory, then close it back?
for i in `cat files`;do sed 's/./ /g';awk '{print $1"$THIS_DATE",$2,$3}';done

Or something


BEGIN{
}
{
isdir1 = "[ -d " $1 " ] "
isdir2 = "[ -d " $2 " ] "


sfile = $1
dfile = $2

if( ( system(isdir1) ) == 0 || system((isdir2)) == 0 )
{
printf "%s can't rename\n",sfile,dfile
next
}
else if ( sfile == dfile )
{
printf "Skiping, \"%s\" is already renamed\n",sfile
next
}
else
{
mvcmd = "mv " sfile " " dfile
printf "Renaming %s to %s\n",sfile,dfile
system(mvcmd)
}
}

END{
}
 
Hi

This task is more suitable for a shell script.

Try it, if you like the outputed commands, then remove the [tt]echo[/tt] and run it again.
Code:
for d in *; do
  for f in "$d/"*; do
    [[ ${f#*/} != *$d* ]] && echo mv "$f" "${f%%.*}-$d.${f#*.}"
  done
done
Tested with [tt]bash[/tt], should work with [tt]ksh[/tt] too.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top