Transferring files between 2 linux servers

Have you ever needed to transfer a file from one headless linux server to another?
Well it’s actually quite simple. You can do so using the scp command.

scp <file> <username>@<IP address or hostname>:<Destination>

So lets say you are logged into Server1

pawel@server1:$

If you “ls -l” you will see a file called myFileToTransfer.txt

To transfer it to Server2, you would write:

pawel@server1:$ scp myFileToTransfer.txt pawel@server2:/home/pawel/

Server2 will ask you for password for user pawel, and once you enter it, the transfer will begin.

du, df & ncdu

du

“du” – or “disk usage” is a useful Linux command especially if you want to know what is taking up space on your hard drive in command line. It is used to check the usage information of files and folders on a disk. The command has a few parameters that can be seen by running “man du” from the terminal window. Here are some example commands that I found useful:

du -h /home/folder – print sizes in human readable format (e.g., 1K 2.8G) See example below to show the difference:

pawel@pawel-ubuntu:/# du /home/folder
2052 /home/folder/secondfolder
1052684 /home/folder
pawel@pawel-ubuntu:/# du -h /home/folder
2.1M /home/folder/secondfolder
1.1G /home/folder

If you’re looking for a summary of the usage of a folder, you can perform this by running the following command:

du -sh /home/folder – display only a total for each argument. See example below:

pawel@pawel-ubuntu:/# du -sh /home/folder
1.1G    /home/folder

Notice that the 2.1Mb file did not show up in the summary, as the summary usually displays the largest blocks. If you’d like, you can change that by adding -b (for Byte blocks), -k (for Kilobyte blocks) or -m (for Megabyte blocks). Below is an example output of the summary of folder in Megabytes:

pawel@pawel-ubuntu:/# du -sm /home/folder
1029    /home/folder


df

“df” – or “disk filesystem” is a built-in utility that allows you to check disk space utilization in Linux. It displays the device name, total blocks, total disk space along with used and available space, use percentage and the mount points on the filesystem. This is particularly useful if you have different mount points especially on a shared drive or storage device. You can see many of the options for df by typing “man df” in your terminal, but I found that df -h is the most useful command for me. Below is an example output of the command:

pawel@pawel-ubuntu:/# df -h
Filesystem      Size    Used    Avail    Use%   Mounted on
udev                  3.9G        0      3.9G         0%   /dev
tmpfs              790M  9.6M  780M         2%  /run
/dev/sda1      286G    9.7G   262G         4%  /
tmpfs               3.9G    148K    3.9G          1%  /dev/shm
tmpfs              5.0M    4.0K    5.0M         1%  /run/lock
tmpfs               3.9G        0        3.9G        0%  /sys/fs/cgroup
tmpfs             790M     36K   790M        1%  /run/user/112


ncdu

“ncdu” – or “NCurses Disk Usage” is a curses-based version of the well-known ‘du’, and provides a fast way to see what directories are using your disk space. To note, this utility does not come pre-installed with Linux, so it needs to be installed. You can do so by running yum (Fedora) or apt-get (Debian/Ubuntu) command:

sudo yum install ncdu
sudo apt-get install ncdu

When you first run ncdu the utility will scan the sub-directories and count measure the space used by each sub-directory:

ncdu scanOnce the scan is complete, you will be presented with a screen that will allow you to use menus to get additional information about each sub-directory. You can learn more about this utility by pressing the “?” and using the up and down arrow keys to see all the menu options:

ncdu menuA complete manual for ncdu command can be found here: Ncdu Manual