2.3 Using Directories and Listing Files
Navigation of home and system directories and listing files in various locations.
- In Linux, everything is stored in files.
- Files are used to store data such as text, graphics, and programs.
- Directories are a type of file used to store other files.
- Directories are used to provide a hierarchical organization structure.
- On a Windows system, the top level of the directory structure is called My Computer.
- The Linux directory structure, called a filesystem, also has a top level called the root directory (symbolized by the slash / character).
- To view the contents of the root directory, use the ls command with the / character as the argument:
- Notice that there are many directories with descriptive names including /boot, which contains files to boot the computer.
- On most Linux distributions there is a directory called home under the root / directory.
- Under this /home directory there is a directory for each user on the system.
- When a user opens a shell, they should automatically be placed in their home directory.
- The user has the full control to create and delete additional files and directories in their home directory.
- Most other directories in a Linux filesystem are protected with file permissions.
- The home directory has a special symbol used to represent it, the tilde ~ character.
- The directory name is the same as the name of the user.
- So, a user named sysadmin would have a home directory called /home/sysadmin:
- The pwd (print working directory) command can be used to determine where the user is currently located within the filesystem.
- The pwd command prints the working directory, which is the current location of the user within the filesystem. pwd [OPTIONS]
- When a user opens a shell, they typically begin in their home directory.
- To navigate the filesystem, use the cd (change directory) command.
- To move from the home directory into the Documents directory use the directory name as an argument to the cd command:
- After changing directories, the new location can also be confirmed in the new prompt, again shown in blue.
- When used with no arguments, the cd command will take the user to their home directory.
- If the user tries to change to a directory that does not exist, the command returns an error message:
- A path is a list of directories separated by the / character.
- There are two types of paths: absolute and relative.
- For example, /home/sysadmin is a path to the home directory:
- Absolute paths allow the user to specify the exact location of a directory.
- Absolute paths always starts at the root directory, and therefore it always begins with the / character.
- The path /home/sysadmin is an absolute path; it tells the system to:
- Begin at the root / directory > move into the home directory > then into the sysadmin directory
- If the path /home/sysadmin is used as an argument to the cd command, it moves the user into the home directory for the sysadmin user.
- A relative path gives directions to a file relative to the current location in the filesystem
- The user must currently be in a directory that contains objects in the path
Paths – Shortcuts: The .. Characters
- Two period .. characters always represents one directory higher relative to the current directory, sometimes referred to as the parent directory.
- For example, to move from the Art directory back to the School directory:
- The double dot can also be used in longer paths as well:
- The single period . character always represents the current directory.
- For the cd this shortcut is not very useful, but it comes in handy for commands covered in subsequent sections.
Listing Files in a Directory
- The ls (list) command is one of the most powerful tools for navigating the filesystem.
- The ls command is used to display the contents of a directory and can provide detailed information about the files.
- When used with no options or arguments, the ls command lists the files in the current directory:
- The ls command can also be used to list the contents of any directory in the filesystem, provide the path to the directory as an argument.
Listing Hidden Files
- A hidden file is any file (or directory) that begins with a dot . character.
- The ls command omits hidden files by default.
- To display all files, including hidden files, use the -a option to the ls command:
- Most of the hidden files are customization files, designed to customize how Linux, your shell or programs work. ○For example, the .bashrc file in the home directory customizes features of the shell
Long Display Listing
- Each file has details associated with it called metadata, this can include information such as the size, ownership, or timestamps.
- Use the -l option to the ls command to view this information.
- For example, below, a listing of the /var/log directory:
- In the output above, each line describes metadata about a single file.
- The following describes each of the fields of data in the output of the ls -l command:
- File Type:
- The next nine characters demonstrate the permissions of the file.
- Permissions indicate how certain users can access a file.
- Hard Link Count:
- This number indicates how many hard links point to this file.
- User Owner:
- Every file is owned by a user account.
- This is important because the owner has the rights to set permissions on a file.
- Group Owner:
- Indicates which group owns this file, this is important because any member of this group has a set of permissions on the file.
- File Size:
- Size of files in bytes.
- Indicates when a file’s contents were last modified.
- File Name:
- The name of the file or directory.
Human Readable Sizes
- The -l option to the ls command displays file sizes in bytes.
- For text files, a byte is 1 character so small files are easy to read, but for larger files, it is hard to comprehend how large the file is:
- The file size is hard to determine in bytes. Is 1561400 a large file or small?
- To present the file size in a more human readable size, like megabytes or gigabytes, add the -h option (with the -l option) to the ls command:
- When the command ls -d is used, it refers to the current directory, and not the contents within it.
- The . represents the current directory.
- The ls -l command lists the contents of the directory:
- To use the ls -d command in a meaningful way requires the addition of the -l option:
- Recursive listing is when you want to display all of the files in a directory as well as all of the files in all subdirectories under a directory.
- To perform a recursive listing, use the -R option to the ls command:
- Note that in the example above, the files in the /etc/ppp directory were listed first and the files in the /etc/ppp/peers directory were listed after.
Sort a Listing
- By default, the -ls command sorts files alphabetically by file name.
- Sometimes it may be useful to sort files using different criteria.
- To sort files by size, we can use the -S option (capital letter s).
- While the -S option works by itself, it is most useful when used with the -l option so the file sizes are visible:
- It may also be useful to use the -h option to display human-readable file sizes:
- The -t option sorts files based on the time they were modified:
- For more detailed modification time information you can use the –full-time option to display the complete timestamp (including hours, minutes, seconds):
- It is possible to perform a reverse sort by using the -r option. It can be used alone, or combined with either the -S or -t options:
Berikut file materi yang bisa di download :