Copy files based on specific word in a file name & its extension and putting it in required location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files based on specific word in a file name & its extension and putting it in required location
# 1  
Old 10-20-2016
Copy files based on specific word in a file name & its extension and putting it in required location

Hello All,
Since i'm relatively new in shell script need your guidance.
I'm copying files manually based on a specific word in a file name and its extension and then moving it into some destination folder.
so if filename contains hyr word and it has .md and .db extension; it will move to TUM/HYR folder; if extension is .txt it should convert using dd command to edcdic and move it to TUM/HYR
if filename contains par word and it has .md and .db extension; it will move to TUM/par folder; if extension is .txt it should convert using dd command to edcdic and move it to TUM/par
if filename contains mar word and it has .md and .db extension; it will move to TUM/mar folder; if extension is .txt it should convert using dd command to edcdic and move it to TUM/mar

I have a folder which contains multiple sub folder.
Main folder name is MUM
sub folders are (there are multiple folder, but just for testing i have mentioned few folders only; script should be for any numbers of folder)
Code:
HYR
PAR
MAR
SAR

Now i have few files in the above subfolder. File name are
Code:
ttt_hyr_20162010.md
ttt_hyr_20162010.db
ttt_hyr_20162010.txt

ttt_par_20162010.md
ttt_par_20162010.db
ttt_par_20162010.txt

ttt_mar_20162010.md
ttt_mar_20162010.db
ttt_mar_20162010.txt

ttt_sar_20162010.md
ttt_sar_20162010.db
ttt_sar_20162010.txt

so for example the folder structure and the files in it will look like below
Code:
MUM/HYR/ttt_hyr_20162010.md
MUM/HYR/ttt_hyr_20162010.db
MUM/HYR/ttt_hyr_20162010.txt
MUM/PAR/ttt_par_20162010.md
MUM/PAR/ttt_par_20162010.db
MUM/PAR/ttt_par_20162010.txt

and so on..

I simply want to create a shell script to copy files from MUM/HYR to TUM/HYR , MUM/PAR to TUM/PAR, MUM/MAR to TUM/MAR and MUM/SAR to TUM/SAR folders based on a word in a filename and its extension

1. The script will read the MUM folder and its subfolders(HYR) and the file name and from that file name it will read hyr word and copy that particular file which has particular word hyr and extension .md and .db and put it into the output folder(TUM/HYR).

2. Same things should happen for other subfolder. The script will read the MUM folder and its subfolders called PAR and the file name and from that file name it will read par word and copy that particular file which has particular word par and extension .md and .db and put it into the output folder called TUM/PAR.

3. For .txt file in those particular subfolder; the files should get converted from ascii to ebcdic and then move to that particular output subfolder based on the particular word in a file hyr and

put that in TUM/HYR folder

4. For .txt file in those particular subfolder; the files should get converted from ascii to ebcdic and then move to that particular output subfolder based on the particular word in a file par and

put that in TUM/PAR folder

After every file copy error and success log should be created

After the Script is executed the output folder will look like below

Code:
TUM/HYR/ttt_hyr_20162010.md
TUM/HYR/ttt_hyr_20162010.db
MUM/HYR/ttt_hyr_20162010.dat
TUM/PAR/ttt_par_20162010.md
TUM/PAR/ttt_par_20162010.db
TUM/HYR/ttt_hyr_20162010.dat

and so on..

below is the code im in need of
Code:
#!/bin/bash
DATESTMP="`date +%m%d%y%H%M`"
OGIT="/usr/local/testing/log.${DATESTMP}"
touch $OGIT
echo " SCRIPT STARTED AT `date` " >$OGIT
chmod 666 "$OGIT"
MRPT=/usr/local/resting




echo " ############# START OF AUTOMATION SCRIPT FOR MOVING FILES at `date` ################# "

cd MUM/HYR
if [filename contains hyr and has extension .md and db]; then
cp MUM/HYR/*.md TUM/HYR 
cp MUM/HYR/*.db TUM/HYR

if [filename contains hyr and has extension .txt]; then
dd  if=text.ascii of=text.ebcdic conv=ebcdic
cp MUM/HYR/*.txt TUM/HYR 

cd MUM/PAR
if [filename contains hyr and has extension .md and db]; then
cp MUM/PAR/*.md TUM/HYR 
cp MUM/PAR/*.db TUM/HYR


if [filename contains par and has extension .txt]; then
dd  if=text.ascii of=text.ebcdic conv=ebcdic
cp TUM/PAR/*.txt TUM/PAR


echo " ############# END OF AUTOMATION SCRIPT FOR MOVING FILES at `date` ################# "

The script should look for all the sub folders and copy files of particular word in that file and its extension and put it into output or destination folder.

Last edited by Don Cragun; 10-23-2016 at 11:30 PM.. Reason: Change HTML tags to CODE tags; add several CODE tags; add lots of ICODE tags.
# 2  
Old 10-20-2016
This might get you started:

Code:
find MUM/HYR -type f -name "*hyr*" \( -name "*.db" -o -name "*.md" \) -print0 |
  xargs -r -0 cp -t TUM/HYR

cd MUM/HYR
for file in *hyr*.txt
do
   [ -f "$file" ] || continue
   dd if="$file" of="${file}.tmp" conv=edcdic
   mv "${file}.tmp" ../../TUM/HYR/"$file"
done
cd ../..

The [ -f "$file" ] || continue line above traps when not *hyr*.txt files exist in the source folder.
-r option of xargs stops running the cp command when input is empty (no files detected by find command).
# 3  
Old 10-20-2016
Thanks Chubler for the quick response.
But the above script is only looking specifically into MUM/HYR folder...but I'm looking out for a script which will look the main folder MUM and then all the subfolders and then based on the word in the filename and extension the file will be copied to destination folder...
Pls refer files names and it's extension, its related subfolders as well...
So this script will be considered as a automated scripts where in if it has any subfolders and files in it, files will be copied based on above logic and copied to destination folders..
Also once one by one copying is done error log or success log shud be created

Last edited by Don Cragun; 10-20-2016 at 10:24 PM.. Reason: Remove duplicated text.
# 4  
Old 10-20-2016
So what we should do is take those two conversion scripts that work for HYR and extend them to work on your 4 various types.

Using a for loop should get us there:

Code:
for typ in hyr par mar sar
do
  TYP=$(echo $typ | tr '[:lower:]' '[:upper:]')

  find MUM -type f -name "*${typ}*" \( -name "*.db" -o -name "*.md" \) -print0 |
  xargs -r -0 cp -t TUM/${TYP}

  find MUM -type f -name "*${typ}*.txt" -print0 | while read file
  do
      dd if="$file" of="${file}.tmp" conv=edcdic
      dest=TUM/${TYP}/${file##*/}
      mv "${file}.tmp" "$dest"
  done
done 2>> ${OGIT}

Here we are also appending stderr to your OGIT logfile. If you are after a log of what was done (not just the failed stuff) consider adding the -v (verbose) option to the cp and mv commands above.
# 5  
Old 10-21-2016
The challenge is I don't hv 4 there , I almost have more that 35..
HYR PAR MAR SAR & so on
This is a challenge for me...so the script will check in every subfolders and will search a word hyr in HYR folder and will check the .db and .md and then copy in destination folder.

Last edited by prajaktaraut; 10-21-2016 at 08:57 AM..
# 6  
Old 10-21-2016
Are there ONLY those files that are to be transferred, or others as well that should remain? Is the file name structure always ccc_TYP_DATE.EXT?
And, what bash version do you run?

Does the (uppercase) directory name always coincide with the filename's three char (lower case) TYP fragment?

Last edited by RudiC; 10-21-2016 at 04:01 PM..
# 7  
Old 10-21-2016
Based on a few assumptions as answers to above questions, try
Code:
for i in MUM/*/*
  do    EXT=${i#*.}
        FP=${i%.*}
        [ $EXT == "txt" ] && { CV="conv=ebcdic"; EXT="dat"; } || CV=""
        echo dd if=$i of=${FP/MUM/TUM}.$EXT $CV
  done
dd if=MUM/HYR/ttt_hyr_20162010.db of=TUM/HYR/ttt_hyr_20162010.db
dd if=MUM/HYR/ttt_hyr_20162010.md of=TUM/HYR/ttt_hyr_20162010.md
dd if=MUM/HYR/ttt_hyr_20162010.txt of=TUM/HYR/ttt_hyr_20162010.dat conv=ebcdic
dd if=MUM/MAR/ttt_mar_20162010.db of=TUM/MAR/ttt_mar_20162010.db
dd if=MUM/MAR/ttt_mar_20162010.md of=TUM/MAR/ttt_mar_20162010.md
dd if=MUM/MAR/ttt_mar_20162010.txt of=TUM/MAR/ttt_mar_20162010.dat conv=ebcdic
dd if=MUM/PAR/ttt_par_20162010.db of=TUM/PAR/ttt_par_20162010.db
dd if=MUM/PAR/ttt_par_20162010.md of=TUM/PAR/ttt_par_20162010.md
dd if=MUM/PAR/ttt_par_20162010.txt of=TUM/PAR/ttt_par_20162010.dat conv=ebcdic
dd if=MUM/SAR/ttt_sar_20162010.db of=TUM/SAR/ttt_sar_20162010.db
dd if=MUM/SAR/ttt_sar_20162010.md of=TUM/SAR/ttt_sar_20162010.md
dd if=MUM/SAR/ttt_sar_20162010.txt of=TUM/SAR/ttt_sar_20162010.dat conv=ebcdic

You may want to play with dd's status=xxx operand to influence the amount of info printed to stderr...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all files of a particular name & extension except one file.

I wish to delete all files that starts with "body<any number of digits>.xml" except body65.xml on Linux 7 bash shell So, from the below files body64.xml body.sh body65.xml body655.xml body565.xml body66.xml hello65.xml My command should delete all files except the below. body.sh... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Find and Copy file of specific location

Dear All, I need to transfer all files present in one location to another but those files should be of specific extension like. Find and copy all files of extension .xls, .pdf, .txt from location usr/tmp to location /per/Treat (6 Replies)
Discussion started by: yadavricky
6 Replies

3. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

4. Shell Programming and Scripting

How to copy files from one location to another based on a priority?

Hi Gurus, I am a newbie to shell scripting and I am facing a problem right now.I have to automate the copy of files based on a priority.The scenario is as below: 1) There will be files from Mon-Fri with Mon file being named as abc_def_01_YYYYMMDD and Tue file being abc_def_02_YYYYMMDD and so... (4 Replies)
Discussion started by: vikramgk9
4 Replies

5. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

6. Shell Programming and Scripting

How to find a word and move it a specific location in xml file using perl?

Hi friends, I have one XML file having below structure :- INput XML file :- <?xml version="1.0" encoding="UTF-8"?> <START> <A=value1> <attr name1="a1"> </A> <B=value2> <attr name2="b1"> <attr name3="c1"> </B> </START> output xml file should be === (3 Replies)
Discussion started by: harpal singh
3 Replies

7. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

8. UNIX for Dummies Questions & Answers

how to copy files and record original file location?

:EDIT: I think my post name should have been labeled: how to copy files and record original file location. not "retain". Hello, this is my first post! I searched the forums a lot before posting, but was unable to answer my question. Here's my problem: There are several hundred text files... (4 Replies)
Discussion started by: willie8605
4 Replies

9. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

10. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies
Login or Register to Ask a Question