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.
|
 |
|
12-12-2016, 09:18 AM
|
#1
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Rep: 
|
Copy file in folder after creating it
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.
Last edited by trickydba; 12-12-2016 at 09:24 AM.
|
|
|
12-12-2016, 09:28 AM
|
#2
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Code:
mkdir -p /path/to/$(date +%Y%m%d)
would (today) create /path/to/20161212
Code:
mv whatever.file /path/to/$(date +%Y%m%d)/whatever.file
would move whatever.file to /path/to/20161212/whatever.file
Hardly complicated.
|
|
|
12-12-2016, 09:29 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
One way is to use command substitution to get the date for use in a file name.
Code:
file=$(date +"%F");
echo $file
|
|
|
12-12-2016, 09:29 AM
|
#4
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep: 
|
So far, in my script I have done the following:
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
|
|
|
12-12-2016, 09:30 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by TenTenths
Code:
mkdir -p /path/to/$(date +%Y%m%d)
would (today) create /path/to/20161212
Code:
mv whatever.file /path/to/$(date +%Y%m%d)/whatever.file
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.
|
|
|
12-12-2016, 09:32 AM
|
#6
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
almost like @TenTenths did but in one line. with a copy not move;
Code:
#!/bin/bash
mkdir -p /path/to/$(date +%Y%m%d) ; cp -v /source/file /destananation/$(date +%Y%m%d)
example
Code:
userx@SlackDaddy~/testfir>> touch testfile
userx@SlackDaddy~/testfir>> ls
testfile
userx@SlackDaddy~/testfir>> mkdir -p ~/testfir/$(date +%Y%m%d) ; cp -v ~/testfir/testfile ~/testfir/$(date +%Y%m%d)/
'/home/userx/testfir/testfile' -> '/home/userx/testfir/20161212/testfile'
userx@SlackDaddy~/testfir>> ls
20161212 testfile
userx@SlackDaddy~/testfir>> ls 20161212
testfile
BIG NOTE:
Quote:
but copying a file using the 'mv' command into a folder with the current date is.
|
You cannot copy a file using the move file command. perhaps your thinking was obscured when you wrote that.
Last edited by BW-userx; 12-12-2016 at 09:40 AM.
|
|
|
12-12-2016, 09:42 AM
|
#7
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep: 
|
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.
|
|
|
12-12-2016, 09:42 AM
|
#8
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Quote:
Originally Posted by trickydba
So far, in my script I have done the following:
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
|
1) Redundant
2) Redundant
4) Redundant
5) Redundant
Code:
#!/bin/bash
TODAY=$(date +%Y%m%d)
mkdir -p /where/you/want/to/create/folder/${TODAY}
mv /where/you/want/to/move/files/from/* /where/you/want/to/create/folder/${TODAY}
|
|
|
12-12-2016, 09:46 AM
|
#9
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by TenTenths
1) Redundant
2) Redundant
4) Redundant
5) Redundant
Code:
#!/bin/bash
TODAY=$(date +%Y%m%d) #redundant
mkdir -p /where/you/want/to/create/folder/${TODAY}
mv /where/you/want/to/move/files/from/* /where/you/want/to/create/folder/${TODAY}
|
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.
lets not let him get finger stress too by having to write all of that. pls ... 
Last edited by BW-userx; 12-12-2016 at 09:48 AM.
|
|
|
12-12-2016, 09:49 AM
|
#10
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Quote:
Originally Posted by BW-userx
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.
|
|
|
12-12-2016, 09:50 AM
|
#11
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Quote:
Originally Posted by BW-userx
lets not let him get finger stress too by having to write all of that. pls ... 
|
OP also asked for how to do it in a script.
|
|
|
12-12-2016, 09:52 AM
|
#12
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
Quote:
Originally Posted by TenTenths
OP also asked for how to do it in a script.
|
I already did that in post #6
even showed him a working example
|
|
|
12-12-2016, 09:53 AM
|
#13
|
Member
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310
Original Poster
Rep: 
|
@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.
Last edited by trickydba; 12-12-2016 at 09:54 AM.
|
|
|
12-12-2016, 09:55 AM
|
#14
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Quote:
Originally Posted by BW-userx
I already did that in post #6
even showed him a working example
|
Which also has the calling date twice problem that could theoretically cause problems as has been mentioned by Turbocapitalist in post #3.
|
|
|
12-12-2016, 09:56 AM
|
#15
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Quote:
Originally Posted by trickydba
@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.
|
|
|
All times are GMT -5. The time now is 01:04 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
|
|