Command to copy file from linux to linux

5 commands to copy file from one server to another in Linux or Unix

Table of Contents

In my last article I shared the steps to encrypt a file using gpg key in Linux. Now in this article I will share various commands and tools which you can use to securely copy file from one server to another in Linux. There are additionally other ways to transfer files which I cannot cover here for example you can also use HTTPS to upload and download files.

I may write another article with detail list of steps to use HTTPS and curl for secure file upload and download.If you wish to copy files between Windows and Linux then you can always use Samba but since here we are targeting file transfer between two Linux machines, I will not share any steps related to Samba configuration.

Some more articles on related topic you may be interested in

Using SFTP to copy file from one server to another

In computing, the SSH File Transfer Protocol (also Secure File Transfer Protocol, or SFTP) is a network protocol that provides file access, file transfer, and file management over any reliable data stream.

SFTP is easy to work with: You enter sftp along with the name of the remote system on the command line. You are prompted for the account password ; then you are dropped into SFTP with the connection open and waiting.

Once you give the password of deepak user on target node, you will get sftp shell

Next to copy file from host to client (i.e. upload a file from host to client)
Here I have a file ‘ pwd.txt ‘ on my host server under ‘ /home/deepak/pwd.txt ‘ which I wish to copy to my client’s current working directory

To copy a directory and all it’s content use (-r). Here /home/deepak/mydir is available on my host machine which I am copying to the connected client node under current working directory.

So the file was successfully uploaded. You can verify the same

Next copy a file from client node to your host server. I have a file ‘test_file’ on my client node under ‘/home/deepak/test_file’

Validate the same on your host server

You can get more supported options from the man page of sftp .

Using RSYNC to copy file from one server to another

rsync is a utility that you can use to copy file from one server to another very easily, and there are many options available to allow you to be very specific about how you want the data transferred. Another aspect that makes rsync flexible is the many ways you can manipulate the source and target directories. However, you don’t even have to use the network; you can even copy data from one directory to another on the same server.

Copying a file within the same server from one location to another

Here we are using -r to copy recursively, you can also use ( -a ) i.e. for archive which retains as much metadata as possible (in most cases, it should make everything an exact copy).

To copy files between two servers

Using SCP to copy file from one server to another

A useful alternative to rsync is the Secure Copy (SCP) utility to copy file from one server to another, which comes bundled with OpenSSH. It allows you to quickly copy files from one node to another. If your goal is to send a single file or a small number of files to another machine, SCP is a great tool you can use to get the job done. To utilize SCP, we’ll use the scp command. Since you most likely already have OpenSSH installed, you should already have the scp command available

Читайте также:  Windows 10 pro education что означает

Using SCP is very similar in nature to rsync. The command requires a source, a target, and a filename. To transfer a single file from your local machine to another, the resulting command would look similar to the following:

If you do not specifiy the target directory while doing scp, then the home directory of the target user will be used are destination.

With our previous scp examples, we’ve only been copying a single file. If we want to transfer or download an entire directory and its contents, we will need to use the -r option, which allows us to do a recursive copy:

Using NFS to share file from one server to another

A Network File System (NFS) is a great method of sharing files between Linux or UNIX servers. I have written another article with detailed steps to setup NFSv4 and NFSv3 with examples in RHEL/CentOS 7 and 8 Linux

On my RHEL node I have installed nfs-utils and now will setup my exports. To set up NFS, let’s first create some directories that we will share to other users. Each share in NFS is known as an Export .

  • In the first line I have given share access to world for /share directory.
  • In the second line, after the directory is called out in a line, we’re also setting which network is able to access them ( 10.0.2.0/255.255.255.0 in our case). This means that if you’re connecting from a different network, your access will be denied.
  • As far as what these options do, the first ( rw ) is rather self-explanatory.
  • One option you’ll see quite often in the wild is no_root_squash . Normally, the root user on one system will get map to nobody on the other for security reasons. In most cases, one system having root access to another is a bad idea. The no_root_squash option disables this, and it allows the root user on one end to be treated as the root user on the other. .

Next restart your nfs-server services on the server

To check the list of shares currently exported

Now try to mount the directory /share from the client side

So our directory mount is successful, Next validate the content

We can validate the same on our client node using below command

After successfully mounting the share on the client node, you can copy the file locally to your node.

Using SSHFS to copy file from one server to another

SSH Filesystem (SSHFS) is a file sharing solution similar to NFS and Samba. NFS and Samba are great solutions for designating file shares but these technologies may be more complex than necessary if you want to set up a temporary file-sharing service to use for a specific period of time. SSHFS allows you to mount a remote directory on your local machine, and have it treated just like any other directory. The mounted SSHFS directory will be available for the life of the SSH connection and can be used to copy file from one server to another.

Drawbacks of using SSHFS

  • Performance of file transfers won’t be as fast as with an NFS mount, since there’s encryption that needs to be taken into consideration as well
  • Another downside is that you’d want to save your work regularly as you work on files within an SSHFS mount, because if the SSH connection drops for any reason, you may lose data.

SSHFS is part of EPEL repository, which you can install using yum

For SSHFS to work, we’ll need a directory on both your local Linux machine as well as a remote Linux server. SSHFS can mount any directory from the remote server where you have SSH access.

Here I am mounting /share from node2 on node1

Now validate the content of /mnt and make sure the path is properly mounted

Now you can copy the files from /mnt locally to any other directory

Once your copying is complete, manually unmount the respective directory. There are two ways to do so. First, we can use the umount command as the root (just like we normally would):

You can also use HTTPS for file sharing, although it would be more like of file uploading and downloading via GET and PUT using curl command.

Читайте также:  Драйвера для звуковых карт windows 10 что это

Lastly I hope the commands from this article to copy file from one server to another in Linux or Unix was helpful. So, let me know your suggestions and feedback using the comment section.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

10 thoughts on “5 commands to copy file from one server to another in Linux or Unix”

Lovely just what I was searching for.Thanks to the author for taking his clock time on this one.

Excellent post. Keep writing such kind of info on your
blog. Im really impressed by your site.

This is the right site for everyone who hopes to understand this topic.
You know so much its almost tough to argue with you (not that I really would want to…HaHa).
You certainly put a brand new spin on a subject that’s been written about for decades.
Great stuff, just wonderful!

Can we transfer 20tb data of file from one system to anothersystem

Is it possible to transfer 20TB data of file from one system to another system using scp

20TB is a big data, theoretically I don’t see any problem. I would recommend using rsync as it will perform incremental copy. But copying such large chunk of data then you need heave bandwidth, memory resources in your system. With rsync atleast you can be safe to pick up from where you lost in case the transfer fails during the transaction.

Can we copy one public key for many systems for passwordless login to another systems

Hello, congratulations on the blog.
To big data, such as 500Gb or 1Tb, rysnc is better than scp. Have you used it to perform backups throughout the day? How to improve throughput, or bandwidth? Thanks

Hello, thank you for the feedback
In our production environment also we perform transfer of multiple file each 500GB+
Now generally when you attempt to copy such big file, the copy tool (rsync or any other tool) will eat up all the available bandwidth so it is very important that you assign a BW limit. In our network we have pre-defined a restriction of 1Gb so that other applications using BW don’t get impacted
You can use —bwlimit to limit the BW with rsync, also —compare-dest to make sure only newer data is copied (assuming the transfer was stopped due to some reason)

Источник

Копирование файлов в Linux

Копирование файлов — одна из задач, наиболее часто возникающих перед пользователями персонального компьютера. Конечно, можно открыть файловый менеджер, войти в нужную папку и скопировать файл с помощью контекстного меню — тут не о чем говорить. Но в этой статье я хотел бы рассмотреть копирование файлов в Linux с помощью терминала.

Не всегда есть доступ к файловому менеджеру: из-за различных поломок графическая оболочка на домашнем компьютере может быть недоступна, а на серверах используется только консольный интерфейс. К тому же копирование файлов Ubuntu через терминал намного эффективнее, и вы сами в этом убедитесь. Сегодня мы рассмотрим не только обычное копирование командой cp Linux, но и не совсем обычное: с помощью tar и find.

Утилита копирования файлов cp

Название утилиты cp — это сокращение от Copy, что означает копировать. Утилита позволяет полностью копировать файлы и директории.

Синтаксис и опции

Общий синтаксис cp выглядит вот так:

$ cp опции файл-источник файл-приемник

$ cp опции файл-источник директория-приемник/

После выполнения команды файл-источник будет полностью перенесен в файл-приемник. Если в конце указан слэш, файл будет записан в заданную директорию с оригинальным именем.

Утилита имеет несколько интересных опций, которые могут сильно помочь при нестандартных задачах копирования, поэтому давайте их рассмотрим:

  • —attributes-only — не копировать содержимое файла, а только флаги доступа и владельца;
  • -f, —force — перезаписывать существующие файлы;
  • -i, —interactive — спрашивать, нужно ли перезаписывать существующие файлы;
  • -L — копировать не символические ссылки, а то, на что они указывают;
  • -n — не перезаписывать существующие файлы;
  • -P — не следовать символическим ссылкам;
  • -r — копировать папку Linux рекурсивно;
  • -s — не выполнять копирование файлов в Linux, а создавать символические ссылки;
  • -u — скопировать файл, только если он был изменён;
  • -x — не выходить за пределы этой файловой системы;
  • -p — сохранять владельца, временные метки и флаги доступа при копировании;
  • -t — считать файл-приемник директорией и копировать файл-источник в эту директорию.
Читайте также:  С момента активации windows

Примеры копирования файлов в linux

Теперь, когда вы знаете основные опции, можно перейти к практике. Например, мы хотим скопировать некую картинку из домашней папки в подкаталог pictures:

Или можем явно указать имя новой картинки:

Копирование папок осуществляется с помощью ключа -r:

После выполнения этой команды копирования

/папка будет скопирована в папку

/Документы. Главное, не забывайте поставить слэш в конце выражения или использовать опцию -t. Иначе папка

/документы будет перезаписана.

По умолчанию команда cp Linux перезаписывает существующие файлы или папки, но можно заставить утилиту спрашивать, нужно ли перезаписывать каждый файл, если вы не уверены в правильности составления команды:

Есть и противоположная опция -n, означающая «никогда не перезаписывать существующие файлы».

Опция -u полезна в следующем случае: вы знаете или предполагаете, что в директории, куда копируется файл, есть старая его версия, тогда оператор -u выполнит замену на новую версию:

Сp также поддерживает специальные символы замены * и ?. Например, следующая команда скопирует все файлы, начинающиеся на test:

Если нужно применить более сложные регулярные выражения, придётся комбинировать утилиту cp с find или egrep.

В случае, если важно сохранить права доступа к файлу и его владельца, нужно использовать опцию -p:

Для упрощения использования команды можно применять синтаксис фигурных скобок. Например, чтобы создать резервную копию файла, выполните:

Будет создан файл с таким же именем и расширением .bak

По умолчанию в cp не отображается прогресс копирования файла, что очень неудобно при работе с большими файлами, но его можно легко посмотреть с помощью утилиты cv.

Копирование файлов по регулярным выражениям в Linux

В утилите find можно применять различные условия и регулярные выражения для поиска файлов. Я уже немного писал о ней в статье как найти новые файлы в Linux. Мы можем скопировать все найденные с помощью find файлы, вызвав для каждого из них команду cp. Например, копируем все файлы в текущей директории, содержащие в имени только цифры:

find . -name 8 -exec cp <>

Здесь точка указывает на текущую директорию, а параметр name задает регулярное выражение. Параметром exec мы задаем, какую команду нужно выполнить для обнаруженных файлов. Символ <> — подставляет имя каждого файла.

Но не find‘ом единым такое делается. То же самое можно получить, запросив список файлов директории в ls, отфильтровав его по регулярному выражению egrep и передав имена файлов по очереди в cp с помощью xargs:

/ | egrep ‘[a-zA-Z]’ | xargs cp -t

Это не совсем удобный способ копировать файлы Linux, но всё же он возможен. Будут скопированы все файлы из домашней директории, содержащие в имени только английские буквы.

Копирование содержимого файлов в Linux

Вы можете не только копировать сами файлы, но и управлять их содержимым. Например, склеить несколько файлов в один или разрезать файл на несколько частей. Утилита cat используется для вывода содержимого файла, в комбинации с операторами перенаправления вывода Bash вы можете выполнять копирование содержимого файла Linux в другой файл. Например:

cat файл1 > файл2

Если файл был не пустым, он будет перезаписан. Или мы можем склеить два отдельных файла в один:

cat файл1 файл2 > файл3

Специальное копирование файлов в Linux с помощью tar

Linux интересен тем, что позволяет выполнять одно и то же действие различными путями. Копирование в Linux тоже может быть выполнено не только с помощью cp. При переносе системных файлов в другой каталог, резервном копировании системных файлов и т.д. важно чтобы сохранились атрибуты, значения владельцев файлов и символические ссылки как они есть без какой-либо модификации.

Утилита cp тоже может справиться с такой задачей? если указать опцию -p, но можно использовать утилиту архивации tar. Мы не будем создавать никаких файлов архивов, а построим туннель. Первая часть команды пакует файл и отправляет на стандартный вывод, а другая сразу же распаковывает в нужную папку:

tar cf — /var | ( cd /mnt/var && tar xvf — )

Здесь мы полностью копируем содержимое папки /var в папку /mnt/var. Так вы можете копировать папку Linux, причём абсолютно любую или даже целую операционную систему.

Выводы

Теперь вы знаете, как выполняется копирование файлов Ubuntu и в Linux в целом. Как видите, в терминале это выполняется намного быстрее и эффективнее, чем с помощью графического интерфейса, если помнить нужные команды. Если у вас остались вопросы, спрашивайте в комментариях!

Источник

Оцените статью