How to rename files with sed and csv

9,303

Solution 1

If you happen to have a file that looks like this:

old_name1, new_name1
old_name2, new_name2
old_name3, new_name3

You can do a dirty little trick:

sed 's/^/mv -vi "/;s/, /" "/;s/$/";/' < names.csv | bash -

The sed comamnds (delimited by semicolons) does this (s is for substitute, / is used as a delimiter, any other character would do, @ is often used as well):

  • s/^/mv -vi "/ - adds mv -vi " at the beginning of each line (^ is beginning of line);
  • s/, /" "/ - replaces comma followed by a space with " ";
  • s/$/";/ - appends double quotes to all lines ($ means end of line)

and hence the output of sed will be this:

mv -vi "old_name1" "new_name1"
mv -vi "old_name2" "new_name2"
mv -vi "old_name3" "new_name3"

which will be fed to bash to execute. The quotes around filenames are not mandatory, they just guard any possible spaces in the filenames (however they will defintely not help against spaces followed by a comma, since that is used as delimiter). the -v instructs mv to display what it is doing (in case something goes wrong, you'll know what happened), -i will cause it to ask if it were to overwrite already existing file.

Solution 2

There are several quoting problems in your script. It will mangle file names containing special characters: whitespace, \[?*-.

**Always use double quotes around variable substitutions "$i" and command substitutions "$(sed …)". Without double quotes, the value of the variable is interpreted as a list of whitespace-separated wildcard patterns, and each pattern is replaced by the list of matching file names (if there are no matches, the pattern is left alone).

Also, if a file name begins with -, it will be interpreted as an option by mv and possibly by echo. Pass -- first to tell the command that there will be no more options.

for i in *.ai; do
  mv -- "$i" "$(printf '%s\n' "$i" | sed 's,a1,b1,')"
done

(This still assumes that your file names don't contain newlines.) (I also fixed the sed argument into what you presumably meant.)


If your list of file names is plain comma-separated with no quotes, you can parse it with a shell script. This assumes that there are no commas or newlines in any file name.

while IFS=, read -r from to junk; do
  mv -- "$from" "$to"
done <"$files_to_rename.csv"

If your input file is a CSV file with possible quoting, use a real CSV parser. Don't try to roll your own. See Is there a robust command line tool for processing csv files? For example, in Python (with the CSV on standard input):

import csv, os, sys
for line in csv.reader(sys.stdin):
    (source, dest) = line
    os.rename(source, dest)

Solution 3

If your files have funny characters (',",\,$), you can loop over the file and read it into a bash array.

while IFS=, read -r -a arr; do mv "${arr[@]}"; done <files.csv 
Share:
9,303

Related videos on Youtube

DᴀʀᴛʜVᴀᴅᴇʀ
Author by

DᴀʀᴛʜVᴀᴅᴇʀ

I am an evil overlord that is sometimes mis-judged. I love walks in the park under a lightning storm and driving around my Death Star through the Galaxy. People have been known to find me writing code for websites, light saber drawing in Illustrator on my Wacom, building apps to do something because I'm lazy or using the force to market my campaign on why the Rebels need to be demolished.

Updated on September 18, 2022

Comments

  • DᴀʀᴛʜVᴀᴅᴇʀ
    DᴀʀᴛʜVᴀᴅᴇʀ over 1 year

    I'm new with Linux, sed, and awk but I don't mind challenging myself on new ideas. That being said, I understand the purpose and how to use rename and sed for a common event such as adding a $date or removing a _1234 from all the files. However, I want to figure out how to rename 100's of files that have no relation to one another other than the file type.

    Renaming example:

    • old name alpha.ai renamed to omega.ai
    • old name pie32.ai renamed to apple.ai
    • old name xmas.ai renamed to santa.ai

    I originally used a bash script to pull all the names to a .csv file and placed them in column A. In column B I wrote in the new names. Now since there is not a common event occurring how could you write sed or the like to do this??

    #!/bin/bash
    for i in *.ai
    do
            mv $i $(echo $i | sed "a1,b1")
    
    done
    
    • Julian
      Julian over 9 years
      It is tricky to rename all special characters by automated scripts, you can find a link to my script in this answer: superuser.com/a/858671/365691
  • DᴀʀᴛʜVᴀᴅᴇʀ
    DᴀʀᴛʜVᴀᴅᴇʀ over 11 years
    I get a mv: missing destination??
  • Demonel
    Demonel over 4 years
    Awesome answer, works exactly as stated.