Linux Command: Bulk Renaming, Adding a Prefix/Suffix to Multiple Files

I split a large file into 1GB chunks. Since I piped half the file into split, I didn’t get to choose the file names. I wanna use a prefix that I recognize later. I also wanted to add a suffix of .csv so I remembered what the contents are. Here’s what I did:

backbox:~/Storage02/MySpaceDump$ ll -h
total 18G
drwxr-xr-x 6 dan dan 176K Oct 16 21:00 ./
drwxr-xr-x 14 dan dan 16K Dec 31 1969 ../
-rw-r–r– 1 dan dan 1.0G Oct 16 20:49 xaa
-rw-r–r– 1 dan dan 1.0G Oct 16 20:50 xab
-rw-r–r– 1 dan dan 1.0G Oct 16 20:50 xac

backbox:~/Storage02/MySpaceDump$ rename ‘s/xa/MySpaceDump.half2.xa/g’ xa*
dan@backbox:~/Storage02$ ll -h
total 18G
drwxr-xr-x 6 dan dan 176K Oct 16 21:51 ./
drwxr-xr-x 14 dan dan 16K Dec 31 1969 ../
-rw-r–r– 1 dan dan 1.0G Oct 16 20:49 MySpace.part2.xaa
-rw-r–r– 1 dan dan 1.0G Oct 16 20:50 MySpace.part2.xab
-rw-r–r– 1 dan dan 1.0G Oct 16 20:50 MySpace.part2.xac

backbox:~/Storage02/MySpaceDump$ zsh

backbox ~/Storage02/MySpaceDump
% autoload zmv

backbox ~/Storage02/MySpaceDump
% zmv -w ‘MySpace.part2.*’ ‘MySpace.part2.$1.csv’

backbox ~/Storage02/MySpaceDump
% ls -al
total 18325520
drwxr-xr-x 2 dan dan 16384 Oct 16 22:15 .
drwxr-xr-x 6 dan dan 180224 Oct 16 21:52 ..
-rw-r–r– 1 dan dan 1073741772 Oct 16 20:49 MySpace.part2.aa.csv
-rw-r–r– 1 dan dan 1073741806 Oct 16 20:50 MySpace.part2.ab.csv

Here’s what we did:

  1. Used the rename command to bulk rename the files with a prefix of MySpace.part2.
  2. Hopped into a Z shell by saying zsh
  3. Enabled the zmv commad with autoload zmv
  4. Used zmv to add .csv to the end of the file

These files are for http://www.kubisec.com / http://kubisec.mywire.org. Next I need to clean the files up so I can smoothly import them into MySQL. Check out my next post to see how I did this.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s