List files with date, create directory, move to the created directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List files with date, create directory, move to the created directory
# 1  
Old 01-26-2016
List files with date, create directory, move to the created directory

Hi all,
i have a folder, with tons of files containing as following,
on /my/folder/jobs/
some_name_2016-01-17-22-38-58_some name_0_0.zip.done
some_name_2016-01-17-22-40-30_some name_0_0.zip.done
some_name_2016-01-17-22-48-50_some name_0_0.zip.done
and these can be lots of similar files,
some_name_2016-01-18-22-40-30_some name_0_0.zip.done
some_name_2016-01-18-22-48-50_some name_0_0.zip.done
and i have also on the same folder tons of files containing, the same name, but the date and time changes, depending on the time and date it was done,
now as it is impossible to check if a file exist on that folder,
i would like to create a script that will do the following;
list all file with a specific date
create a folder with this specific date
move all files with this date to the new created folder,
and i need to do this for every different date.

i thought some thing like this, but not working at all, it work only if i specify the date, but i dont wont to do it manually, the script must understand what to do,

so here my script;
Code:
cd /my/folder/jobs/
files="2016-01-17"
newdir="2016-01-17/"
 for i in {$files};
 do 
    
    newdir=$(echo /my/folder/jobs/$newdir)
    if ! [ -d $newdir ]; then
        echo Directory $newdir does not exist.  Creating $newdir.
        mkdir -p $newdir;
    fi
    mv "*$files*" $newdir;
done

Thanks in advance for any suggestions.

Last edited by charli1; 01-26-2016 at 02:45 PM..
# 2  
Old 01-26-2016
Please use code tags for data (file listing) as well!

What's the contents of $3? What's the meaning of "not working at all"?
# 3  
Old 01-26-2016
Quote:
Originally Posted by RudiC
Please use code tags for data (file listing) as well!

What's the contents of $3? What's the meaning of "not working at all"?

Well, i'm away from scripts some time on,
so please modify the script as you better think and i will test it on my envirement, "not working at all" means the script as it is wright now does nothing.

BTW edited the first script,

Thank in advance.
# 4  
Old 01-26-2016
One reason for failure might be the space in the file names which will make the for loop fail.
# 5  
Old 01-26-2016
Quote:
Originally Posted by RudiC
One reason for failure might be the space in the file names which will make the for loop fail.
Ok, can you modify it or create a new one, to create a new folder for every date, and move those files to the referral folder?
Thanks.
# 6  
Old 01-26-2016
Would this do (needs a recent shell)?
Code:
ls some* | while read FN; do DIR=${FN#*_*_}; DIR=${DIR%-*-*-*_*}; mkdir -p $DIR; mv "$FN" $DIR; done

This User Gave Thanks to RudiC For This Post:
# 7  
Old 01-26-2016
Quote:
Originally Posted by RudiC
Would this do ?
Code:
ls some* | while read FN; do DIR=${FN#*_*_}; DIR=${DIR%-*-*-*_*}; mkdir -p $DIR; mv "$FN" $DIR; done

will test this and let you know,
Quote:
Originally Posted by RudiC
(needs a recent shell)?
!/bin/sh will do the job

thank a lot.

---------- Post updated at 02:00 PM ---------- Previous update was at 01:53 PM ----------

SOLVED

Last edited by charli1; 01-26-2016 at 03:00 PM.. Reason: Solved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

3. Shell Programming and Scripting

How I can find the last file created and move it to a directory?

I have this situation /u03/app/banjobs> ls -ltr icg* 82 Jun 12 10:37 iicgorldi_2419186.log 56810484 Jun 17 10:35 icgorldi_2421592.xml 2859 Jun 17 10:35 icgorldi_2421592.lis - 125 Jun 17 10:35 icgorldi_2421592.log 82 Jun 12 10:37 iicgorldi_2419187.log ... (8 Replies)
Discussion started by: Bernardo Jarami
8 Replies

4. UNIX for Dummies Questions & Answers

Find a list of files in directory, move to new, allow duplicates

Greetings. I know enough Unix to be dangerous (!) and know that there is a clever way to do the following and it will save me about a day of agony (this time) and I will use it forever after! (many days of agony saved in the future)! Basically I need to find any image files (JPGs, PSDs etc)... (5 Replies)
Discussion started by: Clyde Lovett
5 Replies

5. Shell Programming and Scripting

Please help list/find files greater 1G move to different directory

I have have 6 empty directory below. I would like write bash scipt if any files less "1000000000" bytes then move to "/export/home/mytmp/final" folder first and any files greater than "1000000000" bytes then move to final1, final2, final3, final4, final4, final5 and that depend see how many files,... (6 Replies)
Discussion started by: dotran
6 Replies

6. Shell Programming and Scripting

Move files in a list to another directory

I have a number of files in a directory that can be grouped with something like "ls | grep SH2". I would like to move each file in this list to another directory. Thanks (4 Replies)
Discussion started by: kg6iia
4 Replies

7. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

8. Emergency UNIX and Linux Support

How to move files from a directory which falls between Date Range?

Hi All, I am trying to to move files from a directory to another which falls from Current day - 7 days. The files are in zipped format with dates appended on it. Can you pls help me as this came as a immediate change before the production Release planned next week. Pls let me know if... (11 Replies)
Discussion started by: dsfreddie
11 Replies

9. Shell Programming and Scripting

Script to move files to a directory according to date

hi all, here is the description to my problem. input parameter: $date1 based on the date i need to select three files starting with audit.log* based on its modified date, a date before, a date after(if its exists). We need to compare the input date to modified date of the file. And then... (3 Replies)
Discussion started by: ashrocks
3 Replies

10. UNIX for Dummies Questions & Answers

To list all the files created today with directory path

Hi, Can any one tell the command to list all the files that are created as of today from all the directories? The Command "ls -ltR" is listing all the files. But I want the list of files that has been created as of today along with the directory path:) Thank you in advance.:) Regards,... (4 Replies)
Discussion started by: meetusha.b
4 Replies
Login or Register to Ask a Question