LinuxQuestions.org
Help answer threads with 0 replies.
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:59 AM   #16
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193

Quote:
Originally Posted by TenTenths View Post
Which also has the calling date twice problem that could theoretically cause problems as has been mentioned by Turbocapitalist in post #3.
how does that cause a problem? it is ran all at once. the date will not change that fast.

because the date is the same it uses that same date to get the name of that directory it just created.

Quote:
I'm putting this into a script so that this will be done daily.
meaning once a day.

Last edited by BW-userx; 12-12-2016 at 10:01 AM.
 
Old 12-12-2016, 10:02 AM   #17
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by BW-userx View Post
how does that cause a problem? it is ran all at once. the date will not change that fast.
Theoretically it COULD change that fast, in practical terms it's unlikely. But why take the risk for the sake of a bit of good scripting that also reduces the number of external program calls. Your choice though. I know what I do in my production scripts.
 
Old 12-12-2016, 10:02 AM   #18
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Quote:
Originally Posted by BW-userx View Post
how does that cause a problem? it is ran all at once. the date will not change that fast.

because the date is the same it uses that same date to get the name of that directory it just created.

meaning once a day.
if it had a time stamp with seconds on it yes that will diffidently cause problems.
 
Old 12-12-2016, 10:04 AM   #19
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Quote:
Originally Posted by TenTenths View Post
Theoretically it COULD change that fast, in practical terms it's unlikely. But why take the risk for the sake of a bit of good scripting that also reduces the number of external program calls. Your choice though. I know what I do in my production scripts.
ok if it is done at 0000 then a second goes by then the date will change, then an if statemnt will have to be added to check that. then more coding will be needed, yes.
 
Old 12-12-2016, 10:05 AM   #20
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by BW-userx View Post
ok if it is done at 0000 then a second goes by then the date will change, then an if statemnt will have to be added to check that. then more coding will be needed, yes.
An if statement that can TOTALLY be avoided by assigning the current date that the script was initiated at as a variable at the start. Oh wait, that's what I did!

Last edited by TenTenths; 12-12-2016 at 10:07 AM. Reason: ;) To show that I'm amused by this now.
 
Old 12-12-2016, 10:08 AM   #21
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Quote:
Originally Posted by TenTenths View Post
An if statement that can TOTALLY be avoided by assigning the current date that the script was initiated at as a variable at the start. Oh wait, that's what I did!
what this?
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}
well I guess you did, smarty pants. bravo!
you were thinking eggs, while I was thinking toast.
Problem solved. lets move on..
 
Old 12-12-2016, 10:15 AM   #22
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by BW-userx View Post
well I guess you did, smarty pants. bravo!
Thank you, thank you, thank you, you've made me the happiest admin in the world

Quote:
Originally Posted by BW-userx View Post
you were thinking eggs, while I was thinking toast.
Problem solved. lets move on..
I just happened to have been doing a LOT of this kind of scripting recently so when it was clear that the OP needed specific scripting examples I was pretty much there.
 
Old 12-12-2016, 10:19 AM   #23
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@TenTenths...... I tried to use your code mentioned above but there seems to be a problem with your code in the line where 'mkdir' starts. I even navigated to the folder where the directory is being created and still got an error
 
Old 12-12-2016, 10:21 AM   #24
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I could've sworn I have seen a command that uses -1, meaning less than a day old to use to copy the file into a directory less than a day old but I don't remember it
 
Old 12-12-2016, 10:23 AM   #25
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Quote:
Originally Posted by trickydba View Post
@TenTenths...... I tried to use your code mentioned above but there seems to be a problem with your code in the line where 'mkdir' starts. I even navigated to the folder where the directory is being created and still got an error
are you one the system side? if yes, then give it root permissions?

where is the bash file being run?

if on the user side trying to do stuff on the system side. root permissions needed.

if you dump it in your cron.daily it should work without sudo before the mkdir.

if you need to give it sudo permissons, then give the 'user'/owner of that file sudo passwd none in your sudoers file to get past having to enter a password each time it is ran.

Code:
!#/bin/bash

sudo mkdir /somewhere on the system side/

Last edited by BW-userx; 12-12-2016 at 10:28 AM.
 
Old 12-12-2016, 10:26 AM   #26
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,413

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
In addition to the good advice from BW-userx, if you're calling things from the cron then you may need to specify the full paths to things like mkdir and date. For example in CentOS:

/bin/mkdir
/bin/date
/bin/mv
 
Old 12-12-2016, 10:30 AM   #27
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I had permissions issues that have since been resolved
 
Old 12-12-2016, 10:38 AM   #28
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Quote:
Originally Posted by trickydba View Post
I had permissions issues that have since been resolved
something like this to find old files

Code:
find . -mtime -1 -exec cp -a --parents -t /somefolder "{}" \+
man find and cp
to see what the arguments mean. -mtime -1 -exec cp -a --parents -t

Last edited by BW-userx; 12-12-2016 at 10:42 AM.
 
Old 12-12-2016, 10:52 AM   #29
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,239

Rep: Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193Reputation: 2193
Code:
#!/bin/bash
TODAY=$(date +%Y%m%d)
#search area
DIR0=/home/userx/testfir
#Destination area
DIR1=
mkdir -p "${DIR1}"/"${TODAY}"
find "${DIR0}" -type f -mtime +1  -exec mv -vt "${DIR1}"/"${TODAY}"/ "{}" \+

Code:
userx@SlackDaddy~/bin>> ./mkandmv
'/home/userx/testfir/logout' -> '/home/userx/testfir/20161212/logout'
'/home/userx/testfir/JustcreatedTextFile.txt' -> '/home/userx/testfir/20161212/JustcreatedTextFile.txt'
'/home/userx/testfir/mbr' -> '/home/userx/testfir/20161212/mbr'
'/home/userx/testfir/makePlayList' -> '/home/userx/testfir/20161212/makePlayList'

you are going to have to use your own paths, and change the +1 to -1 , and you might want to remove the v in the mv command to not have it verbose.

Last edited by BW-userx; 12-12-2016 at 11:42 AM.
 
Old 12-12-2016, 12:19 PM   #30
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@BW-userx..........let's say the file name is file.xlsx, where do I plug this in in your code above?
 
  


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 06:02 AM.

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