Linux 要复制(scp)和重命名的Shell脚本
我的服务器中有一个文件夹,其中包含一些PDF文件。我的问题是,我必须复制到其他服务器并重命名它 名称格式类似于“yymmdd\u hhmmss\u FileNo\u PdfNo.pdf” 复制这些文件时,我想重命名每个文件。 名称格式如下“FileNo_PdfNo_yymddhhmmss.pdf” 我想为此编写一个shell脚本(bash)。请给我一些与此相关的想法或示例脚本。使用GNU tar:Linux 要复制(scp)和重命名的Shell脚本,linux,bash,shell,copy,rename,Linux,Bash,Shell,Copy,Rename,我的服务器中有一个文件夹,其中包含一些PDF文件。我的问题是,我必须复制到其他服务器并重命名它 名称格式类似于“yymmdd\u hhmmss\u FileNo\u PdfNo.pdf” 复制这些文件时,我想重命名每个文件。 名称格式如下“FileNo_PdfNo_yymddhhmmss.pdf” 我想为此编写一个shell脚本(bash)。请给我一些与此相关的想法或示例脚本。使用GNU tar: tar -cf - *.pdf | ssh user@server "tar -xvf - --t
tar -cf - *.pdf | ssh user@server "tar -xvf - --transform='s/\(.\{6\}\)_\(.\{6\}\)_\(...\)_\(.\{3,\}\)\.pdf/\3_\4_\1\2.pdf/' --show-transformed-names"
如果要更改目标目录,请使用tar的选项-C
请参阅:稍作调整,这应该适合您。我把解释/评论放在脚本中。您可能不需要循环就可以做到这一点,但同时使用scp传输和重命名多个文件会变得很棘手。如果您在本地重命名它们,然后转移它们,那么就很容易进行单个scp调用。你也可以把它浓缩成一行;但我认为这种方法可能更具解释性,更容易修改
#!/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"
scp源名称user@remote_host:dest_name
或者如果需要,可以scp
整个目录,然后sshuser@remote_hostprename…
command添加到目前为止所做的事情总是很好的。请参阅@anishsane,这并不能真正解决他的问题,因为他想增加文件名开头的数字,所以他需要首先检查现有文件,以获得当前的数字。^^从问题中的代码段看,情况并非如此;或者至少还不清楚。我解释为文件名中的部分(用下划线分隔)需要重新排列。
tar -cf - *.pdf | ssh user@server "tar -xvf - --transform='s/\(.\{6\}\)_\(.\{6\}\)_\(...\)_\(.\{3,\}\)\.pdf/\3_\4_\1\2.pdf/' --show-transformed-names"
#!/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"