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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-31-2017, 12:12 AM
|
#1
|
LQ Newbie
Registered: Jan 2017
Posts: 12
Rep:
|
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;
|
|
|
01-31-2017, 12:34 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,655
|
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 .)
|
|
|
01-31-2017, 12:50 AM
|
#3
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,017
|
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.
|
|
|
01-31-2017, 01:30 AM
|
#4
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236
|
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.
|
02-04-2017, 12:35 PM
|
#5
|
LQ Newbie
Registered: Jan 2017
Posts: 12
Original Poster
Rep:
|
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
|
|
|
02-04-2017, 12:55 PM
|
#6
|
Moderator
Registered: Aug 2002
Posts: 26,112
|
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"
|
|
|
02-04-2017, 01:50 PM
|
#7
|
Senior Member
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,950
|
Does 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, fails to remove it.
Code:
rm a.a.txt.gzip.\ or rm a.a.txt.gzip.' '
will remove the file
|
|
|
All times are GMT -5. The time now is 05:32 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|