如何在整个目录中查找具有特定扩展名的文件,然后复制到另一台服务器

时间:2016-12-01 08:01:56

标签: python xml linux shell unix

我正在尝试复制具有特定扩展名的文件。我们说3gp

例如,假设文件annc1.xml具有g711u.3gp文件名。

我需要查看整个目录中可用的3gp文件名,然后使用sftp将它们复制到服务器。

[root@DVRTPG009 regression]# cat annc1.xml | grep 3gp
                    <audio uri="http://10.211.0.159/mxml_clips/10_SecondClip_amr.3gp"/>
            <send target="source" event="app.10_SecondClip_amr.3gp" namelist="play.amt play.end"/> 

2 个答案:

答案 0 :(得分:0)

$ find /location/of/directory -name '*.3gp' -exec scp {} username@remoteip:/remote/location/ \;

答案 1 :(得分:0)

如果你需要一个python脚本来为你做这件事。 我使用python paramiko,scp,寻找干净的文档 关于如何使用它们。

pip install scp
easy_install paramiko

在我的电脑上测试(Ubuntu 16.04)64位。您需要进行少量更改,服务器名称,用户名,密码和要将文件上载到的远程目录。

import os
import sys
import glob 
import fnmatch 
import paramiko # for ssh may need to be installed.
from scp import SCPClient
import time 

# This may work on both linux and window or mac.
DEFAULT_DIR=os.path.expanduser("~")

server="192.168.0.100"
port=22
user="s8software"
password="!your-password.com!" # may not bee needed when we use public key something.pem
SFTP_REMOTE_PATH="/home/s8software/Documents/"

def search_directory_for_files(dirname, extension="*.py"):
    """
    Search an entire directory for particular files
    denoted with extension. and copy them to a remote server.
    @param: dirname--> The directory contains files we want to copy.
    @param: extension--> The files with a particular extension we want to copy.

    """
    if ( not dirname):
        raise AttributeError("We need at least a source directory!")

    for root, direname, filenames in os.walk(dirname):
        for filename in fnmatch.filter(filenames, extension):
            yield {"fullpath":os.path.join(root, filename), "filename":filename}  

def open_scp_connection(server, port, user, password):
    """
    Function returns a well established connection to the
    server we want to transfer files to.
    """
    try:
        client = paramiko.SSHClient()
        client.load_system_host_keys()

        # Note the public key here don't.
        # TODO. See how you can either pass the username and password.

        pkey = "/home/s8software/Working/Keys/KeParis/TestInstance.pem"
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # NOTE: if you use the public_key then uncomment this line and comment
        # the one using the password.
        #client.connect(server, port, user, key_filename=pkey)

        client.connect(server, port, user, password=password)

        return client

    except:
        raise

def get_scpclient_connection(ssh_object):
    try:
        return SCPClient(ssh_object.get_transport())
    except:
        raise

def _send_remote_recursive(scp_client, file_full_path, filename):
    """
    Given a  file send them to the remote server.
    """
    try:
        print time.asctime(), 'Uploading....', filename
        return_status = scp_client.put(file_full_path, remote_path=os.path.join(SFTP_REMOTE_PATH, filename))        
    except:
        raise
    else:
        return return_status

if __name__=="__main__":
    # First open the connection using the port of the server
    endpoint_connection = open_scp_connection(server, port, user, password)
    scp_connection = get_scpclient_connection(endpoint_connection)

    # Recursely get everything we need and send to remove server.
    for f in search_directory_for_files(DEFAULT_DIR):
        source_path = f.get("fullpath")
        filename = f.get("filename")

        # Send the files to the remote server.
        _send_remote_recursive(scp_connection, source_path, filename)

虽然我需要一点修改。