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.
I need to be able, from command line to create a folder with just the current date, then copy a file into it. Would can I achieve this? I'm putting this into a script so that this will be done daily. Making the directory in the current folder is not a problem, but copying a file using the 'mv' command into a folder with the current date is.
1. cd //
2. cd to where I want to create the folder
3. used the 'mkdir' command to created a folder named with the current date
4. cd //
5. cd to folder with the files to move(mv)
6. now need to move the files(only 2) into the directory that was just made with the current date. THIS IS WHERE IM LOST
would move whatever.file to /path/to/20161212/whatever.file
Hardly complicated.
Though there is the potential for a race condition doing it with two separate calls to "date". The day, month, or year could potentially roll over between the first one and the second one.
Im not a linux expert but I see a potential issue. The 'mkdir' command is fine BUT I want to move the file into the folder with the CURRENT date, not just moving a file to a certain named folder because remember each day this script is ran, it will create a folder named with the current date. I want to use the 'mv' command to delete it from the initial folder it was in.
1. cd //
2. cd to where I want to create the folder
3. used the 'mkdir' command to created a folder named with the current date
4. cd //
5. cd to folder with the files to move(mv)
6. now need to move the files(only 2) into the directory that was just made with the current date. THIS IS WHERE IM LOST
If he runs it everyday then that (date +%Y%m%d) is going to change everyday as well. keeping it a simple
Code:
mkdir /path to/$(date +%Y%m%d) ; cp or mv /path to source of file/file /destantion/$(date +%Y%m%d)
will work because it is all done at the same time each day. so no files should get screwed up in where they are put.
And if your mkdir runs at 23:59:59 and the cp runs at 00:00:00 YOUR version will have the same problem as mentioned above, calling the date command twice where it'll pick up different dates. But this is linux, so whatever you think is right works for you. Also calling date ONCE has less processor overhead.
@TenTenths......... what if I have two files I need to move instead of one? I don't want to send all files in the folder. This script will be ran @ 12:30 am so there should not be a problem time wise.
@TenTenths......... what if I have two files I need to move instead of one? I don't want to send all files in the folder. This script will be ran @ 12:30 am so there should not be a problem time wise.
Use the mv line twice, once for each file, or make use of any of BW-userx's solutions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.