LinuxQuestions.org
Review your favorite Linux distribution.
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, 08: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, 09:02 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,019

Rep: Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275
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, 09:14 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
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, 09: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, 09:20 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
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, 09: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


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

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

All times are GMT -5. The time now is 07:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration