Using Mac Terminal as an FTP or SFTP Client

Using Mac Terminal as an FTP or SFTP Client

FTP and SFTP are among the most widely used protocols for transferring files between a local device and a remote server. They’re commonly employed by web developers to push changes to servers. However, Mac users have a powerful built-in tool that enables them to utilize FTP and SFTP protocols for remote server interaction.

This article details how to use Terminal (Mac) as an FTP or SFTP client for various tasks on remote servers. For illustration, I’m using a test server with Linux, Apache, MySQL, and PHP, with SSH access enabled. Learn basic FTP/SFTP tasks like file uploads/downloads, renaming, moving, deleting, using macOS’ Terminal instead of a third-party FTP client.

Note: To use SFTP, SSH access must be enabled on your server. If not, contact your hosting provider or use FTP. However, FTP is generally insecure, so exercise caution.

Logging into the Server

Logging into a remote server is straightforward. You need an FTP/SFTP username and password. FTP may allow anonymous logins, but it’s preferable to authenticate with a username and password.

FTP

The command to log into a remote server with FTP is:

ftp server_ip

Enter your username when prompted, then your password, and you’ll be logged in.

SFTP

The command to log into a remote server with SFTP is:

sftp username@server_ip

Enter the password when prompted, and you’ll be logged in.

1. Uploading and Downloading Files

One basic function of an FTP/SFTP client is to upload files from the local host to the remote server and download files from the remote server.

Using FTP or SFTP

The command to upload files to a remote server is:

put path_to_local_file remote_file

For example, to upload a file named index.txt:

put /Users/akshaygangwar/index.txt index.txt

This command puts the file “index.html” from the home directory into the working directory on the remote server.

  • Download Files

The command to download files from a remote server is:

get path_to_remote_file local_file

For example, to download a file named newfile.txt:

To download a file named “newfile.txt” from the remote server’s working directory to my Mac’s working directory, use:

get newfile.txt newfile.txt

Creating a New Folder

FTP clients handle the crucial task of creating folders (directories) on remote servers.

Using FTP or SFTP

Creating a new folder via Terminal is simple. The command remains consistent across both FTP and SFTP protocols:

mkdir directory_name

For instance, to create a folder named “Beebom”, use:

This command establishes the “Beebom” folder in the remote server’s working directory.

Renaming Files on the Server

To rename files on the remote server, employ the Terminal as a client for easy execution.

FTP or SFTP

To rename files remotely via Terminal as an FTP/SFTP client:

rename old_name new_name

For instance, to change “newfile.txt” to “mainlog.txt”:

rename newfile.txt mainlog.txt

This action alters “newfile.txt” to “mainlog.txt”.

4. Deleting Files

Terminal allows file deletion from the remote server, with distinct commands for FTP and SFTP:

FTP

To delete files remotely via FTP:

delete file_name

For instance, to delete “beebomold.txt”:

Delete the file “beebomold.txt” from the remote server using SFTP:

rm beebomold.txt

For instance, to delete “beebomold.txt” via SFTP:

Delete the file “beebomold.txt” from the remote server.

5. Moving Files within the Remote Server

Utilizing Terminal as an FTP client enables moving files within the remote server akin to third-party FTP clients.

Using FTP or SFTP

Using Mac Terminal as an FTP or SFTP Client

The command for moving files within the server, applicable to both FTP and SFTP, is:

rename file_name path_to_new_file/file_name

For instance, to relocate “testresults.txt” from the “test” directory to “results”, use the command:

rename testresults.txt results/testresults.txt

This action transfers “testresults.txt” to the sub-folder “results”.

6. Review “Last Modified” Date

Reviewing the “Last Modified” date of a file or folder is helpful to track recent updates. This can be done via Terminal.

Using FTP or SFTP

To check the last modified date of a file, execute:

ls -l file_name

This command presents data in tabular format. The column containing date and time corresponds to the “Last Modified” value.

To check the last modification date of “testresults.txt,” use:

ls -l testresults.txt

7. Verify and Adjust Permissions

Correct file permissions are crucial. Incorrect permissions can prevent your web app from loading.

Using FTP or SFTP

Verifying and adjusting permissions via Terminal as a client is simple:

ls -l file_name

This command presents information in a table. The first column shows file permissions.

For instance, to check permissions on “testresults.txt,” execute:

ls -l testresults.txt

If you encounter a file with incorrect permissions or wish to adjust permissions, Terminal provides the means to modify them:

chmod permissions_value file_name

For instance, to grant full read, write, and execute permissions to “testresults.txt”, use:

chmod 777 testresults.txt

This grants the specified permissions to “testresults.txt”.

8. Creating New Files

Creating new files on the server via Terminal isn’t straightforward but remains feasible. The challenge lies in having a local copy before uploading to the server.

Using FTP or SFTP

To create a file on the remote server, use:

!touch file_name

put file_name file_name

For instance, to create “newtest.txt” on the server:

put newtest.txt newtest.txt

This action will generate and upload “newtest.txt” to the server.

9. Edit Files

Editing files is crucial. You can edit files in the Terminal using programs like nano or emacs, which are built-in. Nano is simpler, and I’ll demonstrate its use.

Using FTP or SFTP

To edit existing files on the remote server, utilize:

get file_name file_name

!nano file_name

put file_name file_name

For instance, if you need to edit the file “newtest.txt”, the commands will be:

get newtest.txt newtest.txt

put newtest.txt newtest.txt

These commands will edit and upload the file “newtest.txt” back to the server.

10. Duplicating Files

While editing files on the remote server, it’s wise to keep an original copy in case of mistakes.

Using FTP or SFTP

To duplicate a file on the remote server, use these commands:

get file_name file_name

!mv file_name new_file_name

put new_file_name new_file_name

For example, to create a duplicate named “newtest_copy.txt” of “newtest.txt”, execute:

Harness the Power of Mac Terminal with FTP or SFTP

Now, armed with the knowledge of using Terminal as an FTP or SFTP client, you can seamlessly access your development server. Avoid concerns about third-party applications introducing bloatware or compromising your traffic security. If you encounter any difficulties with FTP or SFTP via Terminal or believe we’ve overlooked something, share your thoughts in the comments below.