SFTP, or Secure File Transfer Protocol (or even SSH File Transfer Protocol), is a network protocol that is used to transfer files, access files, and perform file management actions over any reliable data stream. It has been designed to provide secure file transfer capabilities, leveraging Secure Shell (SSH) cryptographic features to ensure data is safely and confidentially transmitted.
This guide aims to provide a comprehensive list of common SFTP commands that you can use to manage your files securely over a network.
Connecting to the SFTP server
First of all, you have to connect to the remote host (SFTP server). The SFTP connection can be initialized with the following command:
You have to replace the user and host with your details. You will also be asked to type the password of the user. If the details are correct, a secure connection has been established and you securely transfer files.
The cd command
The first SFTP command covered is
cd
which stands for "change directory" and it's used to change the remote working directory on the SFTP server. The syntax for using this command in an SFTP session is:
In this command,
directory
is the path of the directory to which you want to change. After executing it, the new remote directory will be
directory
. The
cd command
is covered more in detail in a separate article.
The chmod command
The
chmod
command, which stands for "change mode", is used to change the permissions of a file or directory. When using an SSH session, the syntax for the
chmod
command is as follows:
In this command,
permissions
is the permission code you want to apply, and
file
is the name of the file or directory you want to change. The
chmod command
is covered in more detail in a separate article.
The chown command
The
chown
command is used to change the ownership of files and directories in Unix-like operating systems. However, the
chown
command is typically not available in SFTP sessions due to security reasons, as changing the owner of a file or a directory is a privileged operation. In SSH, you can use the
chown
command like so:
In this command,
newowner
is the username of the new owner, and
filename
is the name of the file or directory whose ownership you want to change.
The dir command
The
dir
command in SFTP works similarly to the
ls
command. It lists the contents of the current directory on the remote server. If the current directory on the remote server has files named "file1.txt", "file2.txt", and a directory named "mydir", then the output of the
dir
command will be something like:
sftp> dir
file1.txt file2.txt mydir
The
dir command
is covered in more detail in a separate article.
The exit command
The
exit
command in SFTP is used to terminate the current SFTP session and return to your local shell prompt. It is very straightforward to use. The syntax for the
exit
command is:
The
exit command
is covered in more detail in a separate article.
The get command
The
get
command in SFTP is used to download a remote file from the remote system to your local system. It is one of the most commonly used commands in SFTP. The syntax for the
get
command is:
get remoteFile [localFile]
In this command,
remoteFile
is the name of the file on the remote server that you want to download. If
localFile
is specified, the downloaded file will be saved with that name on your local system. If
localFile
is not specified, the downloaded file will keep its original name.
Here's an example. Let's assume there's a file named 'example.txt' on the remote server that you want to download to your local system. You would use the
get
command like so:
After running this command, the file 'example.txt' will be downloaded from the remote server to the current directory on your local system.
The help command
The
help
command in SFTP is used to display a brief description and syntax of SFTP commands. It's a handy tool when you forget the syntax or usage of a command.
To use the
help
command, simply type
help
at the SFTP prompt:
The lcd command
The
lcd
command in SFTP is used to change the present working directory on the local machine. It stands for "local change directory". This command can be helpful if you need to switch between directories on your local machine during an SFTP session.
The syntax for using this command is:
In this command,
directory
is the path of the directory on your local machine to which you want to change. The
lcd command
is described in more detail in a separate article.
The lls command
The
lls
command in SFTP is used to list the contents of the current directory on your local machine. This command stands for "local ls", with "ls" being the Unix command to list directory contents.
When you execute this command, it will list all files and directories in your current local directory.
For instance, if you have the following files and directories in your current directory on your local machine:
file1.txt
,
file2.pdf
, and a directory named
documents
, the output of the
lls
command would look like this:
sftp> lls
file1.txt
file2.pdf
documents
The
lls command
is covered in depth in another article.
The lmkdir command
The
lmkdir
command in SFTP is used to create a new directory on your local system. The "l" in
lmkdir
stands for "local", signifying that this command affects your local machine and not the remote server.
For example, let's say you want to create a new directory named 'newfolder' on your local machine. You would use the
lmkdir
command like so:
After running this command, a new directory named 'newfolder' will be created in the current directory on your local machine. The
lmkdir command
is covered in more detail in a separate article.
The lpwd command
The
lpwd
command in SFTP is used to print the current working directory on your local system. The command stands for "local print working directory", and it is the local counterpart of the
pwd
command, which prints the current directory on the remote server.
When you run this command, it will display the full path to your current local directory. Here's an example:
sftp> lpwd
Local working directory: /Users/username/Documents
The ls command
The
ls
command in SFTP is used to list the contents of the current directory on the remote server. It is equivalent to the
ls
command in Unix-based systems and the
dir
command in SFTP.
Here's a simple example of how to use it:
When you execute this command, it will list all files and directories in the current directory on the remote server.
The mkdir command
The
mkdir
command in SFTP is used to create a new directory on the remote server. This command is very similar to the
mkdir
command you might be familiar with from Unix-based systems.
The syntax for the
mkdir
command is:
In this command,
directory
is the name of the new directory that you want to create on the remote server. The
mkdir command
is described more in detail in a separate article.
The put command
The
put
command in SFTP is used to upload a file from your local machine to a remote server.
The syntax for the
put
command is:
put localFile [remoteFile]
In this command,
localFile
is the name of the file on your local machine that you want to upload. If
remoteFile
is specified, the uploaded file will be saved with that name on the remote server. If
remoteFile
is not specified, the uploaded file will keep its original name. The
put command
is described in more detail in another article.
The pwd command
The
pwd
command in SFTP stands for "print working directory". It displays the current working directory on the remote server. This is the directory on the server where you are currently operating.
The syntax for the
pwd
command is very straightforward. You don't need to provide any arguments; simply type
pwd
:
When you run this command, it will display the full path to your current directory on the remote server. The
pwd command
is described in more detail in another article.
The quit command
The
quit command
in SFTP is used to terminate the current SFTP session and exit the program. The command does not take any arguments. Here's an example of how to use it:
When you run this command, it will close the current SFTP session and you will be returned to your local system's command prompt. It is equivalent to the
exit command
in SFTP.
The rename command
The
rename command
in SFTP is used to rename a file or directory on the remote server.
The syntax for the
rename
command is:
In this command,
oldName
is the current name of the file or directory that you want to rename, and
newName
is the new name that you want to give to the file or directory.
The rm command
The
rm command
in SFTP is used to remove or delete a file from the remote server. This command stands for "remove".
The syntax for the
rm
command is:
In this command,
file
is the name of the file you want to delete from the remote server.
Here's an example. Suppose you're connected to a remote server and you want to delete a file named 'oldfile.txt' from the current directory. You would use the
rm
command like so:
After running this command, the file 'oldfile.txt' will be removed from the current directory on the remote server. If the deletion was successful, the
rm
command does not return any message. If the file could not be deleted (for instance, because it does not exist or you do not have the necessary permissions), an error message will be displayed.
The rmdir command
The
rmdir
command in SFTP is used to remove or delete a directory from the remote server. This command stands for "remove directory".
The syntax for the
rmdir
command is:
In this command,
directory
is the name of the directory you want to delete from the remote server.