This series of articles will introduce common commands used in Linux/Unix/macOS, including a brief introduction to the command, common parameter description, and best practices. If you need to use this command quickly, skip to the best practices section.

Linux Commands in Detail

  • Linux command description: SFTP

Introduce a,

Secure File Transfer Protocol (SFTP) is a network Transfer Protocol that provides File access and management based on reliable data stream. Figure 1 shows its structure at the network Protocol layer.

SFTP provides a more secure file transfer mode between the client and server than FTP. If you use FTP for file transfer, you are strongly advised to switch to SFTP, which is more secure.

This article introduces links to SFTP, some common commands on the interactive command line, an explanation of some of the parameters, and best practices in use. Many GUI clients already support SFTP, but it is beyond the scope of this article.

2. Use SFTP for connection

Because SFTP is based on THE SSH protocol, the default authentication method is the same as the SSH protocol. If you already have access to a remote server using SSH, you can use the following command to connect to SFTP:

1
Copy the code
sftp user_name@remote_server_address[:path]Copy the code

If the remote server has a custom port to connect to, you can use the -p parameter:

1
Copy the code
sftp -P remote_port user_name@remote_server_address[:path]Copy the code

After the connection is successful, an SFTP interpreter is displayed, and the cli prompt changes to SFTP >. You can run the exit command to exit the connection.

If the connection address has path and path is not a directory, SFTP retrieves the file directly from the server.

3. Detailed explanation of connection parameters

  • -B: buffer_size specifies the size of the buffer to be transferred. Larger buffers consume more memory. Default is 32768 bytes.
  • -P: port: specifies the port number for the connection.
  • -R: num_requests specifies the number of requests for a single connection. This increases transfer speed slightly, but increases memory usage.

Fourth, directory management

You can use the help command in the SFTP interpreter to view the help documents.

12 34 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34Copy the code
sftp> help Available commands: bye Quit sftp cd path Change remote directory to 'path' chgrp grp path Change group of file 'path' to 'grp' chmod mode path Change permissions of file 'path' to 'mode' chown own path Change owner of file 'path' to 'own' df [-hi] [path] Display statistics for current directory or filesystem containing 'path' exit Quit sftp get [-afPpRr] remote [local] Download file reget [-fPpRr] remote [local] Resume download file reput [-fPpRr] [local] remote Resume upload file help Display this help text lcd path Change local directory to 'path' lls [ls-options [path]] Display local directory listing  lmkdir path Create local directory ln [-s] oldpath newpath Link remote file (-s for symlink) lpwd Print local working directory ls [-1afhlnrSt] [path] Display remote directory listing lumask umask Set local umask to 'umask' mkdir path Create remote directory progress Toggle display of progress meter put [-afPpRr] local [remote] Upload file pwd Display remote working directory quit Quit sftp rename oldpath newpath Rename remote file rm path Delete remote file rmdir path Remove remote directory symlink oldpath newpath Symlink remote file version Show SFTP version ! command Execute 'command' in local shell ! Escape to local shell ? Synonym for helpCopy the code

The SFTP interpreter presets common commands, but they are not as rich as Bash itself.

1) Display current working directory:

1
2
Copy the code
sftp> pwd
Remote working directory: /Copy the code

2) Check the contents of current directory:

1
2
Copy the code
sftp> ls
Summary.txt     info.html       temp.txt        testDirectoryCopy the code

3) Use the -la parameter to view in a list form and display hidden files:

1, 2, 3, 4, 5, 6, 7, 8, 9Copy the code
sftp> ls -la drwxr-xr-x 5 demouser demouser 4096 Aug 13 15:11 . drwxr-xr-x 3 root root 4096 Aug 13 15:02 .. -rw------- 1 demouser demouser 5 Aug 13 15:04 .bash_history -rw-r--r-- 1 demouser demouser 220 Aug 13 15:02 .bash_logout  -rw-r--r-- 1 demouser demouser 3486 Aug 13 15:02 .bashrc drwx------ 2 demouser demouser 4096 Aug 13 15:04 .cache -rw-r--r-- 1 demouser demouser 675 Aug 13 15:02 .profile . . .Copy the code

4) Change directory:

1
Copy the code
sftp> cd testDirectoryCopy the code

5) Create folders:

1
Copy the code
sftp> mkdir anotherDirectoryCopy the code

The above commands are used to operate the remote server, what if you want to operate the local directory? Just add l before each command, for example to display files in the local operation directory:

1
2
Copy the code
sftp> lls
localFilesCopy the code

Use! You can run the commands in the Shell directly:

One, two, three, four, five, six, sevenCopy the code
sftp> ! df -h Filesystem Size Used Avail Capacity iused ifree %iused Mounted on /dev/disk1s1 466Gi 360Gi 101Gi 79% 3642919 9223372036851132888 0% / devfs 336Ki 336Ki 0Bi 100% 1162 0 100% /dev//dev/disk1S4 466Gi 4.0Gi 101Gi 4% 5 9223372036854775802 0% /private/var/vm map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net map auto_home 0Bi 0Bi 0Bi 100% 0 0 100%  /homeCopy the code

5. File transfer

5.1 Pulling files from the Remote Server

Use the get command to pull files from a remote server to the local:

1
Copy the code
sftp> get remoteFile [newName]Copy the code

If newName is not specified, the same file name as the remote server is used.

The -r argument can be used to pull the entire directory:

1
Copy the code
sftp> get -r remoteDirectoryCopy the code

5.2 Uploading files from the Local PC to the Server

You can use the put command to upload files from the local to the server:

1
Copy the code
sftp> put localFileCopy the code

Also, you can use the -r parameter to upload the entire directory, but note that if the directory does not exist on the server, you need to create it first:

1
2
Copy the code
sftp> mkdir folderName
sftp> put -r folderNameCopy the code

Best practices

1) Connect to the remote server

1
Copy the code
sftp remote_user@remote_hostCopy the code

2) Use ports to connect

1
Copy the code
sftp -P remote_port remote_user@remote_hostCopy the code

3) Pull files from the remote server

1
Copy the code
get /path/remote_fileCopy the code

4) Upload the local file to the server

1
Copy the code
put local_fileCopy the code

5) View the contents of the remote server directory



lsCopy the code

6) View the contents of the local directory



llsCopy the code

7) Run the local Shell command

! [command]Copy the code

The resources

  • SSH File Transfer Protocol

  • How To Use SFTP to Securely Transfer Files with a Remote Server


  1. Image drawn with Sketch, inspired by faith-oriented programming [return]