How to Use the Mac Terminal as an FTP or SFTP Client

Two of the most used protocols for exchanging data between a local device and a distant server are File Transfer Protocol (FTP) and Secure File Transfer Protocol (SFTP). There are many FTP clients available since web developers commonly utilise them to upload modifications to their servers. But, a Mac also comes with a rather potent tool that enables users to connect to distant servers using the FTP and SFTP protocols.

This article will explain how to utilise the Mac’s Terminal as an FTP or SFTP client to carry out a number of activities on distant servers. I’m using a test server with Linux, Apache, MySQL, and PHP installed on it for illustration purposes, along with SSH access turned on. Instead of utilising a third-party FTP software, I’ll show you how to use the macOS Terminal to perform common FTP/SFTP functions including uploading and downloading files, renaming, moving, and deleting files, among others.

Note:Your server must have SSH access enabled in order to use SFTP. You can speak with your hosting company or utilise FTP if you don’t have access to SSH. However be cautious because FTP is typically not seen of as secure.

Logging into the Server

The remote server can be accessed rather easily. To access the server, you’ll need an FTP/SFTP login and password. Although FTP may permit anonymous logins, it is preferable to authenticate with a username and password.

Using FTP

The following is the FTP command to access a remote server:

server ip for ftp

After that, you will be asked for your login; enter it, then press Enter. The Terminal will then prompt you for your password; enter it and press Enter to log you in.

Using SFTP

To access a remote server via SFTP, enter the following command:

username@server ip in sftp

The password will then be requested of you. To sign in, type it in and press Enter.

1. Uploading and Downloading Files

The ability to upload files from the local host to the distant server and retrieve files from the remote server is one of the fundamental features of an FTP/SFTP client.

Using FTP or SFTP

  • Upload Files

The following command is to upload files to a distant server:

the path to a local file and the remote file

As an illustration, if I wanted to upload the file index.txt, the command would be:

place index.txt in the Users/akshaygangwar directory.

This command will transfer the index.html file from my home directory to the remote server’s working directory.

To determine your working directory, use the pwd command.

  • Download Files

The following programme will download files from a distant server:

Get the distant file path and local file

To download a file named newfile.txt, as an illustration, the command would be:

gain access to newfile.txt

This command will transfer the newfile.txt file from the remote server’s working directory to my Mac’s working directory.

2. Creating a New Folder

Another significant activity carried out by FTP clients is the creation of folders (directories) on a distant server.

Using FTP or SFTP

It’s simple to make a new folder in the Terminal. The following command is the same in the SFTP and FTP protocols:

directory name: mkdir

For instance, if I wanted to make a folder called Beebom, the command would be:

set up Beebom

In the working directory on the remote server, this will create a folder with the name Beebom.

3. Renaming Files on the Server

Using the Terminal as a client makes renaming files on the distant server simple.

Using FTP or SFTP

The following command can be used to rename files on a remote server using the Terminal as an FTP/SFTP client:

new name replace old name

For instance, if I wanted to rename newfile.txt to mainlog.txt, I would use the following command:

rename mainlog.txt to newfile.txt

The file newfile.txt will be renamed to mainlog.txt as a result.

4. Deleting Files

You can also delete files from the remote server using the Terminal. I’m providing both the FTP and SFTP commands separately because they differ in this situation.

Using FTP

Using FTP, the following command will remove files from a distant server:

to remove file name

For instance, the command will change to: if I wished to remove the file beebomold.txt.

Get rid of Beebomold.txt

By doing this, the remote server’s beebomold.txt file will be removed.

Using SFTP

Using SFTP, the following command will remove files from a remote server:

file name rm

For instance, the command might be as follows if I wanted to use SFTP to delete the file beebomold.txt:

Beebomold.txt, rm

The remote server’s beebomold.txt file will be removed after this.

5. Moving Files within the Remote Server

You can move files within the remote server itself using the Terminal as an FTP client, just like you would with a third-party FTP client.

Using FTP or SFTP

With both FTP and SFTP, the following command is used to move files within the server:

path to new file/file name rename file name

To move a file named testresults.txt from the test directory to the results directory, for instance, the command would be:

rename results/testresults.txt to testresults.txt

This will transfer the testresults.txt file to the results subfolder.

6. Check Last Modified Date

If you need to know which files and folders were updated when, checking the Last Modified date for a file or folder can be helpful. This is also possible through the Terminal.

Using FTP or SFTP

The following command can be used to see when a file was last modified:

file name, ls -l

This command shows certain data in a table format. The Last Modified value is matched by the column containing the date and time information.

For instance, the command would be as follows if I wanted to determine when testresults.txt was last modified:

testresults.txt with ls -l

7. Check and Modify Permissions

It’s crucial that files have the correct permissions configured. Inappropriate permissions may occasionally prevent your web app from even loading.

Using FTP or SFTP

  • Checking Permissions

The command to use while using the Terminal as a client to check and alter permissions is:

file name, ls -l

This command shows certain data in a table format. The file’s permissions are shown in the first column.

As an illustration, I would use the command as follows to verify the permissions on the file testresults.txt:

testresults.txt with ls -l

  • Modifying Permissions

You can use the Terminal to change a file’s rights if you notice that it has the wrong permissions or if you simply wish to experiment. The order is:

chmod file name permissions value

For instance, if I wanted to grant the file testresults.txt full read, write, and execution permissions, the command would be

testresults.txt is chmod 777.

This command will provide the testresults.txt file read, write, and execute permissions.

8. Create New Files

On the Terminal, it is difficult to perform tasks like creating new files on the server. That does not, however, imply that it cannot be done. A problem with producing new files is that you can’t upload them to the server until you have a copy of the file on your laptop.

Using FTP or SFTP

The following commands are used to generate files on the remote server:

!

file name touch

include file name file name

For instance, the commands will be as follows if I wish to create the file newtest.txt on the server:

!

the newtest.txt file

9. Edit Existing Files

newtest.txt should be entered.

Using FTP or SFTP

By doing this, a new file with the name newtest.txt will be created and uploaded to the server.

Another crucial feature is the ability to edit existing files. With tools like nano, emacs, and other ones that are already included in the Terminal, you can edit a file right in the terminal itself. As Nano is easier to comprehend, I’ll use it in this example.

The following commands can be used to modify current files on the remote server:

obtain filename filename

!

text file name

include file name file name

For instance, the commands will be as follows if I want to change the file newtest.txt:

gain access to newtest.txt

10. Creating Duplicate Copies of Files

!

Using FTP or SFTP

nan newtest.txt

newtest.txt should be entered.

The newtest.txt file will be edited by these commands before being uploaded once more to the server.

It is preferable to have a copy of the original file while altering files on a remote server, just in case you make a mistake.

The following commands should be used to duplicate a file on the remote server:

obtain filename filename

!

New File Name: mv File Name

replace new file name with new file name.

Harness the Power of the Mac Terminal with FTP or SFTP

For instance, the commands will be as follows if I wish to make a copy of newtest.txt called newtest copy.txt: