0

Linux novice here.

I'm building my very first bash script and I've written code to truncate and clean up csv file names however its very static and I have to manually count characters I wish to truncate. I'd like to dynamically remove the prefix of files. I've seen numerous examples where folks keep the prefix but I effectively, want to keep the last 7 chars (or more....including file extension.)

Sample below.

# Sample of filenames in a folder
SOLO_PAL3.xyz.ino.IVC.csv   ->  renamed to   IVC.csv
SOLO_PAL3.xyz.ino.EVC.csv   ->  renamed to   EVC.csv
SOLO_PAL3.xyz.ino.VVC.csv   ->  renamed to   VVC.csv
SOLO_PAL3.xyz.ino.WVC.csv   ->  renamed to   WVC.csv

Filename Truncation - while this works, cut -c19- requires manual updating for the integer for specific file lengths at present. I left echo on in this example so I can see the change before I execute.

for file in ???*;      
    do echo mv $file `echo $file | cut -c19-`; 
    # Something equivalent to RIGHT($file,$file.length-($file.length)-7))?
done

Many thanks in advance!

2 Answers 2

1

Your question is rather similar to this one, and my answer is based partially on an answer to that question.

for f in *.csv
do
  newf="${f%.csv}"
  newf="${newf##*.}"
  printf '%s.csv\n' "${newf}"
done

Since you define the problem universe as the set of all *.csv files, this loop first removes the .csv from the end of the filename. Next, it removes the longest possible prefix that consists of any string of characters followed by a dot .. Finally, it printfs the remaining base filename and re-appends the .csv.

The actual mechanics of doing the mv command are left as an exercise to the reader.

1
  • 1
    Jim, I reviewed your link, so thanks again for your answer and sharing. To close the loop completely for future readers, I replaced your print statement above with mv -- "$f" "${newf}.csv"; to actually replace the files with the updated files. Commented Apr 13, 2021 at 15:07
0

Using the perl rename utility:

rename -n 's/^.*\.(.{3}\.csv)$/$1/' *.csv

The -n makes this a dry-run, so it won't actually rename any files, just display what it would do. Check that it does what you want and run it again without -n (or replace -n with -v for verbose output).

NOTE: this only works with the perl rename utility and not other programs with the same name (in particular, the rename utility from the util-linux package is a completely different program with completely different features and command-line options).

You can check if you have perl rename by running it with -V. The output will be similar to:

$ rename -V
/usr/bin/rename using File::Rename version 1.13, File::Rename::Options version 1.10

The executable for perl rename is sometimes called prename or file-rename. Adjust the command lines above to use them if that's what you have installed.

1
  • Thank you, cas but this one is a little over my head at the moment. I am doing all of this in a devcontainer so while I'm certain I can add a reference for perl, I'm still learning on how to add references and such. I found this reference which seems comprehensive https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename Commented Apr 13, 2021 at 14:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.