How to use the SSH ls command

Navigating and managing files on your Linux hosting requires understanding one of the most fundamental commands in the terminal arsenal. In particular, the SSH ls command is your primary tool for viewing and listing directory contents, making it essential for daily server administration tasks. Furthermore, mastering this command will significantly improve your ability to locate files, understand directory structures, and manage your hosting environment efficiently.

Understanding the SSH ls command

The ls (list) command is one of the most frequently used commands in Linux environments. Unlike graphical file managers, ls provides a text-based view of your directories and files through the terminal. Moreover, it offers numerous options that allow you to customize the output, sort files, view hidden content, and display detailed file information including permissions, ownership, and size.

Whether you’re managing a simple website or administering complex server configurations, the ls command will be your constant companion for file system navigation.

Basic syntax of the ls command

The fundamental syntax of the ls command follows this pattern:

ls [options] [directory]

Where:

  • options are flags that modify how the information is displayed
  • directory is the optional path you want to list (defaults to current directory)
  • Multiple options can be combined for more detailed output

Basic file listing

To list files and directories in your current location, simply type:

ls

This displays all visible files and directories in a simple format.

Example: Viewing the contents of a specific directory:

ls ~/public_html

As a result, this shows all files and folders inside your website’s root directory.

Viewing hidden files and detailed information

One of the most useful combinations is the -la option, which displays comprehensive file information including hidden files:

ls -la

The -l flag stands for “long format” and displays:

  • File permissions
  • Number of links
  • Owner name
  • Group name
  • File size
  • Modification date and time
  • File or directory name

The -a flag stands for “all” and reveals hidden files (those beginning with a dot, like .htaccess).

Practical example: Checking your WordPress directory including configuration files:

ls -la ~/public_html/

This command is invaluable for troubleshooting, as it shows critical files like .htaccess and .user.ini that are typically hidden. If you’re working with other file operations, you might also want to learn how to move and rename files using the SSH mv command.

Displaying file sizes in human-readable format

By default, file sizes are shown in bytes, which can be difficult to interpret for larger files. However, the -h (human-readable) option solves this problem:

ls -lh

Consequently, this displays file sizes in KB, MB, or GB, making it much easier to understand at a glance.

Example: Checking which files are consuming the most space:

ls -lhS ~/public_html/uploads/

The -S flag sorts by size (largest first), helping you quickly identify large files.

Sorting files by size

When managing server space, identifying large files becomes crucial. The -S option sorts output by file size in descending order:

ls -S

Combined example: View all files sorted by size with detailed information:

ls -lhS

This combination is particularly useful for identifying files that might be filling up your hosting space.

Displaying inode numbers

Every file and directory on a Linux system has a unique identifier called an inode number. To display these numbers, use the -i option:

ls -i

Example: Finding files with specific inodes:

ls -li ~/public_html/

Inode numbers are particularly useful when dealing with hard links or troubleshooting file system issues with your hosting provider.

Sorting by modification time

To see which files were modified most recently, use the -t option:

ls -lt

Similarly, you can reverse the order with -r to show oldest files first:

ls -ltr

Practical use case: Finding recently modified configuration files after an update:

ls -lth ~/public_html/ | head -10

This shows the 10 most recently modified files, helping you track changes to your website.

Advanced ls command options

Moreover, here are additional powerful options to enhance your file listing capabilities:

-R (recursive): Lists all files in subdirectories

ls -R ~/public_html/wp-content/

-d (directories): Lists directories themselves, not their contents

ls -ld */

-1 (one per line): Displays each file on a separate line

ls -1

–color: Colorizes output to distinguish file types

ls --color=auto

-X: Sorts alphabetically by file extension

ls -lX

Common use cases for web hosting

In practice, here are typical scenarios where the ls command is essential for web hosting management:

1. Checking WordPress file permissions:

ls -l ~/public_html/wp-config.php

2. Finding large log files:

ls -lhS ~/logs/

3. Viewing all uploaded files with details:

ls -lah ~/public_html/wp-content/uploads/

4. Checking for hidden malware or suspicious files:

ls -la ~/public_html/ | grep "^\."

5. Listing only directories in your web root:

ls -ld ~/public_html/*/

6. Finding recently uploaded files:

ls -lt ~/public_html/uploads/ | head -20

Once you’ve located the files you need, you can easily create new directories to organize them or manage your file structure more effectively.

Combining ls with other commands

The ls command becomes even more powerful when combined with other Linux utilities using pipes:

Count files in a directory:

ls | wc -l

Search for specific file types:

ls -la | grep ".php"

View output page by page:

ls -la | less

Save listing to a file:

ls -laR ~/public_html/ > file-inventory.txt

Best practices and tips

Therefore, to maximize your efficiency when using the ls command, follow these best practices:

  • Create aliases for frequently used combinations like alias ll='ls -lah' in your shell configuration
  • Always check hidden files when troubleshooting website issues by including the -a flag
  • Use human-readable format (-h) when checking disk space consumption
  • Combine with grep to filter results and find specific files quickly
  • Sort by time (-t) when investigating recent changes or security incidents
  • Leverage color output to quickly distinguish between files, directories, and links
  • Pipe to less when dealing with directories containing many files
  • Document your findings by redirecting output to text files for future reference

Pro tip: Create a custom alias that combines your most-used options:

alias lsa='ls -laht --color=auto'

Add this to your ~/.bashrc file to make it permanent.

New hosting plan with 80% OFF

Ready to put your SSH skills to work? Upgrade to a hosting plan that gives you full SSH access and complete control over your server files. Additionally, enjoy a huge 80% discount with reliable performance, free SSH access, and full Linux terminal support. Furthermore, you’ll be able to run commands, navigate directories, and manage your hosting environment with professional-grade tools. Perfect for developers, power users, or anyone who prefers the control of a Linux environment.

Explore our powerful hosting solutions and take your server management skills to the next level.

Hosting from JetHost

Need more help?

Mastering the SSH ls command is a fundamental skill that every Linux server administrator needs. Whether you’re listing basic directory contents, analyzing file permissions, identifying large files, or troubleshooting website issues, the ls command provides the visibility you need to manage your hosting environment effectively. By following the examples and best practices outlined in this tutorial, you’ll be able to navigate your server’s file system with confidence and efficiency.

Remember that the ls command is just the beginning of file system navigation. Nevertheless, with practice and experimentation with different option combinations, you’ll discover workflows that perfectly match your specific server management needs, making you a more proficient Linux administrator.