1

I have a folder in my server which contains some PDF files.My problem is, i have to copy to other server and rename it.

Name format is like this "yymmdd_hhmmss_FileNo_PdfNo.pdf"

yymmdd_hhmmss_001_AC1.pdf
yymmdd_hhmmss_001_AC2.pdf
yymmdd_hhmmss_002_AC1.pdf
yymmdd_hhmmss_003_RCY1.pdf

while copying these files i want to rename each file. Name format is like this "FileNo_PdfNo_yymmddhhmmss.pdf"

001_AC1_yymmddhhmmss.pdf
001_AC2_yymmddhhmmss.pdf
002_AC1_yymmddhhmmss.pdf
003_RCY1_yymmddhhmmss.pdf

I want to write a shell script(bash) for this. Please give me some idea or some sample scripts related to this.

Argonauts
  • 199
  • 10
donjulgae
  • 11
  • 1
  • 3
  • 1
    `scp source_name user@remote_host:dest_name` – anishsane May 29 '16 at 15:16
  • Or if you want, you can `scp` entire directory & then `ssh user@remote_host prename ...` command – anishsane May 29 '16 at 15:17
  • It's always good to add what you have done so far. See [\[ mcve \]](http://stackoverflow.com/help/mcve) – sjsam May 29 '16 at 15:19
  • @anishsane that doesn't really solve his problem, because he wants to increment the number in the beginning of the filename, so he needs to check the existing files first, to get the current number.. – GiftZwergrapper May 29 '16 at 16:12
  • ^^ It does not look like so from the snippet in the question; or at least it was not clear. I interpreted it as the parts in filename (separated by underscore) need to be reshuffled. – anishsane May 30 '16 at 03:32

2 Answers2

2

With GNU tar:

tar -cf - *.pdf | ssh user@server "tar -xvf - --transform='s/\(.\{6\}\)_\(.\{6\}\)_\(...\)_\(.\{3,\}\)\.pdf/\3_\4_\1\2.pdf/' --show-transformed-names"

If you want to change destination directory use tar's option -C.


See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
1

With some minor tweaking this should work for you. I put the explanation / comments in the script. You could likely do it without the loop, but it gets tricky transferring and renaming multiple files with scp simultaneously. If you renamed them locally and then transferred them it would be easy to do a single scp call. You could also condense it into a one liner; but I thought this approach might be more expository and easier to modify.

#!/bin/bash - 

# associative array to hold new file names
declare -A new;

# local and remote paths terminated with / for laziness
local_path="/lcl/path/to/pdfs/"
remote_path="/remote/path/to/pdfs/"

# user@machine for remote ssh connection
user_mach="username@machinename"

# error accumulator
local lcl_err=0

# Collect list of files to send - you may want this to be
# more discriminating
orig=`find "$local_path" -name "*.pdf" -exec basename {} \;`

# Iterate through the file list
for i in "${orig[@]}"; do
  # sed replacement to rename them appropriately, stored in
  # associative array new
  new["$i"]="`echo $i | sed -ne 's/^\([0-9]\{6,\}\)_\([0-9]\{6,\}\)_\([0-9]\{3,\}\)_\(.*\)\.pdf$/\3_\4_\1\2\.pdf/p'`"

  # if file was renamed (passed regex filter) then scp it to remote host,
  # saving it remotely with new name
  if [ ${new["$i"]} ]; then
    scp "${local_path}$i" "${user_mach}:${remote_path}${new["$i"]}"
    # don't roll over the return value (256 == 0 in 8 bit return value)
    [[ "$lcl_err" -lt "255" ]] && ((lcl_err+=$?))
  else
    echo "Skipping file $i"
  fi
done
exit "$lcl_err"
Argonauts
  • 199
  • 10