Update file names in single directory with prefix plus letter

1

I have found numerous methods to add a simple prefix onto files in a folder on Linux e.g.

for FILENAME in *; do mv $FILENAME REF_$FILENAME; done 

(https://stackoverflow.com/questions/4787413/rename-files-and-directories-add-prefix)

I would like to do this but with REF changing for each file to REFa, REFb, REFc,... This would be run manually on a single directory because the REF would be different for each.

Thanks in advance because the winner will literally save me hours of manual data entry ;-)

Matthew

Posted 2017-07-04T09:09:08.583

Reputation: 13

1Can you use numbers instead of letters ? That would make the problem much easier to solve. Also, are you using it for ordering purposes (in which case we would want to pad it) – davidgo – 2017-07-04T09:43:35.690

Answers

0

The quick and dirty answer would be as follows:

array=(`echo {a..z}{a..z}{a..z}`);i=0; for FILENAME in *; do  mv $FILENAME REF${array[i]}_$FILENAME; i=$(($i+1)); done

But I'd prefer doing it as a script which you can then run - its easier to maintain and describe

array=(`echo {a..z}{a..z}{a..z}`)
i=0 
for FILENAME in *
do 
    mv $FILENAME REF${array[i]}_$FILENAME 
    i=$(($i+1)) 
done

The first line loads up a sequence of 3 digit letters, starting aaa, and proceeding down to zzz. You can decrease or increase the size of this array by increasing or decreasing the number of {a..z} - and you could replace it with capital letters or some other simple sequence.

i is a counter which we increment so we can get a unique sequence for each REF value.

I note that I don't like the "FILENAME in *" bit of code, because it won't play well with subdirectories. I'd be inclined to replace that line with

for FILENAME in `find . -type f`

to handle files, including subdirectories, or

for FILENAME in `find . -maxdepth 1 -type f`

for files only in the current directory (ie not recursing it, and ignoring directories)

davidgo

Posted 2017-07-04T09:09:08.583

Reputation: 49 152

0

You could build a command tailored to this specific problem, I'm sure other answers will do this. My answer is more general, maybe it will fit you though.

vidir (most likely from moreutils package) allows you to edit filenames in a text editor. To choose an editor set the EDITOR variable.

This way you can use many features of vi, emacs or whatever editor you use. Read man vidir to learn how to make it work with subdirectories if you need.

This is an example of what it would look like in your editor:

1   ./AdbeRdr9.5.5-1_i486linux_enu.bin
2   ./AdobeAIRInstaller.bin
3   ./Dir1
4   ./Dir10
5   ./Dir2
6   ./bashfile.sh

The numbers identify files in this particular instance of vidir, they are followed by tabs. You cannot touch these numbers and tabs unless you know what you are doing. Changes in the filenames are applied when you save the file and exit the editor.

If you're afraid there is a filename with tab or a newline character somewhere and this will break something then keep calm. As far as I know vidir will refuse to work with it in the first place.

Now the best part. The editor may as well work outside the console, so it's possible to:

  1. Invoke EDITOR=kate vidir, this will run kate editor.
  2. Copy all the text and paste to LibreOffice Calc; you will get two columns.
  3. Manipulate the names using tools provided by Calc to obtain the output as two columns: numbers and new names.
  4. Copy these two columns back to kate and replace the old text.
  5. Save the text file (do not change its name).
  6. Exit the editor.

This is very powerful. Deleting lines and saving the text file will delete the files. Swapping numbers will make files swap names (or even paths if you work with subdirectories). With great power comes great responsibility. You should probably experiment with expendable files and directories first.

Kamil Maciorowski

Posted 2017-07-04T09:09:08.583

Reputation: 38 429