github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/lsd/lsd.go (about)

     1  // Package lsd provides the lsd command.
     2  package lsd
     3  
     4  import (
     5  	"context"
     6  	"os"
     7  
     8  	"github.com/rclone/rclone/cmd"
     9  	"github.com/rclone/rclone/cmd/ls/lshelp"
    10  	"github.com/rclone/rclone/fs"
    11  	"github.com/rclone/rclone/fs/config/flags"
    12  	"github.com/rclone/rclone/fs/operations"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	recurse bool
    18  )
    19  
    20  func init() {
    21  	cmd.Root.AddCommand(commandDefinition)
    22  	cmdFlags := commandDefinition.Flags()
    23  	flags.BoolVarP(cmdFlags, &recurse, "recursive", "R", false, "Recurse into the listing", "")
    24  }
    25  
    26  var commandDefinition = &cobra.Command{
    27  	Use:   "lsd remote:path",
    28  	Short: `List all directories/containers/buckets in the path.`,
    29  	Long: `
    30  Lists the directories in the source path to standard output. Does not
    31  recurse by default.  Use the ` + "`-R`" + ` flag to recurse.
    32  
    33  This command lists the total size of the directory (if known, -1 if
    34  not), the modification time (if known, the current time if not), the
    35  number of objects in the directory (if known, -1 if not) and the name
    36  of the directory, Eg
    37  
    38      $ rclone lsd swift:
    39            494000 2018-04-26 08:43:20     10000 10000files
    40                65 2018-04-26 08:43:20         1 1File
    41  
    42  Or
    43  
    44      $ rclone lsd drive:test
    45                -1 2016-10-17 17:41:53        -1 1000files
    46                -1 2017-01-03 14:40:54        -1 2500files
    47                -1 2017-07-08 14:39:28        -1 4000files
    48  
    49  If you just want the directory names use ` + "`rclone lsf --dirs-only`" + `.
    50  
    51  ` + lshelp.Help,
    52  	Annotations: map[string]string{
    53  		"groups": "Filter,Listing",
    54  	},
    55  	Run: func(command *cobra.Command, args []string) {
    56  		ci := fs.GetConfig(context.Background())
    57  		cmd.CheckArgs(1, 1, command, args)
    58  		if recurse {
    59  			ci.MaxDepth = 0
    60  		}
    61  		fsrc := cmd.NewFsSrc(args)
    62  		cmd.Run(false, false, command, func() error {
    63  			return operations.ListDir(context.Background(), fsrc, os.Stdout)
    64  		})
    65  	},
    66  }