Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 01-07-2014, 01:24 AM   #1
LQ Newbie
Registered: Aug 2008
Posts: 7

Rep: Reputation: 0
Question Cleaning up file names with paste, mv and/or sed?

Been a while since I've been on here. So I have a poorly labeled file collection. A friend wanted to see what I had so i
:~/ ls ~/files/ > badfiles.txt
They reviewed and edited the file names and sent it back as goodfiles.txt. Now both files correspond line by line but there are no patterns to the editing.
badfiles.txt          goodfiles.txt
  FILE NAME1.XXX        file name 1
  file_name 2.YYY       file name 2
  file.name3.XXX        file name 3
  filename(4).ZZZ       file name 4
So I ideally want to rename the files but using badfiles.txt as input and goodfiles.txt as output.

I tried this with unintended results
while read -r a b; do ! [[ -e "$b" ]] && mv "$a" "$b"; done <<< "$(paste badfiles.txt goodfiles.txt)"
paste badfiles.txt goodfiles.txt | while read n k; do mv -T $n* $k; done
and several variations of the above. The goodfiles.txt has been stripped of extensions as well so is it possible to preserve them during the renaming?
Old 01-08-2014, 12:48 AM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,630

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
Try parameter substitution to get the extensions
paste badfiles.txt goodfiles.txt | while read n k ; do ext=${n##*.} ; mv $n $k.$ext ; done
You're going to have to escape the spaces goodfiles.txt first - something like this might do it
sed 's/ /\\ /g' goodfiles.txt
(presumes no extranious blanks - especially at end of line)

Last edited by syg00; 01-08-2014 at 01:11 AM. Reason: escape spaces
Old 01-08-2014, 02:21 PM   #3
LQ Newbie
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you for the reply! I knew I had the right utilities, just wasn't sure how to define the variables as needed.

Now do I need to escape anything else? Specifically, some names include ? & ( ) ' ; , -

Last edited by Mr_Fixit; 01-08-2014 at 02:37 PM. Reason: clean up
Old 01-08-2014, 04:55 PM   #4
LQ Newbie
Registered: Aug 2008
Posts: 7

Original Poster
Rep: Reputation: 0
So escaping everything came up with some errors..

sed '
s/ /\\ /g'
<../badfiles.txt >../newbadfiles.txt
I checked the output and the whitespace was escaped.

I was testing this with:
$ paste ../newbadfiles.txt | while read n ; do touch $n ; done
The output was each string was made into a file even with escaped characters. I then tried with double quotes
Touch still manages to split the file at the whitespace and some special characters.

Last edited by Mr_Fixit; 01-08-2014 at 08:19 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 01:27 PM
[SOLVED] How to changing same file names by their directory names? bayaraa_u Linux - General 3 04-09-2010 09:26 AM
grep+awk+sed+paste+sort in one script? mchriste Linux - Software 13 03-05-2009 02:57 PM
Script: SED for Copy/Paste Lines from Files unihiekka Programming 2 10-07-2008 07:12 AM
using sed ( I think ) to extract names from e-mail address rosv Programming 4 07-07-2008 03:52 AM

All times are GMT -5. The time now is 06:47 AM.

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