1

I'd like to rename multiple files in one directory using shell scripting the files have common prefix :

Modify: 2014-09-19 16:08:35.000000000 +0300-16:08:35.000000000-OCS_dmobfw1-oam_d04_20140919_155335.csv
Modify: 2014-09-19 16:09:23.000000000 +0300-16:09:23.000000000-OCS_dmobfw1-oam_d01_20140919_155423.csv
Modify: 2014-09-19 16:09:51.000000000 +0300-16:09:51.000000000-OCS_dmobfw1-oam_d09_20140919_155451.csv
Modify: 2014-09-19 16:12:40.000000000 +0300-16:12:40.000000000-OCS_dmobfw1-oam_d07_20140919_155740.csv
Modify: 2014-09-19 16:14:48.000000000 +0300-16:14:48.000000000-OCS_dmobfw1-oam_d05_20140919_155948.csv
Modify: 2014-09-19 16:18:14.000000000 +0300-16:18:14.000000000-OCS_dmobfw1-oam_d03_20140919_160314.csv
Modify: 2014-09-19 16:18:15.000000000 +0300-16:18:15.000000000-OCS_dmobfw1-oam_d08_20140919_160315.csv

i want to remove the following part from all files :

Modify: 2014-09-19 16:08:35.000000000 +0300-16:08:35.000000000-

so the output can be like this :

OCS_dmobfw1-oam_d01_20140423_223805.csv OCS_dmobfw1-oam_d01_20140423_223905.csv

i tried :

 for name in Modify* ;
do
 mv "$name"  $(echo $name | sed 's/Modify: 2014-09-19 16:18:14.000000000 +0300-16:18:14.000000000-/ /g') ;
done
  • You did not tell us what was the problem with you approach but since your file names contain spaces (don't do that), you'll be in troubles. Using `find` will probably be the safest option. – 5gon12eder Sep 24 '14 at 16:02
  • could you please tell me more about how to use find in my situation ? – Mohamed Yasser Sep 24 '14 at 16:06

3 Answers3

0
for name in Modify* ;
do
    new=`echo $name | sed 's/Modify: 2014-09-19 16:18:14.000000000 +0300-16:18:14.000000000-//g'`; 
    mv $name $new;
done
tcatchy
  • 849
  • 1
  • 7
  • 17
0

For example:

for name in Modify*
do
    echo mv "$name" "$(sed -E 's/Modify: .{55}//'<<<"$name")"
done

remove the echo if satisfied..

The <<< is bash extension, if you don't have bash use

    echo mv "$name" "$(echo "$name" | sed -E 's/Modify: .{55}//')"

If every file contains the OCS, you could use the shorter:

    echo mv "$name" "$(grep -o 'OCS.*' <<<"$name")"
clt60
  • 62,119
  • 17
  • 107
  • 194
  • the last edit is working fine thanks but the first two aren't working . – Mohamed Yasser Sep 24 '14 at 16:16
  • @MohamedYasser in this case, your sed doesn't knows the `-E`. The _aren't working_ isn't helpful, would be good to know what is the error - for correcting... – clt60 Sep 24 '14 at 16:46
0

Your accepted answer is probably simpler and at least as good but here is how you might do it using find.

find . -mindepth 1 -maxdepth 1 -name 'Modify*' -execdir sh -c 'mv -n "$@" "$(echo "$@" | sed "s,Modify: .\{55\},,")";' -- '{}' \;

This approach will be most useful if you want to recurse into subdirectories. In that case, remove the -maxdepth 1 option.

The key problem with your original attempt was that your file names all have different time stamps but you were only replacing a particular one.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92