Linux - NewbieThis 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.
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.
Let's say I have a file named AUDITS_JULY.xlsx, and I want to append the current date to it so it will show AUDITS_JULY_12152016.xlsx. How is this possible.
I know how to append the current time BUT I have to leave out the extension. This is my code that I've used before in another project, but this is a different project and the filename will already have an extension:
My preference would be to append the date to the beginning but it depends on your circumstances.
There are many ways to perform string functions in bash. The easiest to understand might be the basename function which will extract the filename from the extension.
The only way I know how to do it is the code I have listed above, I know no other way and it would take too much to create the file without the extension
With all the quoting it is a little hard to read if you are not familiar with the pieces as it mixes shell with perl.
Because it is wrapped in double quotes, everything is processed by the shell first. That means the $1 for perl has to be escaped as \$1 and that the command substitution $( ... ) runs date and uses the output. That is passed to perl which then does a substitution s/// on the file name(s) given to rename
I too have reservations about how the date is represented. If you put the date at the beginning of the name, it will sort better, especially if you use +"%Y%m%d" (aka +"%F") as the format.
Last edited by Turbocapitalist; 12-15-2016 at 07:35 AM.
Reason: grammar
@michaelk................So all I have to do is plug your one line of code in? No variables have to be defined? I was saying I had to leave out the extension of the file for another project I was doing but this time the extension will be left in. So when t his file is renamed, it will not show as 'filename.xlsx.xlsx using your suggested code? Cause I tried this without any results;
I figured it would be easier to not append the date, just send the file(s) with no date, move them first then after they are in the folder I want them to be THEN append the date to all files ending with .xlsx.
@michaelk..............It would be better to just append the current date to all files with an extension of ".xlsx" in this format: AUDITS_JULY_MM_DD_YYYY.xlsx.
Yes, it's clunky, yes it's sloppy and redundant but it gets the job done till I figure out better.
Because a file looks like this after the code is ran(AUDITS_20161215.xlsx, now I need to figure out how to send files with appended dates, which of course as you know change daily. I'll open a new thread for this!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.