LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 12:30 PM   #31
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 trickydba View Post
@BW-userx..........let's say the file name is file.xlsx, where do I plug this in in your code above?
Code:
#!/bin/bash
TODAY=$(date +%Y%m%d)
DIR0=/home/userx
DIR2=/home/userx/testfir

mkdir -p "${DIR2}"/"${TODAY}"
find "${DIR0}" -mindepth 1 -maxdepth 1 -type f -name "*.xlsx"  -mtime -1  -exec mv -t "${DIR2}"/"${TODAY}"/ "{}" \+

do a little research on these used in find.

-mindepth 1 -maxdepth 1

they limit the search depth so you can better control where is looks to find your filess.
 
Old 12-12-2016, 12:39 PM   #32
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
oh yeah DO NOT forget to do some test run on it before you put it into production.
 
Old 12-12-2016, 01:05 PM   #33
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Absolutely!!!!! 8.))
 
Old 12-12-2016, 01:08 PM   #34
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@BW-userx......I tried your code and while I did not get any errors, it did not create a folder named with the current date and did not move the file I specified
 
Old 12-12-2016, 01:20 PM   #35
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 trickydba View Post
@BW-userx......I tried your code and while I did not get any errors, it did not create a folder named with the current date and did not move the file I specified
now would be a good time to start posting your code. while telling us your steps in setting it up and running it. all of the wheres and what fors.
 
Old 12-12-2016, 01:20 PM   #36
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I can't believe this is this hard. I tried all code provided and I either got an error or no errors but nothing was created nor moved. I do these steps in command line and get no errors:

1. cd //
2. cd dir1/dir2/foldertomakenewfolder......................folder I want to create a folder named the current date
3. mkdir $(date +%Y%m%d)...............make the directory named the current date locally

A folder was created, being named the current date BUT when trying to do this in a script(which should be like doing it in command line) I get an error. Weird!!!

Last edited by trickydba; 12-12-2016 at 01:23 PM.
 
Old 12-12-2016, 01:24 PM   #37
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 trickydba View Post
I can't believe this is this hard. I tried all code provided and I either got an error or no errors but nothing was created nor moved. I do these steps in command line and get no errors:

1. cd //
2. cd dir1/dir2/foldertomakenewfolder folder I want to create a folder named the current date
3. mkdir $(date +%Y%m%d)

A folder was created, being named the current date BUT when trying to do this in a script(which should be like doing it in command line) I get an error. Weird!!!
you need to give better explanations to what you are doing , code and steps taken to run code. showing your work. as if we were actually there watching what you are doing, so this side can better understand what is going on.
 
Old 12-12-2016, 01:29 PM   #38
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I really can't put it any easier than what I gave in my above post..........shouldn't what I do in command line be inserted in a script and executed without error?
 
Old 12-12-2016, 01:32 PM   #39
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 trickydba View Post
I really can't put it any easier than what I gave in my above post..........shouldn't what I do in command line be inserted in a script and executed without error?
what does your code look like now?
did you do this to your file first before you ran it?

Code:
chmod +x filename
./filename
then copy paste your output in your terminal into your next post along with your code in your bash file.
 
Old 12-12-2016, 01:37 PM   #40
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Yes. the file permissions of the script has been changed to an executible. Here is the code:

# Change file permissions of incoming files automatically
chmod 777 *.xlsx

exit <------ I did this to change to the user that has permissions to create a directory

cd //
cd dir1/dir2/dir3/dir4/dir5/audits
mkdir $(date +%Y%m%d)
 
Old 12-12-2016, 01:44 PM   #41
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 trickydba View Post
Yes. the file permissions of the script has been changed to an executible. Here is the code:

# Change file permissions of incoming files automatically
chmod 777 *.xlsx

exit <------ I did this to change to the user that has permissions to create a directory

cd //
cd dir1/dir2/dir3/dir4/dir5/audits
mkdir $(date +%Y%m%d)
ok let takes a sip of coffee, and sit back and relax for a second.
why did you change permissions to the files you are looking for?
why are you still doing this command line when @TenTenths and I gave you code to create a BASH script to use?
 
Old 12-12-2016, 01:45 PM   #42
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by trickydba View Post
I can't believe this is this hard. I tried all code provided and I either got an error or no errors but nothing was created nor moved. I do these steps in command line and get no errors:

1. cd //
2. cd dir1/dir2/foldertomakenewfolder......................folder I want to create a folder named the current date
3. mkdir $(date +%Y%m%d)...............make the directory named the current date locally

A folder was created, being named the current date BUT when trying to do this in a script(which should be like doing it in command line) I get an error. Weird!!!
What Error? Exactly.
Are these files always in the same location?
Code:
mkdir -pv dir1/dir2/$(date +%Y%m%d)
http://xyproblem.info/
 
Old 12-12-2016, 02:01 PM   #43
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 Habitual View Post
What Error? Exactly.
Are these files always in the same location?
Code:
mkdir -pv dir1/dir2/$(date +%Y%m%d)
http://xyproblem.info/
One I have no idea where the files are kept, I have no idea of whatever it is your using to create them. No one needs to know that but you.


ok taking from your first post.
Quote:
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.
you want to run a BASH script that creates a directory using the date created as its name. then moving files into said directory. the file types you are wanting to move where discovered later on in the thread, them being of the type, .xlsx
somewhere in this thread I learn you want to only move files
Quote:
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
so to summarize:

have script:
1. create a directory with current date.
2 move .xlsx that are less than one day old files wherever they are into freshly created directory.

steps needed.
1. created dated directory
2. find files that meet criteria.
3. move files into freshly created directory.

Code:
#!/bin/bash
TODAY=$(date +%Y%m%d)
DIR0=/home/userx
DIR2=/home/userx/testfir

mkdir -p "${DIR2}"/"${TODAY}"
find "${DIR0}" -mindepth 1 -maxdepth 1 -type f -name "*.xlsx"  -mtime -1  -exec mv -t "${DIR2}"/"${TODAY}"/ "{}" \+
Code needed for a BASH sctipt ro be copied and pasted into a fresh file then saved under whatever the user wants to call it, example myfreshscript.

now all the user has to do after changing the paths to meet his needs is,

Code:
#chmod +x myfreshscript
first setting up a test base to make sure it is doing exactly what he wants it to do.
User must first make up dummy directory and put test files within it. Make sure the BASH script leads to that dummy directory that the files are in, then add a path to where he wants the dummy dated directory to go within the script.


cd into a completely different directory that script is in then run it
Code:
./myfreshscript
then make needed changes if any before putting it into the /etc/cron.daily directory using root to do so. then double checking to be sure it is still executable.

Last edited by BW-userx; 12-12-2016 at 02:05 PM.
 
1 members found this post helpful.
Old 12-12-2016, 02:24 PM   #44
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@BW-userx.. excellent post, quoting me like a politician LOL!!!! Naw, seriously it is a great post covering everything I asked for. Only instead of all files ending in '.xlsx', I just defined a file which shouldn't harm any of your code. I created a separate BASH script, copied and pasted your code, defined the paths(which I checked several times to be sure they are right), ran the script. No errors BUT no directory created
 
Old 12-12-2016, 02:29 PM   #45
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
You will not believe this..oh your code IS correct. I copied and pasted each line of your code in the command line and MANUALLY ran each line.it does EXACTLY what I need it to do. Why in the world will it not run in a script???? This is crazy!!! Yes, the file permissions has been changed to make it an executable
 
  


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 02:04 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