-2

Any suggestions on how I should approach this please? I need to move all files in a directory and change their names removing everything before and including the first '_' eg:

Example Before:

data directory
DT21_Filename1
D22_Filename2
D33_Filename3

export_data directory Example After:

Filename1
Filename2
Filename3
Rakholiya Jenish
  • 3,165
  • 18
  • 28
Sascha
  • 398
  • 6
  • 14

2 Answers2

1

First rename all the file with:

rename 's/([^_]*_)//' *

Then move them to the directory as:

mv * PATH_TO_NEW_DIRECTORY

You can also write a script for the above.

Script:

curr_dir=$PWD
cd $1
for i in *; do
    rename 's/([^_]*_)//' $i
done;
mv * "$curr_dir/$2"

Usage:

bash filename.sh path_to_old_directory relative_path_to_new_directory_from current

This will rename and move files from old_directory to new_directory.

If you are giving absolute path for new directory:

cd $1
for i in *; do
    rename 's/([^_]*_)//' $i
done;
mv * $2
Rakholiya Jenish
  • 3,165
  • 18
  • 28
  • Thanks for you help. However, how do I point to a directory to do that within a bash script please? – Sascha Jun 16 '15 at 14:26
1
cd SOURCE_DIR
for file in *; do
    newname=`echo $file | sed 's/[^_]*_//'`
    mv $file DESTINATION_DIR/$newname
done
Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26