LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-31-2017, 12:12 AM   #1
EVL
LQ Newbie
 
Registered: Jan 2017
Posts: 12

Rep: Reputation: Disabled
Append modified-date to filename before extension -- ONLY IF THERE'S AN EXTENSION


I've found lots of info online about appending date to filename before the extension, but none that do so only if there's an extension.
& if there's no extension, it goes at the end of file name.

This is how far I got.

for FILE in *; do
EXT=${FILE#*.};
NAME=${FILE%%.*};
mv "$FILE" "$NAME$(date --reference "$FILE" '+%d%m%y').$EXT";
done

that works provided there's an extension and the only . in filename is right before the extension.

It doesn't work if there's no extension or if there's another . in the filename.

Ideas for making this work for filenames with or without an extension ..
& with or wtihout a . elsewhere in filename?

thanks much;
 
Old 01-31-2017, 12:34 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
see man test, and check -n or -z for example (that is for existing EXT).
see man bash, look for parameter expansion, especially ## (for several .)
 
Old 01-31-2017, 12:50 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,818

Rep: Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083Reputation: 3083
Having no extension is a no brainer, but you will need someway to identify what you are considering a valid extension? If there is a period in the file name and no extension, I see no real way
of not placing the date inside the file name unless you have an acceptable list of extensions.
 
Old 01-31-2017, 01:30 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,713

Rep: Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549
A long time ago I used to worry about things like this. Then I just decided the date would go on the end regardless - and told people to live with it.
Extensions have no intrinsic value in Linux.
 
2 members found this post helpful.
Old 02-04-2017, 12:35 PM   #5
EVL
LQ Newbie
 
Registered: Jan 2017
Posts: 12

Original Poster
Rep: Reputation: Disabled
pan64, grail, syg00,

Thanks to all who replied. This is my third attempt to reply .. other two were eaten by glitches <grr> ..

I will try the suggestions.

I especially like syg00's suggestion, "deal with it approach" when the date is appended to the extension. This is especially time-efficient, unless (like me) you don't know which OS you'll be using in a month, functioning as I do with highly improvised hardware configurations.

grail's
> "If there is a period in the file name and no extension, I see no real way
> of not placing the date inside the file name unless you have an acceptable list of extensions."
.. is spot-on, if I can think of a complete list ..

pag64
> see man test, and check -n or -z for example (that is for existing EXT).
> see man bash, look for parameter expansion, especially ## (for several .)
Good idea.

EVL
 
Old 02-04-2017, 12:55 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,505

Rep: Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113
Not a real solution but something that my work group has used for many years which works for us and is difficult to convey to others of the advantages is putting the date first as YYYYMMDD. Because of how alphanumeric sorting works using YYYYMMDD_file-name.ext makes sorting by name "automatic"
 
Old 02-04-2017, 01:50 PM   #7
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,887

Rep: Reputation: 467Reputation: 467Reputation: 467Reputation: 467Reputation: 467
Does
Code:
a.a.txt.gzip.' '
have an extension?
It's valid in that
Code:
touch a.a.txt.gzip.' '
creates a file called a.a.txt.gzip. (according to ls).
However,
Code:
rm a.a.txt.gzip.
fails to remove it.

Code:
rm a.a.txt.gzip.\  or rm a.a.txt.gzip.' '
will remove the file
 
  


Reply

Tags
date


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
Find files that they have the same filename but different extension. dimitriauxio Linux - Newbie 2 09-11-2013 01:10 PM
Convert filename extension to lowercase jakev383 Programming 6 12-07-2008 06:24 AM
How to get filename extension makowka Programming 7 04-30-2007 05:32 PM
redirect to same filename without extension geekpie Linux - Software 1 10-31-2006 10:12 AM
Filename extension question BajaNick Linux - General 2 07-05-2004 05:07 PM

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

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