Linux Command Line 1 - Getting to Know Linux

LHT

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
2
3
4
5
6
7
8
9
10
[root@lht ~]# date
Tue Jun 13 15:52:59 CST 2023
[root@lht ~]# cal
June 2023
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

df: Disk free space

free: Free memory

1
2
3
4
5
6
7
8
9
10
11
12
[root@lht ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 41147472 15358444 23885536 40% /
devtmpfs 930536 0 930536 0% /dev
tmpfs 940988 0 940988 0% /dev/shm
tmpfs 940988 472 940516 1% /run
tmpfs 940988 0 940988 0% /sys/fs/cgroup
tmpfs 188200 0 188200 0% /run/user/0
[root@lht ~]# free
total used free shared buff/cache available
Mem: 1881980 442268 471820 472 967892 1265708
Swap: 0 0 0

clear: Clears the screen, shortcut Ctrl-L

End Session

exit: Ends the session, shortcut Ctrl-D

1
2
3
4
[root@lht ~]# exit
logout

Connection closed.

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
2
[root@lht ~]# pwd
/root

Basic Symbol Interpretation

1
2
3
[root@lht ~]#
# Current username @ hostname current directory name (~ is the home directory of the root user)
# '#' means the current user is root, and for a regular user it is '$'

Listing Directory Contents

ls: List the files and subdirectories contained in the directory

If the current directory is empty, nothing will be displayed.

1
2
[root@lht ~]# ls
[root@lht ~]#

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
2
[root@lht ~]# cd /usr/
[root@lht 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
2
[root@lht usr]# cd ..
[root@lht /]#

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
2
3
4
5
[root@lht /]# cd
[root@lht ~]# cd -
/
[root@lht /]# cd ~root
[root@lht ~]#

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
2
3
4
[root@lht ~]# ls -a
. .bash_logout .cache .docker-conf .local .pki .ssh
.. .bash_profile .config .halo .mysql_history .pydistutils.cfg .tcshrc
.bash_history .bashrc .cshrc .lesshst .pip .rediscli_history .viminfo

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
2
3
4
5
6
[root@lht usr]# ls
bin etc games include java lib lib64 libexec local sbin share src tmp
[root@lht usr]# ls /usr/local/
aegis cloudmonitor etc include lib libexec sbin src
bin codesever games java lib64 nginx share
[root@lht usr]# ls /usr/ /usr/local/

Using the -l Option

To get more detailed output, you can use the -l option or simply ll.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@lht usr]# ls -l
total 124
dr-xr-xr-x. 2 root root 32768 May 4 19:12 bin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 43 root root 4096 Feb 22 07:39 include
drwxr-xr-x 3 root root 4096 Dec 25 2021 java
dr-xr-xr-x. 42 root root 4096 Jan 8 06:56 lib
dr-xr-xr-x. 41 root root 32768 May 4 19:12 lib64
drwxr-xr-x. 23 root root 4096 May 4 19:12 libexec
drwxr-xr-x. 17 root root 4096 Feb 22 10:53 local
dr-xr-xr-x. 2 root root 16384 May 4 19:05 sbin
drwxr-xr-x. 100 root root 4096 May 4 19:12 share
drwxr-xr-x. 4 root root 4096 Jul 11 2019 src
lrwxrwxrwx. 1 root root 10 Jul 11 2019 tmp -> ../var/tmp
[root@lht usr]# ll
total 124
dr-xr-xr-x. 2 root root 32768 May 4 19:12 bin
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 43 root root 4096 Feb 22 07:39 include
drwxr-xr-x 3 root root 4096 Dec 25 2021 java
dr-xr-xr-x. 42 root root 4096 Jan 8 06:56 lib
dr-xr-xr-x. 41 root root 32768 May 4 19:12 lib64
drwxr-xr-x. 23 root root 4096 May 4 19:12 libexec
drwxr-xr-x. 17 root root 4096 Feb 22 10:53 local
dr-xr-xr-x. 2 root root 16384 May 4 19:05 sbin
drwxr-xr-x. 100 root root 4096 May 4 19:12 share
drwxr-xr-x. 4 root root 4096 Jul 11 2019 src
lrwxrwxrwx. 1 root root 10 Jul 11 2019 tmp -> ../var/tmp

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@lht usr]# ls -lt
total 124
dr-xr-xr-x. 2 root root 32768 May 4 19:12 bin
dr-xr-xr-x. 41 root root 32768 May 4 19:12 lib64
drwxr-xr-x. 100 root root 4096 May 4 19:12 share
drwxr-xr-x. 23 root root 4096 May 4 19:12 libexec
dr-xr-xr-x. 2 root root 16384 May 4 19:05 sbin
drwxr-xr-x. 17 root root 4096 Feb 22 10:53 local
drwxr-xr-x. 43 root root 4096 Feb 22 07:39 include
dr-xr-xr-x. 42 root root 4096 Jan 8 06:56 lib
drwxr-xr-x 3 root root 4096 Dec 25 2021 java
drwxr-xr-x. 4 root root 4096 Jul 11 2019 src
lrwxrwxrwx. 1 root root 10 Jul 11 2019 tmp -> ../var/tmp
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
[root@lht usr]# ls -lt --reverse
total 124
drwxr-xr-x. 2 root root 4096 Apr 11 2018 games
drwxr-xr-x. 2 root root 4096 Apr 11 2018 etc
lrwxrwxrwx. 1 root root 10 Jul 11 2019 tmp -> ../var/tmp
drwxr-xr-x. 4 root root 4096 Jul 11 2019 src
drwxr-xr-x 3 root root 4096 Dec 25 2021 java
dr-xr-xr-x. 42 root root 4096 Jan 8 06:56 lib
drwxr-xr-x. 43 root root 4096 Feb 22 07:39 include
drwxr-xr-x. 17 root root 4096 Feb 22 10:53 local
dr-xr-xr-x. 2 root root 16384 May 4 19:05 sbin
drwxr-xr-x. 23 root root 4096 May 4 19:12 libexec
drwxr-xr-x. 100 root root 4096 May 4 19:12 share
dr-xr-xr-x. 41 root root 32768 May 4 19:12 lib64
dr-xr-xr-x. 2 root root 32768 May 4 19:12 bin

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:

Linux Command GitHub

And this related Linux command query website:

Linux Command Search Engine

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
2
[root@lht ~]# file picture.jpg 
picture.jpg: ASCII text

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:

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.

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.