LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-12-2016, 09:18 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
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.
 
Old 12-12-2016, 09:28 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
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.
 
Old 12-12-2016, 09:29 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,423
Blog Entries: 3

Rep: Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788
One way is to use command substitution to get the date for use in a file name.

Code:
file=$(date +"%F");
echo $file
 
Old 12-12-2016, 09:29 AM   #4
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-12-2016, 09:30 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,423
Blog Entries: 3

Rep: Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788Reputation: 3788
Quote:
Originally Posted by TenTenths View Post
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.
 
Old 12-12-2016, 09:32 AM   #6
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.
 
Old 12-12-2016, 09:42 AM   #7
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-12-2016, 09:42 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
Quote:
Originally Posted by trickydba View Post
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}
 
Old 12-12-2016, 09:46 AM   #9
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by TenTenths View Post
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.
 
Old 12-12-2016, 09:49 AM   #10
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 12-12-2016, 09:50 AM   #11
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 12-12-2016, 09:52 AM   #12
BW-userx
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

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by TenTenths View Post
OP also asked for how to do it in a script.
I already did that in post #6

even showed him a working example
 
Old 12-12-2016, 09:53 AM   #13
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 12-12-2016, 09:55 AM   #14
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
Quote:
Originally Posted by BW-userx View Post
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.
 
Old 12-12-2016, 09:56 AM   #15
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,497

Rep: Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570Reputation: 1570
Quote:
Originally Posted by trickydba View Post
@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.
 
  


Reply


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
Copy the List of all folder in a file richa07 Linux - Newbie 8 07-03-2013 12:03 PM
[SOLVED] how to just copy the folder not the file ytyyutianyun Linux - Newbie 2 11-19-2011 04:47 AM
i cannot copy a file to a folder in file system with my root account realbezo Linux - Newbie 4 03-03-2009 10:45 PM
Copy File While File is Creating in Disk suhas7860 Linux - General 4 04-03-2007 08:38 AM
copy a file to all subdirectories in a folder raj000 Linux - General 6 03-24-2006 03:55 AM

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

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