Open menu

Learn

SFTP dir command

One of the key commands in the SFTP command set is the dir command. The dir command is primarily used to list the contents of a directory on a remote server. This article aims to provide an in-depth understanding of the SFTP dir command and its various uses.

The Basics

The basic syntax of the dir command in an SFTP environment is simply dir . When this command is issued, it provides a list of files and directories in the current working directory on the remote server. The dir command, much like its counterpart in a regular shell environment, provides a simple, straightforward way to view the contents of a directory. The dir command can also be used with a specific directory name. For instance, dir /home/user/documents would list the contents of the 'documents' directory in the 'user' home directory.

Understanding the Output

The output of the dir command in an SFTP environment provides a simple list of files and directories in the current directory on the remote server. For more detailed information on files and directories, you can use the dir -l command within the SFTP environment. dir -l provides a detailed list of files and directories, including permissions, ownership, size, and modification time. For example, dir -l /documents would list the contents of the documents directory, including detailed information about each file and directory. Here is an example of the output:
sftp> dir -l
-rw-r--r--    1 0        0         1486129 Mar 26 19:32 my_file.pdf
-rw-r--r--    1 0        0         1486129 Mar 26 19:46 another_file.pdf
-rw-r--r-- : this part of the line is the file permissions. The first character - signifies that this is a file. A d would mean it is a directory. The next three characters rw- show that the owner of the file has read and write permissions, but not execute permissions. The next three characters r-- mean that the group has only read permission. The last three characters r-- mean that all other users also have only read permission 1 : This number signifies the number of hard links to the file. In this case, there's only one link 0 : This is the user ID of the owner of the file. In this case, the owner's user ID is 0, which typically belongs to the root user in a Unix-like system 0 : This is the group ID of the file. The group ID is also 0, which typically signifies the root group in a Unix-like system 1486129 : This is the size of the file in bytes, which in this case is the same for both files Mar 26 19:32 and Mar 26 19:46 : These are the dates and times that the files were last modified my_file.pdf and another_file.pdf : These are the names of the files.