ls commands code example

Example 1: display all files in a directory linux

ls command list computer files in a directory in Unix OS with next structure:
ls [OPTION]... [FILE]...
Examples:
ls -l #display all files in current directory with  (-l) long format.
ls -a /directory #display all hidden files in given directory that start with .

Example 2: linux list

# syntax
# ls *<options> '*<file-dir>'

# example 
ls -ltr './*.log'

# + --------- + -------------------------------------------------------+
# | OPTION    |  DESCRIPTION                                           |
# + --------- + -------------------------------------------------------+
# | -a        | list all files including hidden file starting with '.' |
# | --color   | colored list [=always/never/auto]                      |
# | -d        | list directories - with ' */'                          |
# | -F        | add one char of */=>@| to enteries                     |
# | -i        | list file's inode index number                         |
# | -l        | list with long format - show permissions               |
# | -la       | list long format including hidden files                |
# | -lh       | list long format with readable file size               |
# | -ls       | list with long format with file size                   |
# | -r        | list in reverse order                                  |
# | -R        | list recursively directory tree                        |
# | -s        | list file size                                         |
# | -S        | sort by file size                                      |
# | -t        | sort by time & date                                    |
# | -X        | sort by extension name                                 |
# + --------- + -------------------------------------------------------+

Example 3: list all files in a directory and subdirectory linux

find . -type f -follow -print

Example 4: how to use the ls -l command in python

from subprocess import Popen, PIPE  def listdir_shell(path, *lsargs):     p = Popen(('ls', path) + lsargs, shell=False, stdout=PIPE, close_fds=True)     return [path.rstrip('\n') for path in p.stdout.readlines()]  dirlist = listdir_shell('/var/log', '-t')[:10]  from pprint import pprint pprint(dirlist)