0

Lets say i have multiple filesnames e.g. R014-20171109-1159.log.20171109_1159. I want to create a shell script which creates for every given date a folder and moves the files matching the date to it. Is this possible?

For the example a folder "20171109" should be created and has the file "R014-20171109-1159.log.20171109_1159" on it.

Thanks

4 Answers4

0

Below commands can be put in script to achieve this,

Assign a variable with current date as below ( use --date='n day ago' option if need to have an older date).

if need to get it from File name itself, get files in a loop then use cut command to get the date string,

dirVar=$(date +%Y%m%d) --> for current day,

dirVar=$(date +%Y%m%d --date='1 day ago') --> for yesterday,

dirVar=$(echo $fileName | cut -c6-13)   or
dirVar=$(echo $fileName | cut -d- -f2) --> to get from $fileName

Create directory with the variable value as below, (-p : create directory if doesn't exist.)

mkdir -p ${dirVar}

Move files to directory to the directory with below line,

mv *log.${dirVar}* ${dirVar}/
Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
0

Since you want to write a shell script, use commands. To get date, use cut cmd like ex:

cat 1.txt 
R014-20171109-1159.log.20171109_1159
 cat 1.txt | cut -d "-" -f2

Output

20171109 

is your date and create folder. This way you can loop and create as many folders as you want

Ajay2588
  • 527
  • 3
  • 6
0

Its actually quite easy(my Bash syntax might be a bit off) -

for f in /path/to/your/files*; do

## Check if the glob gets expanded to existing files.
## If not, f here will be exactly the pattern above
## and the exists test will evaluate to false.
[ -e "$f" ] && echo $f > #grep the file name for "*.log." 
#and extract 8 charecters after "*.log." .
#Next check if a folder exists already with the name of 8 charecters. 
#If not { create}
#else just move the file to that folder path

break
done

Main idea is from this post link. Sorry for not providing the actual code as i havent worked anytime recently on Bash

Naveen KH
  • 153
  • 2
  • 11
0

This is a typical application of a for-loop in bash to iterate thru files. At the same time, this solution utilizes GNU [ shell param substitution ].

for file in /path/to/files/*\.log\.*
do

  foldername=${file#*-}
  foldername=${foldername%%-*}
  mkdir -p "${foldername}" # -p suppress errors if folder already exists
  [ $? -eq 0 ] && mv "${file}" "${foldername}" # check last cmd status and move

done
sjsam
  • 21,411
  • 5
  • 55
  • 102