Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-11-2007, 05:21 PM
|
#1
|
Member
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73
Rep:
|
use awk &/or sed to read file 1 line 1 & file 2 line 1
I'm having a beast of a time trying to reference line1 of two different files in the same command from a small shell script. The goal is to end up with the following sent to the shell:
mv "stupid filename with spaces copied from Windows.mp3" less_stupid_filename.mp3
I have sed doing the search & replace algorithm just fine, but the closest I've gotten to automating the rename of entire directories of my music collection by shell script is the above command, separated by a newline.
For some reason, BASH is not friendly to that format!
Any general tips will be appreciated. If you care to look at the existing script to troubleshoot, just ask.
|
|
|
07-11-2007, 05:31 PM
|
#2
|
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873
|
I would use Krename. I love that utility for making changes in massive numbers of file names if they have similar names.
|
|
1 members found this post helpful.
|
07-11-2007, 05:42 PM
|
#3
|
Member
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73
Original Poster
Rep:
|
Thanks stress junkie, I'll check that out.
I've never heard of krename. Good info.
|
|
|
07-11-2007, 08:28 PM
|
#4
|
Member
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73
Original Poster
Rep:
|
Thanks stress junkie
krename rocks the casbah!
|
|
|
07-12-2007, 05:46 AM
|
#5
|
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873
|
Quote:
Originally Posted by gr8scot
krename rocks the casbah!
|
I'm very glad that you like it. I often use it after downloading vast numbers of jpeg files from NNTP news groups. (What can I say? I love actresses. ) I have found that it is worth using Krename if you have as few as five files to rename as compared to using a file manager and renaming each file individually by hand.
Last edited by stress_junkie; 07-12-2007 at 05:48 AM.
|
|
|
07-12-2007, 08:04 AM
|
#6
|
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:
|
If you already have the sed command worked out, maybe you could try the next loop (in bash)?
Code:
IFS="\n";
for fromFile in `cat list_of_files_with_spaces`; do
toFile=`echo ${fromFile} | your_sed_command`;
mv "${fromFile}" "${toFile}";
done;
You may want to echo the "mv" command before actually executing it, just to make sure that it does what you expect it to do.
Alternatively, say you have the file names with spaces in one file and the sed'ed file names in another, then you can use "paste" utility to combine both files (it kind of concatenates line by line), using an easy field separator like ',' or so.
Then you can go over that file in a loop (again, line-wise), each time using 'cut' to get both fields into a variable. Or simply use "sed" directly to create the mv commands.
Example:
Code:
file1=/path/to/some/file #file1 contains the win-style file names with spaces
file2=/path/to/2nd/file #file2 contains the "corrected" file names
#The first sed adds the "mv" command and an opening double quote in front,
#the second replaces the ',' delimiter with the closing double quote and adds a space for the mv command
paste --delimiters=, file1 file2 | sed -e 's/^./mv "&/' | sed -e 's/,/" /' > yourScript
After this, examine the results in file yourScript. If they're OK, make the script executable and run it.
|
|
1 members found this post helpful.
|
07-12-2007, 08:13 PM
|
#7
|
Member
Registered: Jun 2007
Distribution: Debian, kubuntu
Posts: 73
Original Poster
Rep:
|
That looks like exactly what I want
Quote:
Originally Posted by timmeke
If you already have the sed command worked out, maybe you could try the next loop (in bash)?
|
Code:
IFS="\n";
for fromFile in `cat list_of_files_with_spaces`; do
toFile=`echo ${fromFile} | your_sed_command`;
mv "${fromFile}" "${toFile}";
done;
Quote:
Originally Posted by timmeke
You may want to echo the "mv" command before actually executing it, just to make sure that it does what you expect it to do.
|
I've already re-named all the files with krename, but it did have trouble with directories. Possibly by design. Even when started from a terminal as root, it wouldn't change the name of any directory with any contents. Thanks.
Last edited by gr8scot; 02-20-2011 at 10:29 PM.
Reason: fix open quote tag
|
|
|
All times are GMT -5. The time now is 07:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|