Moving files into dirs corresponding to dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files into dirs corresponding to dates
# 1  
Old 06-23-2015
Moving files into dirs corresponding to dates

I am trying to find a way to move files into corresponding date files.
Code:
i=0
while read line
do
    array[ $i ]="$line"
    (( i++ ))
done < <(ls)

cd  $(echo ${array[1]})
echo ${array[1]}}
pwd
#cd "$(array[1]}"
[[ -d 2015 ]] || mkdir 2015
cd "2015"
[[ -d 02-February ]] || mkdir 02-February
[[ -d 03-March ]] || mkdir 03-March
[[ -d 04-April ]] || mkdir 04-April
[[ -d 05-May ]] || mkdir 05-May
[[ -d 06-June ]] || mkdir 06-June
[[ -d 07-July ]] || mkdir 07-July
[[ -d 08-August ]] || mkdir 08-August
[[ -d 09-September ]] || mkdir 09-September
[[ -d 10-October ]] || mkdir 10-October
[[ -d 11-November ]] || mkdir 11-November
[[ -d 12-December ]] || mkdir 12-December
cd ".."



Here is where I want to move the files. I can make it work by using something like this while in directory that contains the files:
Code:
for file in *; do
if [[ "$file" =~ 03-[0-9][0-9]-2015 ]]; then
varx=`echo $file|head -1|cut -d- -f 4|sed 's/.txt//g'`;
vary=`echo $file|head -1|cut -d ' ' -f 2|sed 's/-.*//'`;
jx=$(varz=`echo $vary`; if [[ $varz =~ $vary ]]; then echo $varz-March;fi)
echo mv "$file" /tmp/Safe\ Dirs/${array[1]}/$varx/$jx
fi
done

However, I need to to go through every directory that has date files and move them into the /tmp/Safe\ Dirs/subdir1 subdir2 subdir3 etc folders.

So we have
Code:
 /tmp/Safe\ Dirs/subdir1
/tmp/Safe\ Dirs/subdir2
/tmp/Safe\ Dirs/subdir3
/tmp/Safe\ Dirs/subdir4

and every file that corresponds to a certain month needs to be moved to the 2015/02-February etc. folder
This works, but I have to indicate the index of the array in each statement. I'm not sure how to move the files that are in each directory into the directories represented by the element of the array.

So these are a sample of the directories:
Code:
/tmp/Safe\ Dirs/vault/file-02-12-15 
/tmp/Safe\ Dirs/vault/file-03-15-15
/tmp/Safe\ Dirs/lock/file-02-12-15 
/tmp/Safe\ Dirs/lock/file-03-18-15

These need to be moved into:
Code:
/tmp/Safe\ Dirs/vault/02-February
/tmp/Safe\ Dirs/vault/03-March

and
Code:
/tmp/Safe\ Dirs/lock/02-February
/tmp/Safe\ Dirs/lock/03-March

So what happens is that I need to use these statements, etc.
Code:
 mv "$file" /tmp/Safe\ Dirs/${array[1]}}/$varx/$jx
 mv "$file" /tmp/Safe\ Dirs/${array[2]}}/$varx/$jx
 mv "$file" /tmp/Safe\ Dirs/${array[3]}}/$varx/$jx

I'm sure there is a much easier way to do this. Any suggestions?

Moderator's Comments:
Mod Comment add more code tags

Last edited by jim mcnamara; 06-23-2015 at 05:44 PM..
# 2  
Old 06-23-2015
cant you use like this
Code:
 cp /tmp/Safe\ Dirs/vault/file-02* /tmp/Safe\ Dirs/vault/02-February

# 3  
Old 06-23-2015
What shell are you using?

As a comment: spaces in file names or directory names are a problem waiting to happen.
Also, /tmp is not meant for long term storage and is often implemented as part of RAM for performance reasons. Consider parking data files elsewhere unless these are going away soon.

Depending on your shell, parameter substitution can make your code work in just a few lines.
# 4  
Old 06-25-2015
To make it simpler - I want to use an array to cd to different directories. I do want it to be in array format.
Code:
#! /bin/bash
i=0
while read line
do
    array[ $i ]="$line"
    (( i++ ))
done < <(ls)

I have tried various ways, I just get "directory not found." I assume this is because cd is a built in command. However, if I do this, I do get the directory to change
Code:
#! /bin/bash

i=0
while read line
do
    array[ $i ]="$line"
    (( i++ ))
done < <(ls)

cd  $(echo ${array[1]})
echo ${array[1]}}
pwd

Are there any ideas of how I can get it to run? At this point I can get this to work by doing cd $(echo ${array[1]}) cd $(echo ${array[2]}) cd $(echo ${array[3]}) and then typing the command below that, etc. But there are 100 elements of the array and this is too much work to cd each time.
# 5  
Old 06-25-2015
This in bash
Code:
declare -a array=( $(ls) )

does what this tries to do:
Code:
i=0
while read line
do
    array[ $i ]="$line"
    (( i++ ))
done < <(ls)

This
Code:
cd  $(echo ${array[1]})

should be:
Code:
cd  ${array[1]}

To print the whole array:
Code:
for(( i=0; i< ${#array[*]}; i++ ))
do
   echo ${array[i]}
done

That should get you further down the road.
# 6  
Old 06-25-2015
In both bash and a 1993 or later version of ksh:
Code:
declare -a array=( $(ls) )

can be simplified to just:
Code:
array=( * )

and I would strongly suggest changing:
Code:
   echo ${array[i]}

to:
Code:
   printf '%s\n' "${array[i]}"

to be safe in cases where a filename in the current directory:
  • starts with a hyphen,
  • contains one or more backslash characters, or
  • contains one or more whitespace characters.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 06-25-2015
Yes - quoting a variable is alwayds the best idea. Thanks. However I'm not that sure about globbing is a better choice than ls. If that were the case, then the use of simple ls would be completely obviated. IMO.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in files in all dir and sub dirs

Hello, I need to replace xml version='1.1' with xml version='1.0' in all xml files under /app/jenkins/ in all dir and sub dirs in my CentOS VM, I tried below command but it didn't help, looks like I'm missing a character somewhere. grep -rl "xml version='1.1'" . | xargs sed -i 's/"xml... (2 Replies)
Discussion started by: mahesh Madpathi
2 Replies

2. Red Hat

Moving files with specific dates

Hi, These are the list of files in one directory in the server : # ls -lrt total 10120 -rw-r--r-- 1 root root 4484 Jul 8 2011 install.log.syslog -rw-r--r-- 1 root root 51890 Jul 8 2011 install.log -rw------- 1 root root 3140 Jul 8 2011 anaconda-ks.cfg drwxr-xr-x 2 root root... (2 Replies)
Discussion started by: anaigini45
2 Replies

3. Debian

Problem with files/dirs deletion

Hi, The other day i installed a PHP based CMS (modx) on my shell account and noticed that i couldn't delete any of files/dirs it created after. Also, i noticed that all that stuff is owned by username-www instead of username. I tried chown, chmod and using a PHP script to do the same wti... (4 Replies)
Discussion started by: pentago
4 Replies

4. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

5. Shell Programming and Scripting

AWK help print dirs with files in it

Hi, I'm writing some start of day checks for my work. I want to check some dirs for files that have been created longer than 10 mins ago and not been transfered. I've already used a find command to write a list of files that meet this criteria to a log called sod.log i.e. ... (1 Reply)
Discussion started by: elcounto
1 Replies

6. Shell Programming and Scripting

sort retrieved data files in different dirs

Hi, I have a script to retrieve data files from server and store them in a directory on local disk. The script runs everyday as cron job. But now those files are too many so my boss wants me to put the files into different directories base on dates. Those files look like this: ... (4 Replies)
Discussion started by: leafei
4 Replies

7. Shell Programming and Scripting

Have absolute path for files in different dirs

Hi everybody. I need a command to print the absolute path of files which name starts always with a pattern (MOD03), independently on where they are in the filesystem. I have tryedls -ld ${INPUTPREFIX}/*/*/* | grep MOD03 | awk '{ print $8 }'but I have to use "/*/*/*" in this case to have the... (5 Replies)
Discussion started by: canduc17
5 Replies

8. Shell Programming and Scripting

Find most recent files in dirs and tar them up?

Hey all.. This should be simple but stoopid here can't get head around it! I have many directories, say 100 each with many files inside. I need a script to traverse through the dirs, find most recent file in each dir and add it to a tar file. I can find the files with something like for... (1 Reply)
Discussion started by: bobdung
1 Replies

9. Shell Programming and Scripting

script find files in two dirs HELP

I have a directory which is /home/mark/files/ , inside this particular I have a bunch of filles (see examples below) TST_SHU_00014460_20090302.txt TST_SHU_00016047_20090302.txt TST_SHU_00007838_20090303.txt TST_SHU_00056485_20090303.txt TST_SHU_00014460_20090303.txt... (2 Replies)
Discussion started by: fierusbentus
2 Replies

10. UNIX for Dummies Questions & Answers

I need to ls all files in 4-6 deep dirs

I need to print to file , a listing of all files below a certain directory. Example: I need to print to file a listing of all files below the etc dir (including the subdirectories) with their full path. Any ideas on how to do this with one command. Or is this something I need to do on all... (4 Replies)
Discussion started by: gforty
4 Replies
Login or Register to Ask a Question