Linux ls: Find Files, Read Listings, and Avoid Common Mix-Ups
When you SSH into a Linux server, ls is often the fastest way to get your bearings. But a directory listing can hide files, show a directory’s contents when you meant to inspect the directory itself, or make a size look more meaningful than it is. Here’s a practical way to use ls and understand what it’s telling you. Start with the view you need With no arguments, ls lists non-hidden entries in…
Using the "ls" command in Linux is an effective way to locate files and read directory listings. However, it's important to be aware of common mistakes that can occur when using this command. When you SSH into a Linux server and run "ls", it will quickly show you the non-hidden files in your current directory. If you want to inspect a different location, you can pass a path to the command, like "ls /var/log".
For a more detailed inspection, you can use a combination of options such as long format (-l), hidden entries (-a), and readable sizes (-h). These options can be combined in any order. For example, "ls -alh" will display metadata, hidden files, and sizes in units like K or M.
If you only want to see hidden files and not the special . and .. entries, you can use "ls -A" instead of "-a". Remember, "ls -A" includes hidden files but excludes . and ..
Here's a quick guide to the most commonly used options:
- Option -l: Displays permissions, owner, group, size, and modification time.
- Option -a: Includes hidden names and also shows . and ..
- Option -A: Includes hidden names but omits . and ..
- Option -h: Uses readable size units with -l or -s
- Option -1: Prints one entry per line
- Option -t: Sorts by modification time, newest first
- Option -S: Sorts by reported file size, largest first
- Option -R: Lists directory contents recursively
If you're working with directory paths, there's an important distinction to make. When you pass a directory to "ls", it will typically list the contents of the directory. To show the directory entry itself, you can use "-d": "ls -d project". This is particularly useful when working with shell patterns. For instance, "ls -d * /" will print directory names in the current location and won't search recursively.
When dealing with shell patterns, remember that spaces can cause issues. If your path includes spaces, you should quote it to ensure the shell interprets it as one argument. For example, "ls Project Files" instead of "ls Project Files".
The size displayed by "ls -l" is not the total size of the directory's contents. Instead, it reports metadata for the directory entry. If you want to know the total space used by everything inside the directory, you'll need to use the "du" command instead.
In summary, use "ls" to inspect names and metadata. Include "-a" or "-A" when hidden files are relevant, "-l" when you need more detail, and "-d" when you want to see the directory itself rather than its contents. For a directory's total usage, switch to the "du" command.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.