LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2016, 01:38 PM   #1
Firmbyte
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Rep: Reputation: Disabled
Move files listed in col 1 of csv to new location renaming them to name in col 2 of csv


I'm trying to copy & rename files between an Ubuntu Server and a mounted Windows share.
In the Windows mounted share, I've got a csv file with 2 columns: guiid and name, whcih comes from a MySQl database that lists the files. but the files are only stored with the guiid as the file name. I'm looking for guidance on exporting the files from Ubuntu to the mounted share with new names.
Querying the csv gives me this output of 2 columns
/mnt/SQLUTIL cat files.csv

eg: 947b43a1d3f1e4451fe1108c353ff571,w2Export.csv

I can get the first column only, to use for searching the directory for the files I want
cut -d',' -f1 files.csv

but I cant figure out how to copy the files using guiid returned by "cut -d',' -f1 files.csv" and rename them using the values returned by "cut -d',' -f2 files.csv"

any advice & help greatly appreciated!
 
Old 01-15-2016, 02:30 PM   #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
Try using a bash while loop that reads from the file and does the split for you. Assuming you are running it from within the directory where all the files are located it should be a
simple case of then using mv to rename the files.
 
Old 01-15-2016, 02:36 PM   #3
Firmbyte
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
I have tried a loop but not been able to get
it to work. I think I'm doing the loop and output wrong.
 
Old 01-15-2016, 02:37 PM   #4
Firmbyte
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
I have tried a loop but not been able to get
it to work. I think I'm doing the loop and output wrong. Hence me looking for help
 
Old 01-15-2016, 06:59 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Show us your code
 
Old 01-16-2016, 12:54 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Firmbyte View Post
Querying the csv gives me this output of 2 columns
/mnt/SQLUTIL cat files.csv

eg: 947b43a1d3f1e4451fe1108c353ff571,w2Export.csv

I can get the first column only, to use for searching the directory for the files I want
cut -d',' -f1 files.csv

but I cant figure out how to copy the files using guiid returned by "cut -d',' -f1 files.csv" and rename them using the values returned by "cut -d',' -f2 files.csv"

any advice & help greatly appreciated!
A pragmatic solution would be editing the file and replace the beginning of each line with "cp " and the comma with a blank (or a blank followed by the target directory). Then you have a script that does the copying.

You can do this interactively with an editor like vim or with sed, for example:
Code:
sed -e 's#^#cp #' -e 's#,# /tmp/target/#' files.csv > copyscript
then run copyscript.

Explanation: The "s" command in sed substitutes a regular expresssion with a string. The first expression is "^", which means the beginning of the line. I use hash signs "#" to separate expressions from replacements; any character can be used for the separation, as long as it doesn't appear in the expression or the replacement.
 
Old 01-16-2016, 03:15 AM   #7
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
You could use the approach above, although if we are getting stuck on a simple loop this may be a little advanced.

As chrism01 said, show us your code with the loop and we will help you through it
 
Old 01-16-2016, 07:33 AM   #8
Firmbyte
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
A pragmatic solution would be editing the file and replace the beginning of each line with "cp " and the comma with a blank (or a blank followed by the target directory). Then you have a script that does the copying.

You can do this interactively with an editor like vim or with sed, for example:
Code:
sed -e 's#^#cp #' -e 's#,# /tmp/target/#' files.csv > copyscript
then run copyscript.

Explanation: The "s" command in sed substitutes a regular expresssion with a string. The first expression is "^", which means the beginning of the line. I use hash signs "#" to separate expressions from replacements; any character can be used for the separation, as long as it doesn't appear in the expression or the replacement.
I really appreciate your help, and I followed that, it worked except for spaces in my file names but got that fixed.

The process you described work great otherwise - I really appreciate you help.

Last edited by Firmbyte; 01-16-2016 at 07:43 AM.
 
Old 01-16-2016, 07:38 AM   #9
Firmbyte
LQ Newbie
 
Registered: Jan 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
You could use the approach above, although if we are getting stuck on a simple loop this may be a little advanced.

As chrism01 said, show us your code with the loop and we will help you through it
Chrism01, I could bang this out in Powershell in a under a minute with one line of code but never having had any Unix, Linux or anything else but Windows & SQL Server experience, I've been asked by my work yesterday to take this on. Having been given a deadline to get files out from a list of over 25 million files stored with only guiid, I hoped some consulting fellow IT folk might be helpfull and save me. So a little understanding would be appreciated.
 
Old 01-16-2016, 07:56 AM   #10
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
We do understand, but the usual process is to show us what you have tried and where you are stuck?

Obviously powershell and bash are different, but the concept of a loop in either to read from a file should be familiar. What will be different is the redirection and the setting of something to split
the file on. So to get you going:
Code:
while IFS=, read -r guid filename
do
  ... use cp or mv here as required
done<files.csv
There are a plethora of online material for bash, here are a couple:

http://tldp.org/LDP/abs/html/
http://mywiki.wooledge.org/TitleIndex
 
  


Reply



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] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
merge multiple files each with two columns. 11st col same but may have difft values newbie271 Linux - Newbie 2 01-10-2012 06:03 PM
[SOLVED] Forum index post col not alligned ted_chou12 LQ Suggestions & Feedback 3 09-21-2011 07:55 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
command col(1) as fully buffered to stdout. Sunilsbjoshi Programming 0 06-09-2006 07:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:15 PM.

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