• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

How to Upload Files to SFTP Folders Using Bash

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
If you're looking to upload files to their respective folders on a server using SFTP (SSH File Transfer Protocol), you've come to the right place. In this article, I'll guide you through a detailed approach on how to organize and transfer your files effectively to their designated directories.

Understanding the Problem


You have a list of files: data.csv, data.xml, and data.json, located on your local machine in the directory ~/Documents. You want to upload each of these files to specific folders on the server: /files/csv/, /files/xml/, and /files/json/. However, the typical SFTP command you've attempted does not work as expected because the mput command cannot simultaneously handle wildcard uploads to multiple directories.

To address this issue, you'll need to follow a clear step-by-step approach to accomplish your goal. Let's break it down.

Step-by-Step Solution: Uploading Files with SFTP


Below is a structured method to upload each file to its respective directory. We will start by connecting to the SFTP server and then upload files one by one to their respective directories.

Step 1: Connect to the SFTP Server


Open your terminal and connect to your SFTP server using the following command:

sftp username@server_address


Make sure to replace username with your actual username and server_address with the address of your SFTP server. You will be prompted to enter your password.

Step 2: Navigate to the Target Directory on the Server


Once connected, navigate to the desired directory where you want to upload the files. Use the cd command:

cd files/


This will switch you to the files directory on your remote server.

Step 3: Upload Each File to Its Respective Folder


Now, you can upload each file individually using the put command. Here’s how you do it:

  • Upload CSV file:

put ~/Documents/data.csv csv/

  • Upload XML file:

put ~/Documents/data.xml xml/

  • Upload JSON file:

put ~/Documents/data.json json/


By specifying the destination folder for each file, you ensure that they are sorted correctly into their designated directories.

Conclusion


Using SFTP for file transfer requires the consideration of file organization, especially when dealing with multiple file types and destinations. By following this structured approach, you can effectively navigate and utilize the SFTP commands to manage your files efficiently.

Frequently Asked Questions (FAQ)

Can I upload multiple files to different directories at once?


Unfortunately, the SFTP mput command does not support uploading to multiple directories in one command. Each file must be uploaded individually as demonstrated above.

What if I have a lot of files to upload?


If you have numerous files, you might consider writing a shell script to automate the process. Using a loop in a Bash script can simplify your uploads. For instance:

echo 'Uploading files...'

directory=("csv" "xml" "json")
files=("data.csv" "data.xml" "data.json")

for i in {0..2}; do
sftp username@server_address <<< $'put ~/Documents/${files} ${directory}/'
done


This script will efficiently iterate through arrays of file names and directories to upload each file accordingly.

In conclusion, remember to check your server structure and paths for accuracy to avoid errors during the upload process. Happy uploading!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу