{"id":9144,"date":"2025-07-07T18:30:55","date_gmt":"2025-07-07T13:00:55","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=9144"},"modified":"2025-07-07T18:31:08","modified_gmt":"2025-07-07T13:01:08","slug":"how-to-copy-files-between-servers-using-a-bash-script","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-copy-files-between-servers-using-a-bash-script\/","title":{"rendered":"How to Copy Files Between Servers Using a Bash Script?"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When managing multiple servers, it&#8217;s common to transfer files between them. Using a Bash script with <code>scp<\/code> (secure copy), We can automate this task efficiently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Remote user credentials<\/li>\n\n\n\n<li>Install the sshpass service.<\/li>\n\n\n\n<li>Remote server path.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step1 :<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Bash Script to Copy a File with SSH Port and Password.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save the script as <code>script_file.sh<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>#!\/bin\/bash\n\n# Prompt for inputs\nread -p \"Enter the full path of the local file to copy: \" SOURCE_FILE\nread -p \"Enter remote SSH username: \" DEST_USER\nread -p \"Enter remote server IP or hostname: \" DEST_HOST\nread -p \"Enter SSH port number (e.g. 22): \" SSH_PORT\nread -p \"Enter full remote destination path (e.g. \/home\/user\/): \" DEST_PATH\nread -s -p \"Enter SSH password: \" SSH_PASS\necho \"\"\n\n# Run SCP using sshpass with custom port\nsshpass -p \"$SSH_PASS\" scp -P \"$SSH_PORT\" \"$SOURCE_FILE\" \"$DEST_USER@$DEST_HOST:$DEST_PATH\"\n\n# Check the result\nif &#091; $? -eq 0 ]; then\n    echo \"\u2705 File copied successfully.\"\nelse\n    echo \"\u274c File copy failed!\"\nfi<\/em><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make the file executable<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$chmod +x script_file.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run the file with the command below.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>$    .\/script_file.sh<br>or<br>$    sh script_file.sh<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Follow the prompts and enter the required details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This Bash script provides a quick way to transfer files between servers using <code>scp<\/code> a custom SSH port and password authentication. While it&#8217;s convenient for controlled or temporary environments, always prioritise key-based authentication for production environments to maintain security<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: When managing multiple servers, it&#8217;s common to transfer files between them. Using a Bash script with scp (secure copy), [&hellip;]<\/p>\n","protected":false},"author":496,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-9144","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phns3A-2nu","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/496"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=9144"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9144\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=9144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=9144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=9144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}