Linux Command Line 1 - Getting to Know Linux
This article mainly references the book The Linux Command Line (2nd Edition). The shell tool used is Xshell, and the operating system is CentOS 7.6. This means that some shortcuts may not work in other shell tools, and there may be slight differences in files across different Linux operating systems. Please analyze based on your actual situation.
Since CentOS 7.6 is EOL, consider using Rocky Linux or Alma Linux instead.
What is Shell
Simple Commands
date: Displays the current system time and date
cal: Displays the calendar for the current month
1 | [root@lht ~]# date |
df: Disk free space
free: Free memory
1 | [root@lht ~]# df |
clear: Clears the screen, shortcut Ctrl-L
End Session
exit: Ends the session, shortcut Ctrl-D
1 | [root@lht ~]# exit |
Ctrl-Shift-R: Reconnect
Command History
Use the up and down arrow keys to view previously entered commands
Some Shell Considerations
Do not use Ctrl-C and Ctrl-V to copy or paste content in the shell. These keys have different meanings in Windows, but in the shell, they mean something entirely different. These meanings were established long before the release of Windows.
For example, Ctrl-C typically means to terminate the currently running program or to invalidate the characters already entered into the command line.
You can use the shortcut Ctrl-insert to copy the selected content, and use the middle mouse button to paste the clipboard content at the cursor. This works at least in Xshell. You can also use the right-click menu to copy and paste.
File System
View the Current Working Directory
We use the pwd (print working directory) command to display the current working directory, which is the absolute path of the directory you are currently in.
When we first log in to the system (or start a terminal emulator session), the current working directory is our home directory. Every user has their own home directory, and when a user operates the system as a normal user, the home directory is the only place where the user is allowed to write files.
pwd: View the current working directory
1 | [root@lht ~]# pwd |
Basic Symbol Interpretation
1 | [root@lht ~]# |
Listing Directory Contents
ls: List the files and subdirectories contained in the directory
If the current directory is empty, nothing will be displayed.
1 | [root@lht ~]# ls |
Changing Directories
cd: Change directories
The cd command is usually followed by the path of the directory we want to go to, which can be an absolute or relative path.
Absolute Path
An absolute path starts from the root path and begins with ‘/’.
1 | [root@lht ~]# cd /usr/ |
Relative Path
A relative path starts from the current working directory. ‘.’ (dot) represents the working directory, and ‘..’ (dot dot) represents the parent directory.
1 | [root@lht usr]# cd .. |
If a file path is not specified, it is assumed to be in the current working directory. This means we can omit “./”.
Some Quick Directory Changes
cd: Change to the home directory (current user)
cd -: Change to the previous working directory
cd ~user_name: Change to the specified user’s home directory
1 | [root@lht /]# cd |
About Filenames
Hidden Files
Files that begin with the ‘.’ character are hidden files. The ls command won’t display them unless the -a option is used.
Some applications store their configuration files as hidden files in your home directory.
ls -a: List all contents, including hidden files
1 | [root@lht ~]# ls -a |
Case Sensitivity
Filenames and command names are case-sensitive. The filenames “File1” and “file1” refer to two different files.
File Extensions
Linux does not have the concept of “file extensions” like other systems. You can name a file anything you like. The content or purpose of a file is determined by other methods. Although Unix-like operating systems do not rely on file extensions to determine the content or use of a file, some applications do.
Filename Conventions
Although Linux supports long filenames, and filenames can contain spaces and punctuation marks, punctuation is limited to “.”, “-“, and underscores. Most importantly, do not use spaces in filenames. If you want to indicate a space between words, use an underscore instead.
Exploring the Operating System
The ls Command
The ls command is perhaps the most commonly used command. It can list the contents of the current directory, specify other directories, or even list multiple specified directories.
1 | [root@lht usr]# ls |
Using the -l Option
To get more detailed output, you can use the -l option or simply ll.
1 | [root@lht usr]# ls -l |
Options and Arguments
Most commands follow a format:
1 | command -options arguments |
Many commands also support long options, similar to --argument, where the argument starts with two hyphens.
For short options, many commands allow combining multiple options, e.g., ls -lt. The -t option sorts files by modification time, and adding the long option --reverse will output the results in reverse order.
1 | [root@lht usr]# ls -lt |
Linux Command Parameters Query Project
The ls command has a vast number of options, and indeed, many commands have numerous options. For detailed explanations of various parameters and usage examples, you can refer to this GitHub project:
And this related Linux command query website:
Detailed Output Parameter Interpretation
Here’s an example of an output line:
1 | dr-xr-xr-x. 2 root root 32768 May 4 19:12 bin |
| Output Segment | Meaning |
|---|---|
| dr-xr-xr-x | File access permissions. The first character indicates the file type. In different types, ‘-’ indicates a regular file, while ‘d’ indicates a directory. The next three characters represent the owner’s permissions, followed by three for the group members, and three for others. This field’s complete meaning will be discussed later. |
| 2 | The number of hard links to the file, which will be referenced later when discussing links. |
| root | The username of the file owner. |
| root | The name of the group to which the file belongs. |
| 32768 | The size of the file in bytes. |
| May 4 19:12 | The date and time the file was last modified. |
| bin | The name of the file. |
Determine File Type
file: Determine the file type
1 | file filename |
Note, in Linux systems, it is not required for the file name to reflect the file content, meaning that a file named “picture.jpg” may not actually contain a JPEG compressed image.
1 | [root@lht ~]# file picture.jpg |
Of course, you can still associate file extensions with file types, but in Linux, the file extension does not necessarily represent the file type.
Browsing File Contents
Many system settings are stored in text format files called configuration files. Reading them can provide a deeper understanding of how the system works. Additionally, many actual programs used by the system (called scripts) are also stored in this format. In later sections, we will learn how to edit text files to modify system settings and write our own script files, but for now, we will simply view their contents.
less: Viewing Text Files
You can use the less command to browse text files:
1 | less filename |
For example:
1 | [root@lht ~]# less hello.txt |
Output:
1 | hello world |
If the file content is longer than one page, you can scroll up and down the file, and press the “q” key to exit the viewer.
For more information on keyboard commands in the less program and options for the less command itself, you can refer to:
Common Commands in less
| Command | Action |
|---|---|
| Page Up or b | Scroll up one page |
| Page Down or Space | Scroll down one page |
| UP Arrow | Scroll up one line |
| Down Arrow | Scroll down one line |
| G | Move to the last line |
| 1G or g | Move to the first line |
| /characters | Search forward for a specified string |
| n | Search for the next occurrence of the specified string |
| h | Display help screen |
| q | Exit the less program |
Less is More
The less program is an improved version of the earlier Unix program more. The name “less” is a play on the saying “less is more,” a motto of modernist architects and designers.
less belongs to the category of “paging” programs, which allow you to easily browse long text documents page by page. The more program can only scroll forward, while less allows both forward and backward scrolling, among many other features.
Linux System Directory Structure
In Linux, everything is a file. Here are some system directories along with their descriptions:
| Directory | Description |
|---|---|
| / | Root directory, the origin of everything. |
| /bin | Contains essential binary programs required for system startup and operation. |
| /boot | Contains the Linux kernel, initial RAM disk image (for drivers needed at startup), and boot loaders. |
| /home | In a typical configuration, each user is assigned a directory under /home. Regular users can only write files in their directories, protecting the system from erroneous user activities. |
| /lib | Contains shared library files used by core system programs, similar to dynamic link libraries in Windows. |
| /lost+found | Each formatted partition or device in Linux has this directory for recovering damaged files during filesystem recovery. It should be empty unless the filesystem is genuinely damaged. |
| /media | Contains mount points for removable media, such as USB drives and CD-ROMs, which are automatically mounted when connected. |
| /mnt | Historically used for mounting removable media. |
| /opt | Used for installing “optional” software, mainly to store commercial software products that might be installed on the system. |
| /proc | A special directory that is not a real filesystem in terms of files stored on disk. It is a virtual filesystem maintained by the Linux kernel, providing insight into how the kernel manages the computer. |
| /root | Home directory for the root account. |
| /sbin | Contains “system” binary files, which are programs for performing major system tasks, typically reserved for the superuser. |
| /tmp | A directory for storing temporary files created by various programs. Some configurations clear this directory on each reboot. |
| /usr | Likely the largest directory in a Linux system, containing all programs and files needed by ordinary users. |
| /usr/bin | Contains executable programs installed on the system. Generally, this directory contains many programs. |
| /usr/lib | Contains shared libraries used by programs in /usr/bin. |
| /usr/local | Directory for installing programs not included with the system distribution, often for programs compiled from source code. This directory is typically empty in newly installed Linux systems. |
| /usr/sbin | Contains many system administration programs. |
| /usr/share | Contains shared data used by programs in /usr/bin, including default configuration files, icons, desktop backgrounds, audio files, etc. |
| /usr/share/doc | Most software packages installed on the system contain documentation, found categorized under /usr/share/doc. |
| /var | Stores dynamic files, such as various databases, spooled files, user mail, etc., differing from the static nature of most other directories. |
| /var/log | Contains log files documenting various system activities. These files are crucial and should be monitored regularly. One of the most important files is /var/log/messages. Note that some systems may require superuser access to view these log files for security reasons. |
For more related content, refer to:
Symbolic Links
1 | lrwxrwxrwx. 1 root root 8 Jul 11 2019 sbin -> usr/sbin |
A file that starts with “l” is called a symbolic link (also known as a soft link). In this example, the symbolic link sbin points to usr/sbin, meaning accessing the symbolic link sbin actually accesses the sbin directory under the usr directory.
Hard Links
There is another type of link called a hard link. Hard links also allow files to have multiple names, but they create multiple filenames in a different way. We will discuss the differences between symbolic links and hard links in more detail later.
- Title: Linux Command Line 1 - Getting to Know Linux
- Author: LHT
- Created at : 2023-01-01 08:00:00
- Link: https://blog.327774.xyz/2023/01/01/linux/Linux command line 1 - Getting to know Linux/
- License: This work is licensed under CC BY-NC-SA 4.0.