LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-25-2017, 07:55 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Change appended date on filename


I have this code:
Code:
# Define variables
current_date=$(date "+%m%d%Y")
windows_folder="/dir/home/dir/reports/dir/dir/$current_date"

# Make folder
mkdir -p "$windows_folder"

#Copy all .DAT files to current dated folder in MDM folder
cp *.DAT /dir/home/dir/reports/dir/dir/$current_date

# Append current date to all files, move files to Windows shared folder
for file1 in *[a-z].xlsx
do
  mv "$file1" "$windows_folder/${file1%.*}${current_date}.xlsx"
done
That sends all files with an extension '.xlsx' to a folder dated with today's date. Example, in folder '01292017' are several files like this: TODAY_REPORT01292017. I like the way the folder is named with the current date (01292017), but I would like the files to be as such 'TODAY_REPORT_01292017'. How is this possible?
 
Old 01-25-2017, 08:02 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Change this:
Code:
mv "$file1" "$windows_folder/${file1%.*}${current_date}.xlsx"
To this:
Code:
mv "$file1" "$windows_folder/${file1%.*}_${current_date}.xlsx"
 
2 members found this post helpful.
Old 01-25-2017, 08:14 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I feel you did not make much of an attempt to solve this on your own. You need to write a smaller play script and play with the variables so you can better understand what limitations there might be.

Another thing you might consider for dates when creating folders or appending to similarly named files is that if you use the format 'yyyymmdd' it can actually be sorted for you easily
 
1 members found this post helpful.
Old 01-25-2017, 08:19 AM   #4
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@grail.........actually I have tried several attempts to resolve this issue. If any would have solved my issue I would have posted it. I apologize for not posting my attempts. I will be sure to do it in the future.
 
Old 01-25-2017, 08:20 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
there was another thread: http://www.linuxquestions.org/questi...es-4175597103/ with solution(s). It looks quite similar.
 
1 members found this post helpful.
Old 01-25-2017, 08:25 AM   #6
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
This code has resolved my issue:
Code:
mv "$file1" "$windows_folder/${file1%.*}_${current_date}.xlsx"
Thank you for everyone's help!!
 
  


Reply



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
[SOLVED] Move files with appended date using SCP trickydba Linux - Newbie 26 12-16-2016 01:16 PM
"How to rename files with current date stamp appended into name using shell script?" nandi Linux - Newbie 4 10-05-2011 10:36 AM
Why is "\r" appended to my filename and does it matter? linuxneub Linux - Newbie 10 06-10-2011 02:10 AM
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
Script to change date in filename minus one day ft77 Linux - General 3 06-02-2006 09:56 AM

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

All times are GMT -5. The time now is 09:02 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