Script move and rename based on matching criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script move and rename based on matching criteria
# 1  
Old 08-26-2008
Script move and rename based on matching criteria

Hi all,

i would like to create a script that move and rename files from all the subdirectories of a given directory to others subdirectories of the same directory based on "matching" criteria in the "from" and "to" parameters.

Example:

Begin script
from /home/test/1_T_2008* move to /home/test/1_T/2008/*
from /home/test/12_B_2008* move to /home/test/12_B/2008/*
from /home/test/1765_T_2008* move to /home/test/1765_T/2008/*
and so on....
End script

N.B. All the directories are already created.

Anyone can help me?

Thank you all,
# 2  
Old 08-26-2008
For each selected file you can do something like that :
Code:
file=/home/test/1_T_20080826
src_dir=${file%/*}
src_file=${file##*/}
dst_sdir1=$(echo "${src_file}" | cut -d_ -f1-2)
dst_sdir2=$(echo "${src_file#${dst_sdir1}_}" | cut -c1-4)
dst_file=${src_file#${dst_sdir1}_${dst_sdir2}}
mv ${file} ${src_dir}/${dst_sdir1}/${dst_sdir2}/${dst_file}

Jean-Pierre.
# 3  
Old 08-26-2008
Quote:
Originally Posted by aigles
For each selected file
thank you! it works great if there's only one file in the directory /home/test...but i would like to move multiple files at once using some wildcards... is it possible?

how can i do a loop to find all the files in the /home/test directory with same criteria (1_T_2008aaa.txt and 1_T_2008abc for example) and move them in the correct directory /1_T/2008 directory?

I've also tried to solve the problem with mmv command and adding * in
file=/home/test/1_T_2008*
but not successfully...

thanks again for your help...
# 4  
Old 08-26-2008
ok, i've modified the script (also changed some path) and now it moves all the files with '1_T_2008' in the name...

how can i move the files without insert in the script a "fixed text" (avoiding to declare a fixed file; in this case file=/var/www/comune/1_T_2008*)?

here is the code:

Code:
file=/var/www/comune/1_T_2008*
src_dir=/var/www/files
src_file=${file##*/}
dst_sdir1=$(echo "${src_file}" | cut -d_ -f1-2)
dst_sdir2=$(echo "${src_file#${dst_sdir1}_}" | cut -c1-4)
dst_file=${src_file#${dst_sdir1}_${dst_sdir2}}
mv ${file} ${src_dir}/${dst_sdir1}/${dst_sdir2}/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Merge two files based on matching criteria

Hi, I am trying to merge two csv files based on matching criteria: File description is as below : Key_File : 000|ÇÞ|Key_HF|ÇÞ|Key_FName 001|ÇÞ|Key_11|ÇÞ|Sort_Key22|ÇÞ|Key_31 002|ÇÞ|Key_12|ÇÞ|Sort_Key23|ÇÞ|Key_32 003|ÇÞ|Key_13|ÇÞ|Sort_Key24|ÇÞ|Key_33 050|ÇÞ|Key_15|ÇÞ|Sort_Key25|ÇÞ|Key_34... (3 Replies)
Discussion started by: PK29
3 Replies

2. UNIX for Dummies Questions & Answers

Need script to move files based on month

Hi , I need a script which moves files based on month. Example : Apr 29 03:16 log4.txt Apr 29 03:16 log5.txt May 4 09:17 log1.txt May 4 09:17 log2.txt Move Apr files into Apr2015(Folder) Move May files into May2015(Folder). This is urgent requirement , if you can help me... (5 Replies)
Discussion started by: rockingvj
5 Replies

3. Shell Programming and Scripting

changing the file-name in a directory for all files matching a particular criteria

i have a directory which consist of multiple files out of which there are some files that has -e in their name. I want to write a script that will change all those file-name to -l example there are some files with name : file1-e.wav file2-e.wav file3-english-e.wav file-4-e.wav ... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Shell Programming and Scripting

Find and replace string matching criteria

Dear Friends, I am looking for a way to replace a string (multiple lines) starting with something and ending with something (these two values do not change) with blank. Basically I want to delete this code injection accross many sites and folders. Search Code (across files and folders) that... (2 Replies)
Discussion started by: libras
2 Replies

5. UNIX for Dummies Questions & Answers

How to fetch files right below based on some matching criteria?

I have a requirement where in i need to select records right below the search criteria qwertykeyboard white 10 20 30 30 40 50 60 70 80 qwertykeyboard black 40 50 60 70 90 100 qwertykeyboard and white are headers separated by a tab. when i execute my script..i would be searching... (4 Replies)
Discussion started by: vinnu10
4 Replies

6. Shell Programming and Scripting

Rename Script based on Pattern

Hi I am writing a Rename file Script based on the pattern , like script.sh file*2 file*xx my script should rename all the files which matches file*2 and rename them to file*xx iPattern=$1 oPattern=$2 Array=$(ls $iPattern) for i in $Array do mv $i XXXXXXX << could not get here >> ... (2 Replies)
Discussion started by: rakeshkumar
2 Replies

7. Shell Programming and Scripting

Need script to move files based on name

Hi folks, I'm new here and appreciate greatly any help. I have a bunch of files that need be moved and renamed. Fortunately, they are all in sequence... Present filename and path: /.catalog1/t76038_842-01 Move to: /.catalog1/76038-01 So, we need to drop the... (8 Replies)
Discussion started by: axslinger
8 Replies

8. Shell Programming and Scripting

making a list matching certain criteria in bash...

Hello everyone!I am trying to make a mail list(a simple .txt file)in which i put certain records that match specific criteria. Let's say that i have a(sorted by last column file)like this one: 0100567 Bla1 Lala1 100 1234567 Bla2 Lala2 80 8769029 Bla3 Lala3 70 1001007 ... (0 Replies)
Discussion started by: bashuser2
0 Replies

9. Shell Programming and Scripting

Rename/Move files and schedule the script

Hello, I'm new in the forum and in UNIX scripting, what I need is to write a simple batch script that renames or move the files back & forth from one directory to another, and then schedule the script to run on the server when the scheduled down time is, which is on Thursdays at 8pm and during... (5 Replies)
Discussion started by: dannyghost
5 Replies

10. Shell Programming and Scripting

deleting files and folders matching criteria

Hello, I'm spendind hours trying to figure out how a script could remove files and folders older than 30days in a given volume (/dataVolumes/Booba.1.0). Within this volume, all users have their personal folder that starts with "RC-..", so the script should skip them for deletion. I will... (4 Replies)
Discussion started by: H3001
4 Replies
Login or Register to Ask a Question