2.3 Using Directories and Listing Files

2.3 Using Directories and Listing Files

Weight: 2

Description: Navigation of home and system directories and listing files in various locations.

Key Knowledge Areas:

  • Files, directories

  • Hidden files and directories

  • Home directories

  • Absolute and relative paths

The following is a partial list of the used files, terms and utilities:

  • Common options for ls

  • Recursive listings

  • cd

  • . and ..

  • home and ~

Linux Files and Directories

So what’s a file, In Linux, a “file” is a fundamental unit of storage that contains data or information. There’s a popular quote “On a UNIX system, everything is a file; if something is not a file, it is a process.” And just for clarification a “directory” is also a file in Linux, as it’s just another file containing name of other files. So now we know since everything is a file let’s see what all sorts of files are there:

  • Directories

  • Special files i.e. mechanism used for input and output like `/dev`

  • Links

  • Sockets: Yes, sockets are also a type of file

  • Named pipes: Acts as communication channel between processes without using network socket semantics.

The best way to view these types of files and their types would be using the command which every LINUX user learns first i.e. `ls` follow this command with a `-l` and voila you’re able to view the all details of a file.

If we look at the above command output represents a regular file while dis representation of directory. You might be wondering why other are there those are part of file permissions and we don’t need to look into that for now. Only the first character from `ls` output depicts what sort of file is that. Below is the list of different characters representing corresponding file types:

  • -: Regular file

  • d: Directory

  • l: Symbolic link

  • c: Character device file

  • b: Block device file

  • p: Named pipe (FIFO)

  • s: Socket

Now since we have understood file types, we’ll take a look at ls command options.

Listing Files and Directories in Linux

The ls command is used to list all files and directories in the Linux terminal.

  • Displays the contents of the current working directory or a specified path.

  • Lists items in alphabetical order by default.

  • The command can be customized with flags to display file permissions, ownership, size, and modification date.

  • Helps users view file and directory names with optional details.

as we have seen the basic usage of ls command is to see what's in the current folder. But ls command has other options:

ls Options Overview

The ls command has a variety of options to customize its output:

  • -l - Long listing format

  • -a - Include hidden files

  • -h - Human-readable sizes

  • -t - Sort by modification time

  • -r - Reverse order while sorting

  • -R - List subdirectories recursively

  • -S - Sort by file size

  • -1 - List one file per line

  • -d - List directories themselves, not their contents

  • -F - Append indicator (one of */=@|) to entries

Long Listing Format

The -l option gives you detailed information about files and folders.

It displays information such as:

  • file permissions

  • number of links

  • owner name

  • owner group

  • file size

  • time of last modification

  • file or directory name

This format is useful for getting a comprehensive overview of the file attributes.

Listing hidden files

The -a option includes hidden files in the listing.

This option is helpful when you need to view or manage configuration files that are not visible by default.

Human-Readable Sizes

The -h option makes file sizes easier to read by converting byte counts into kilobytes (K), megabytes (M), gigabytes (G), etc.

This option is particularly useful when you want to quickly assess the size of files and directories without manually converting bytes.

Sorting by Time

The -t option sorts files and directories by modification time, with the most recently modified files first.

This option is useful when you want to see the most recently updated files first.

Reverse Order

The -r option reverses the order of the sort.

When used in combination with other options like -t, it can display the oldest files first.

This option is useful for reversing the default sorting behavior to meet specific needs.

Recursive Listing

The -R option lists directories and their contents recursively.

This is useful for viewing the entire directory tree.

Sort by Size

The -S option sorts files by size, with the largest files first.

This is helpful for quickly identifying large files in a directory.

One File per Line

The -1 option lists one file per line, which is useful for scripts or when piping output to other commands.

Directories Only

The -d option lists directories themselves rather than their contents.

This is useful for seeing directory names without contents.

Append Indicator

The -F option appends an indicator character to entries (e.g., / for directories, * for executables).

Using Multiple Options

Changing the Directory in Linux

The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

The current working directory is the directory (folder) in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory.

cd Command

cd is a shell builtin, and its behavior may slightly differ from shell to shell. It uses the shell environment variables to determine necessary information for its execution.

The syntax for the cd command is as follows:

In its simplest form, when used without any argument, cd will take you to your home directory.

To switch to a directory, you must have executable permissions for that directory.

Absolute and Relative Path Names

When specifying a directory to change to, you can use either absolute or relative path names. The absolute or full path starts from the system root /, and the relative path starts from your current directory.

By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that the Downloads directory exists in your home directory, you can navigate to it by using the relative path to the directory:

You can also navigate to the same directory by using its absolute path:

In short, if the path starts with a slash (/), it is the absolute path to the directory.

The Parent Directory

On Unix-like operating systems, the current working directory is represented by a single dot (.). Two dots (..), one after the other, represent the parent directory or the directory immediately above the current one.

If you type cd ., you will change into the current directory or, in other words, the command will do nothing.

Suppose you are currently in the /usr/local/share directory. To switch to the /usr/local directory (one level up from the current directory), you would type:

To move two levels up to the /usr directory (the parent’s parent), you could run the following:

Here is another example. Let’s say you are in the /usr/local/share directory, and you want to switch to the /usr/local/src. You can do that by typing:

To change back to the previous working directory, pass the dash (-) character as an argument to the cd command:

For example, if you want to navigate to the Downloads directory, which is inside your home directory, you would type:

You can also navigate to another user’s home directory using the following syntax:

Directories with Space in Their Names #

If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash (\) character to escape the space:

By now, you should have a good understanding of what is the current working directory and how to use the cd command to navigate through your system’s directory structure

.

.

.


sources:

https://prasha7.medium.com/journey-of-a-linux-file-part-1-what-is-a-file-202d9edea3fe https://www.geeksforgeeks.org/linux-unix/ls-command-in-linux/ https://www.w3schools.com/bash/bash_ls.php https://linuxize.com/post/linux-cd-command/

Last updated