Move files listed in col 1 of csv to new location renaming them to name in col 2 of csv
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
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.
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.
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.
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.