LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2015, 11:24 AM   #1
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Rep: Reputation: 5
Question Bash Scripting: rename bunches of files using source/destination list


I know this is easy, but I'm not getting the syntax quite right.

I have a file which contains this pattern:

path/source_file1 path/destination_file1
path/source_file2 path/destination_file2
path/source_file3 path/destination_file3

I want to loop through that file (let's call it index.txt) and use the move command to move the source file to the destination file. The index.txt file contains over 200 entries.

There's only one source and one destination per line in the inedex.txt file.

I'm trying to loop thru using an array, but I'm not "getting it".

Thanks, I hope I included enough info.
 
Old 08-17-2015, 11:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please show us what you have so far and where you are stuck?
 
Old 08-17-2015, 12:16 PM   #3
bits45
Member
 
Registered: Jul 2009
Posts: 44

Original Poster
Blog Entries: 2

Rep: Reputation: 5
OH, I'm stuck on Bash 3.2

This is what the file contents might look like:

Code:
cat index.txt 

/source/dir/file /destination/dir/newfile
/source/dir/file1 /destination/dir/newfile1
/source/dir/file2 /destination/dir/newfile2

If I'm saying this correctly, I'm trying to save each row of that index.txt file as an associative array (I think?) so then I could loop thru the index.txt file (array), but "move" the elements from source_file to destination_file for each iteration of the loop. There's no white space in the file names or path, only between the source and destinations themselves.

I thought it might work something like this with the move command once I get the array's elements working right.

Code:
declare -A Array=$(cat index.txt)

for Line in ${Array[@]}
do
  mv ${Line[0]} ${Line[1]}
done
I know this is mucked up.

I know I can read in the file with read, but I suck at that too.

Last edited by bits45; 08-17-2015 at 12:20 PM.
 
Old 08-17-2015, 01:08 PM   #4
bits45
Member
 
Registered: Jul 2009
Posts: 44

Original Poster
Blog Entries: 2

Rep: Reputation: 5
Well, I accomplished the task, but without an array. I know this is too much "work". I shouldn't need awk, right?

Code:
for Line in `<index.txt`; do 

  Source=$(echo $Line | awk '{print $1}')
  Dest=$(echo $Line | awk '{print $2}')
  cp $Source $Dest && echo " Copied $Source to $Dest"

done
 
Old 08-17-2015, 01:26 PM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Edit. OP solved the issue. Awesome!

Last edited by HMW; 08-17-2015 at 01:28 PM.
 
Old 08-17-2015, 01:31 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
HMW ... you really are tired ... hehe

Why not just:
Code:
#!/bin/bash

while read source target; do
    echo -n "Source file: $sourcefile | "
    echo "Target: $target"
done < index.txt
 
2 members found this post helpful.
Old 08-17-2015, 01:35 PM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by grail View Post
HMW ... you really are tired ... hehe
I know. I will refrain from posting now :-P
 
Old 08-17-2015, 02:43 PM   #8
bits45
Member
 
Registered: Jul 2009
Posts: 44

Original Poster
Blog Entries: 2

Rep: Reputation: 5
grail. THANKS!

That worked GREAT, as if you didn't know that already.

I've never used 'read' to create multiple variables.

As expected, it was simpler than I was making it out to be.

Take care!
 
  


Reply

Tags
bash, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash scripting problem: CSV list of filenames and actual files compare; Issue with plus symbol gn000we Linux - General 2 08-05-2013 06:57 PM
[SOLVED] Bash Shell Scripting rename files pomico Programming 9 07-02-2012 01:32 PM
[SOLVED] rename files using text list carlosestensser Fuduntu 6 06-06-2012 12:33 PM
Rename files matching a list Qu3ry Programming 34 07-22-2009 06:59 PM
Bash scripting problem: Can't get a list of all files, including hidden ones oxi Programming 24 03-12-2007 06:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration