LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2017, 03:12 PM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Append date to different named files


I realize that I opened a thread concerning this, but it wasn't clear. Let's say I have three files:

cat.xlsx
dog.xlsx
bird.xlsx

and I wanted them to look like this:

cat_01092017.xlsx
dog_01092017.xlsx
bird_01092017.xlsx

How can I go about doing this? All files will have a .xlsx extension

With my current code:

file1=DOG_BROWN.xlsx; cfile1=DOG_BROWN; current_time=$(date "+_%Y%m%d.xlsx"); newfile1=$cfile1$current_time; mv $file1 $newfile1

It's not as dynamic as I would like it cause I have to put in manually every new file that get generated.

Last edited by trickydba; 01-09-2017 at 03:14 PM.
 
Old 01-09-2017, 03:43 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668Reputation: 1668
If you put it in a script named say "addtime.sh" with contents like:

Code:
file1=$1
basefile1=$(echo $file1 |awk -F. '{print $1}')
current_time=$(date "+_%Y%m%d.xlsx")

mv $file1 ${basefile1}${current_time}
You make the script executable then run it against any file:
./addtime.sh bird.xlsx

Alternatively you could look for files in the directory in your script and do a for loop:
Code:
for file1 in $(ls *[a-z].xlsx)
do 
basefile1=$(echo $file1 |awk -F. '{print $1}')
current_time=$(date "+_%Y%m%d.xlsx")

mv $file1 ${basefile1}${current_time}
done
The latter assumes your files are always named with an alpha character followed by .xlsx so would exclude any files you'd already renamed.

Last edited by MensaWater; 01-10-2017 at 11:57 AM.
 
1 members found this post helpful.
Old 01-09-2017, 03:55 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 17,243
Blog Entries: 10

Rep: Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160Reputation: 5160
Code:
#!/bin/bash
now="$(date +%Y%m%d)
for i in *.xlsx; do
    mv "$i" "${i%.xlsx}_${now}.xlsx"
done
you're welcome.

edit:
oh, someone else managed to whip up something more elaborate meanwhile.

Last edited by ondoho; 01-09-2017 at 03:57 PM.
 
2 members found this post helpful.
Old 01-09-2017, 04:12 PM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Obligatory one-liner. You can use this for any filetype (instead of just .xlsx) and appears to be somewhat reliable. Only the extension should be cut off / reattached. Remove the echo once you establish it is working correctly.

Code:
datestampfile() { for i in $@; do echo mv "$i" "${i%.*}_$(date +'%Y%m%d').${i##*.}"; done; }
Usage:

Code:
datestampfile file.mp4
datestampfile *
datestampfile *.mp4
Example output:

Code:
mv foobar.ext foobar_date.ext
mv filez.10.mov filez.10_20170109.mov
mv filez.10.mp4 filez.10_20170109.mp4
mv filez.10.xlsx filez.10_20170109.xlsx
mv filez.11.mov filez.11_20170109.mov
mv filez.11.mp4 filez.11_20170109.mp4
mv filez.11.xlsx filez.11_20170109.xlsx
mv filez.12.mov filez.12_20170109.mov
mv filez.12.mp4 filez.12_20170109.mp4
mv filez.12.xlsx filez.12_20170109.xlsx
mv filez.13.mov filez.13_20170109.mov
mv filez.13.mp4 filez.13_20170109.mp4
mv filez.13.xlsx filez.13_20170109.xlsx
mv filez.14.mov filez.14_20170109.mov
mv filez.14.mp4 filez.14_20170109.mp4
mv filez.14.xlsx filez.14_20170109.xlsx
mv filez.15.mov filez.15_20170109.mov
 
2 members found this post helpful.
Old 01-10-2017, 06:30 AM   #5
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Ya'll are freakin AWESOME!!!!!!!!!!!!!!!!!!!!! Thank you sooooooo much!!! Fast too!!! 8.))
 
Old 01-10-2017, 06:31 AM   #6
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
All above codes resolve my issue. What do I say to anyone with the same issue that reads this thread...........take yer pick!!! lol
 
Old 01-10-2017, 06:32 AM   #7
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Mensawater............hey neighbor I'm in the A too!!!!
 
Old 01-10-2017, 06:35 AM   #8
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Mensawater............... Can the files already have a .xlsx extension or must it be removed first?
 
Old 01-10-2017, 06:50 AM   #9
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Mensawater....I got an error using your second code. All I did was copy and pasted it in, was I supposed to edit your code?
 
Old 01-10-2017, 07:09 AM   #10
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@Mensawater...............your 1st code suggestion did not add the date to any files. I don't know if it matters but in some of the files they have an underscore

Last edited by trickydba; 01-10-2017 at 07:17 AM.
 
Old 01-10-2017, 07:14 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,317
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
Why have you not tried to use the rename command?:
Code:
$ ls
bird.xlsx     cat.xlsx     dog.xlsx
$ rename 's/.xlsx/_01092017.xlsx/' *.xlsx
$ ls
bird_01092017.xlsx     cat_01092017.xlsx     dog_01092017.xlsx
 
1 members found this post helpful.
Old 01-10-2017, 07:29 AM   #12
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@rtmistler........ cause I'm trying to make it more dynamic
 
Old 01-10-2017, 07:32 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,500

Rep: Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535Reputation: 5535
what do you mean by more dynamic? rename is definitely a very flexible tool, I think you can do almost anything you need in one line.
 
2 members found this post helpful.
Old 01-10-2017, 07:34 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,317
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
Quote:
Originally Posted by trickydba View Post
@rtmistler........ cause I'm trying to make it more dynamic
Quote:
Originally Posted by trickydba View Post
I realize that I opened a thread concerning this, but it wasn't clear. Let's say I have three files:

cat.xlsx
dog.xlsx
bird.xlsx

and I wanted them to look like this:

cat_01092017.xlsx
dog_01092017.xlsx
bird_01092017.xlsx

How can I go about doing this? All files will have a .xlsx extension

With my current code:

file1=DOG_BROWN.xlsx; cfile1=DOG_BROWN; current_time=$(date "+_%Y%m%d.xlsx"); newfile1=$cfile1$current_time; mv $file1 $newfile1

It's not as dynamic as I would like it cause I have to put in manually every new file that get generated.
Then you should describe your total requirements more clearly. Besides a few compliments to those who have offered their solutions, you've not updated your problem description. All it says is that you do not wish to put in every file manually.

Noticing what pan64 added just as I was typing my response. Same point.

Last edited by rtmistler; 01-10-2017 at 07:35 AM.
 
1 members found this post helpful.
Old 01-10-2017, 07:36 AM   #15
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@rtmistler......I apologize I've only been on this site about 2-3 months so I'm still working to get the hang of things
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Append date to existing file name? miros84 Linux - Software 8 02-13-2013 01:56 AM
Compress and append date to a file using PHP Freddy49 Programming 5 11-05-2012 01:24 PM
Help, Selinux blocking append to named.log. mysteron Linux - Security 2 07-15-2008 07:01 AM
script to change file name and append the date john_551 Linux - Software 4 08-26-2006 06:56 AM
How to append today's date in a script anjaan Programming 4 06-12-2004 08:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 PM.

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