1

I have multiple files with an example of how they look like shown below.

-rw-r--r--   1 my_user     users           12 Dec 13 09:56 Example_30_001_20130913175000.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 09:57 Example_30_002_20130913180854.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 09:58 Example_30_003_20130913180857.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 09:58 Example_30_004_20130913180901.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 09:59 Example_30_005_20130913180904.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 10:02 Example_30_006_20130913180907.DAT  
-rw-r--r--   1 my_user     users           12 Dec 13 09:59 Example_30_007_20130913180911.DAT  

My question is how do I copy them in the same directory and rename the copied files using a sh script such that they start with something like the filenames shown below?

Ex_Example_001.DAT  
Ex_Example_002.DAT  
Ex_Example_003.DAT  
Ex_Example_004.DAT  
Ex_Example_005.DAT  
Ex_Example_006.DAT  
Ex_Example_007.DAT
1

Execute this in the folder of your files:

find . -type f -name "Example_30*.DAT" | awk -F\_ '{printf "cp -v %s Ex_Example_%s.DAT\n", $0, $3}' | bash
  • find . -type f: search only for files
  • -name "Example_30*.DAT": file beginning with "Example_30" and ending with ".DAT"
  • | awk -F\_: pipe this to awk and set the delimiter to _
  • '{printf "cp -v %s Ex_Example_%s.DAT\n", $0, $3}': generate a command like this: cp -v oldname newname
  • | bash: and pipe this to bash to execute it

Output should look like this:

»./Example_30_002_20130913180854.DAT“ -> »Ex_Example_002.DAT“
»./Example_30_005_20130913180904.DAT“ -> »Ex_Example_005.DAT“
»./Example_30_003_20130913180857.DAT“ -> »Ex_Example_003.DAT“
»./Example_30_006_20130913180907.DAT“ -> »Ex_Example_006.DAT“
»./Example_30_007_20130913180911.DAT“ -> »Ex_Example_007.DAT“
»./Example_30_004_20130913180901.DAT“ -> »Ex_Example_004.DAT“
»./Example_30_001_20130913175000.DAT“ -> »Ex_Example_001.DAT“

Edit:

What if I want it to be put in a script outside of that folder? How would I go about doing it?

Create a file called script. Add the following lines into that file:

#!/bin/bash
DIRECTORY=/path/to/dir/
cd $DIRECTORY
find . -type f -name "Example_30*.DAT" | awk -F\_ '{printf "cp -v %s Ex_Example_%s.DAT\n", $0, $3}' | bash
cd -

Make the script executable:

chmod u+x script

And then call the script:

./script

or

/absolute/path/to/script
| improve this answer | |
  • What if I want it to be put in a script outside of that folder? How would I go about doing it? – CHT Dec 13 '13 at 7:16
  • @CHT in your script execute cd /path/to/dir prior to the command above, and cd - after, to go back to the prevous directory. – chaos Dec 13 '13 at 7:26
  • If you have an example of how that new code would be implemented in the current code, I would really appreciate that. Edit: I'm new to this, thus I need the example so as to see how it would connect. – CHT Dec 13 '13 at 8:17
  • @CHT: see my edit – chaos Dec 13 '13 at 8:31
  • Thanks for the edit. I have read it through and from what I understand, it's supposed to first change to the directory of the files, then "find and replace" the names of the files and finally going back to the original folder it came from. Am I right? – CHT Dec 13 '13 at 8:56
1

In zsh:

autoload -U zmv
zmv -C -o -p '/path/to/source/directory/(Example_)[0-9]##_([0-9]##)_20130913175000(.DAT)' '/path/to/destination/directory/Ex_$1$2$3'

In other shells:

cd /path/to/source/directory/
for source in Example_*_*_*.DAT; do
  tail=${source##*/}
  target=Ex_${tail%%_*}; tail=${tail#*_}
  tail=${tail#*_}
  target=${tail%%_*}.${tail##*.}
  cp -p "$source" "target"
done
| improve this answer | |
  • While your answer is good, I am a newbie in Unix, meaning I won't understand why your answer is so. Therefore, the best answer would have to go to the other person. Sorry for the trouble. – CHT Dec 16 '13 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.