Megatools Linux install and Basic Introduction

This tutorial will deal with Megatools Linux install and Basic Introduction. MEGA is one of the most famous cloud storage and file hosting services available. The service offered by the company are normally accessible via web interface or dedicated applications also on smartphone operating systems such as Android or iOS. In this article we see how to access the service from the command line via a free and open source set of tools written in Python: Megatools.

In this tutorial you will learn:

  • How to install Megatools
  • How to check the available, free and used storage space
  • How to list files and directories on the remote storage
  • How to upload and download files and directories
  • How to create and remove remote files and directories
  • How to specify information in the ~/.megarc configuration file

 

Megatools Linux install and Basic Introduction
Megatools Linux install and Basic Introduction

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software The Megatools suite
Other Administrative privileges in order to install software globally
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Megatools Linux install

The Megatools suite of applications includes a series of utilities which let us performs operations such as moving and downloading files and directories, or streaming video and audio content. As we already said, the project is absolutely free and open source, so it is really easy to install it on some of the most used Linux distributions such as Debian (and its derivatives) or Fedora. To install the application on the Debian family of distributions we can launch the following command:

$ sudo apt-get update && sudo apt-get install megatools

Megatools Linux install on Fedora we can use the dnf package manager instead. Here is how we invoke it:

$ sudo dnf install megatools

Being Megatools written in Python, a distribution-independent way to install it, is by using pip: the Python package manager. This tool allows us to obtain the software even in those distributions which don’t have it available in their official repositories. To perform a per-user installation of Megatools using pip, we can run:

$ pip install megatools --user

Since the command above installs the software in the user $HOME directory (that is what the --user option is for), we don’t need to use administrative privileges when we run it.

The Megatools utilities

Once Megatools is installed in our system, we can take a quick look at all the utilities included in the suite.

Checking the available space with megatools df

The megatools df utility can be used to show a report of the total, free and used space on the Mega platform. To get a full report we can invoke the program in the following way:

$ megatools df -u 
Enter password for ():
Good, signing in...
Total: 21474836480
Used:  1496628
Free:  21473339852

In the previous example, we invoked megatools df using the -u option (short for --username). This option is common to all the utilities included in the Megatools suite, since it is used to provide our credentials. When the program is used this way, the password it’s asked interactively in a command line prompt. Once we provide it, the report is displayed onscreen, however not in a very human-friendly way. If we want the data to be displayed in a more human-friendly format, we should add the -h option (--human):

Total: 20.0 GiB
Used:  1.4 MiB
Free:  20.0 GiB

Although not a very good idea from a security point of view, to avoid having to provide the account password interactively, we can pass it as an argument to the --password option:

$ megatools df -u  -p

Credentials can also be stored into the ~/.megarc configuration file, in order to avoid having to repeat each time we run a command. We will talk about it later.

Listing files and directories with megatools ls

The megatools ls utility is used to list the files stored on the Mega.nz platform and optionally visualize the public download links. Let’s see an example of its usage. To list all files in the root of the cloud storage we would run:

$ megatools ls -u 
Enter password for ():
Contacts
/Inbox
/Root
/Root/Welcome to MEGA.pdf
/Root/testdir
/Trash

If we want the public download link for the shared files to be included in the output, all we have to do is to invoke the command with the -e option (--export). The links will be displayed in the following way (in this case you can notice that only the “Welcome to MEGA.pdf” file is shared):

                                                                       /Contacts
                                                                       /Inbox
                                                                       /Root
https://mega.nz/#!znIXiQha!isXGprskZbLP4KnLNuNHcbI279s6FnLcsj8Vydm_sio /Root/Welcome to MEGA.pdf
                                                                       /Root/testdir
                                                                       /Trash

To include additional information in the output of the command, we can use the -l option (--long), while to include headers information, we can use the --header option. Including both of them, makes the utility produce an output similar to the following:

===================================================================================
Handle      Owner       T          Size Mod. Date           Path
===================================================================================
NETWORK                 9             -                     /Contacts
mypkWT7D                3             - 2021-08-26 16:05:33 /Inbox
Sqo02b4J                2             - 2021-08-26 16:05:33 /Root
uih02ZCR    xxxxxxxxxxx 0       1496628 2021-08-26 16:05:33 /Root/Welcome to MEGA.pdf
KrpWCZoa    xxxxxxxxxxx 1             - 2021-08-26 16:16:57 /Root/testdir
byhETDSZ                4             - 2021-08-26 16:05:33 /Trash

Uploading and downloading files and directories

Two command line utilities allow us to upload and download files, they are, respectively: megatools put and megatools get. Let’s see how they work. Uploading files with megatools put is quite simple. Suppose we have a local file called linuxconfig.txt and we want to upload it in the root of the cloud storage; we would run the following command:

$ megatools put -u  linuxconfig.txt

If we want to upload one or more files into a specific remote directory we must use the --path option, which takes as argument the remote path where the file(s) should be uploaded to. To upload the linuxconfig.txt file into the remote testdir directory, we would run:

$ megatools put -u  --path /Root/testdir linuxconfig.txt

If we want to perform the opposite operation, so to download files from the cloud storage into our local filesystem, we should use the megatools get command. Supposing we want to download the linuxconfig.txt file, we would run:

$ megatools get -u  /Root/linuxconfig.txt

To download a remote file to a specific local directory, we provide the --path option and pass as argument the local path. To download the linuxconfig.txt file into the ~/Downloads directory, for example, we would run:

$ megatools get -u  --path ~/Downloads /Root/linuxconfig.txt

The above commands operates only on standard files. To upload and download the content of directories, we have to use another utility which is part of the Megatools suite: megatools copy. The default action of the command is to upload files to the cloud storage, so, for example, to upload the content of a directory called linuxconfig-dir, we would run:

$ megatools copy -u  --local linuxconfig-dir --remote /Root

The command does not replicate the directory itself remotely: it just tries to sync its content to the specified remote path. The result of running the command above will be that all of the content of the linuxconfig-dir directory will be “copied” into the /Root of the cloud storage. If we would like to sync the content of the directory into a directory with the same name remotely, we should create first. We will see how to do it in the next section.

If we want to copy the content of a remote directory locally, we have to launch the same command, but we must add the --download option. For example, to copy all the content of the remote /Root directory in our current local working directory we would run:

$ megatools copy -u  --local . --remote /Root --download

Creating and removing files and directories

In the Megatools suite we can find two utilities which allow us to create files and remove/create files and directories. They are respectively, megatools mkdir and megatools rm. To create one or more remote directories in our cloud storage we use megatools mkdir. Let’s see an example. Suppose we want to create two remote directories in the root of the cloud storage: linuxconfig_0 and linuxconfig_1. We would run:

$ megatools mkdir -u  /Root/linuxconfig_0 /Root/linuxconfig_1

To remove existing files and directories, instead, we would use the megatools rm command. Say for example we want to remove the directories we create in the previous example. We would run:

$ megatools rm -u username@gmail.com /Root/linuxconfig_0 /Root/linuxconfig_linuxconfig_1

The Megatools configuration file

The utilities included in the Megatools suite uses a common configuration file: ~/.megarc. It is an .ini style file, and can be used to specify a series of information. We can use it to specify, among the other things, the access credentials, to avoid having to repeat them each time we launch a command. Said information can be specified in the [Login] stanza, for example:

[Login]
Username = _yourusername_
password = _yourpassword_

In the [Network] section, instead, we can specify, for example, the values of the SpeedLimit and ParallelTransfers settings, which determines respectively the maximum allowed upload and download speed (KiB/s), and the maximum allowed number of parallel connections when uploading or downloading files:

[Network]
SpeedLimit = 1024 # 1 MiB/s
ParallelTransfers = 2

Always keep in mind that the file is in clear, so store your credentials in it only if you can trust the machine you are operating on. Further information about the Megatools configuration file can be found by consulting the dedicated manpage.

Conclusions

In this Megatools Linux install tutorial we talked about the Megatools suite of utilities which allow us to connect and interact with the Mega.nz cloud storage from the command line. The project is free and open source, written in Python and is independently developed from the aforementioned cloud storage. We saw how easy is it to create and remove files and directories, how to upload and download them, how to check the available, free, and used storage space and how to obtain a list of all the files contained in the cloud. Finally, we saw how to use the ~/.megarc configuration file.



Comments and Discussions
Linux Forum