Basics of Linux Command Line
Synopsis
Understanding the basics of the Linux command line is essential for anyone working with Linux-based systems. This guide will introduce you to fundamental concepts and commands, helping you navigate and manipulate the Linux environment efficiently.
What is Linux Command Line
The Linux command line, also known as the terminal or shell, is a text-based interface used to interact with the operating system. Unlike graphical user interfaces (GUIs), the command line allows for more direct and powerful control over the system. It is widely used by system administrators, developers, and power users for tasks such as file management, system monitoring, and software installation.
Instructions:
1: Opening the Terminal
1.1 On most Linux distributions, you can open the terminal by searching for “Terminal” in the applications menu or using a keyboard shortcut like Ctrl + Alt + T.
2: Basic Commands
2.1 “pwd”: (Print Working Directory): Displays the current directory.
pwd
2.2 “ls”: (List): Lists files and directories in the current directory.
ls
2.3 “cd”: (Change Directory): Navigates between directories.
cd /path/to/directory
2.4 “mkdir”: (Make Directory): Creates a new directory.
mkdir new_directory
2.5 “touch”: Creates a new empty file.
touch newfile.txt
2.6 “cp” (Copy): Copies files or directories.
cp source_file destination_file
2.7 “mv” (Move): Moves or renames files or directories.
mv oldname.txt newname.txt
2.8 “rm” (Remove): Deletes files or directories.
rm file.txt
2.9 “man” (Manual): Displays the manual page for a command.
man ls
3: File Permissions
3.1 “chmod”: Changes file permissions
chmod 754 script.sh -rwxr-xr--
The permissions applied to script.sh for the command above are 754.
That means the Owner has read, write, and execute (rwx).
The Group has read and execute (r-x).
The Other has read (r–).
Note: File permissions can be modified by using numbers or letter, but using the former is encouraged. Here’s what the structure looks like.
Permission Action chmod option
Read View r or 4
Write Edit w or 2
Execute Execute x or 1
User chmod w/Numbers Output
Owner 700 -rwx------
Group 070 ----rwx---
Other 007 -------rwx
Let’s break this down more.
In Linux, every file and directory has an associated set of permissions that determine who can access and modify it.
These permissions are divided into three categories for three types of users:
Owner: The user who owns the file.
Group: The group that owns the file.
Others: All other users.
Each of these categories can have three types of permissions:
Read (r): Permission to read the file or list the directory contents.
Write (w): Permission to modify the file or directory.
Execute (x): Permission to execute the file or traverse the directory.
Breaking down the example below:
The first character represents the file type (- for a regular file, d for a directory).
The next three characters (rwx) represent the owner’s permissions: read, write, and execute.
The following three characters (r-x) represent the group’s permissions: read and execute, but not write.
The last three characters (r–) represent the permissions for others: read-only.
The way to set permissions using numbers would be to add them together.
Owner: read (4), write (2), execute (1), adding them all together (4+2+1) = 7
Group: read (4), execute (1), adding them together (4+1) = 5
Other: read (4) = 4
(rwx) = 7
(r-x) = 5
(r–) = 4
In the example, the permissions show -rwxr-xr– and is set by using “chmod 754 file.txt”
3.2 “chown”: Changes file owner and group
chown user:group file.txt
4: Viewing and Editing Files
4.1 “cat”: Displays file content
cat file.txt
4.2 “nano”: A simple text editor
nano file.txt
4.3 “vim”: A more advanced text editor
vim file.txt
5: System Information
5.1 “uname”: Displays system information
uname -a
5.2 “top”: Displays active processes
top
5.3 “htop”: Displays active processes, but allows you to scroll vertically and horizontally, and use your mouse to interact.
htop
Use Case:
Imagine you need to set up a new project on a Linux server. You can use the command line to create directories for your project, set the appropriate permissions, and navigate the file system to manage your files effectively.
Example:
1. Open the terminal.
2. Create a new project directory:
mkdir my_project
3. Navigate into the project directory:
cd my_project
4. Create subdirectories for organization:
mkdir new_project
5. Create a README file, change the permissions, and edit it:
touch README.md
chmod 754 README.md
nano README.md
Conclusion
The Linux command line is a powerful tool that provides direct access to the system’s functionality. Mastering basic commands can significantly enhance your productivity and control over your Linux environment.
What Next:
1. Practice: Regularly use the command line to become more familiar with its capabilities.
2. Learn Advanced Commands: Explore more complex commands and shell scripting.
3. Join Communities: Engage with Linux communities and forums to learn from others and solve problems collaboratively.
References
Linux Command Line Basics – Ubuntu Documentation
The Linux command line for beginners