Linux at Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage processes on a Linux system using the at command. The lab covers exploring the Linux file system hierarchy, managing files and directories with essential commands, and understanding and using Linux permissions. You will learn about the different directories in the file system hierarchy and their purposes, as well as how to use commands like ls, cd, and chmod to interact with files and directories. The lab provides practical examples to help you become proficient in process management on a Linux system.

Linux Commands Cheat Sheet

Explore the Linux File System Hierarchy

In this step, you will learn about the Linux file system hierarchy and explore the different directories and their purposes.

The Linux file system is organized in a hierarchical structure, with the root directory (/) being the top-level directory. Let's start by exploring the contents of the root directory:

sudo ls -l /

Example output:

total 80
drwxr-xr-x  2 root root 4096 Apr 18 06:14 bin
drwxr-xr-x  2 root root 4096 Apr 18 06:14 boot
drwxr-xr-x  5 root root 4096 May 11 05:53 dev
drwxr-xr-x 93 root root 4096 May 11 05:53 etc
drwxr-xr-x  3 root root 4096 Apr 18 06:14 home
lrwxrwxrwx  1 root root   33 Apr 18 06:14 initrd.img -> boot/initrd.img-5.15.0-58-generic
drwxr-xr-x 20 root root 4096 Apr 18 06:14 lib
drwxr-xr-x  2 root root 4096 Apr 18 06:14 lib64
drwxr-xr-x  2 root root 4096 Apr 18 06:14 media
drwxr-xr-x  2 root root 4096 Apr 18 06:14 mnt
drwxr-xr-x  2 root root 4096 Apr 18 06:14 opt
dr-xr-xr-x 99 root root    0 May 11 05:53 proc
drwx------  2 root root 4096 Apr 18 06:14 root
drwxr-xr-x  5 root root 4096 Apr 18 06:14 run
drwxr-xr-x  2 root root 4096 Apr 18 06:14 sbin
drwxr-xr-x  2 root root 4096 Apr 18 06:14 snap
drwxr-xr-x  2 root root 4096 Apr 18 06:14 srv
dr-xr-xr-x 13 root root    0 May 11 05:53 sys
drwxrwxrwt  2 root root 4096 May 11 05:53 tmp
drwxr-xr-x 12 root root 4096 Apr 18 06:14 usr
drwxr-xr-x 14 root root 4096 Apr 18 06:14 var
lrwxrwxrwx  1 root root   30 Apr 18 06:14 vmlinuz -> boot/vmlinuz-5.15.0-58-generic

The root directory contains several subdirectories, each with a specific purpose:

  • /bin: Essential user binaries (commands)
  • /boot: Boot loader files
  • /dev: Device files
  • /etc: System configuration files
  • /home: User home directories
  • /lib: Essential shared libraries and kernel modules
  • /media: Mount point for removable media
  • /mnt: Mount point for temporary file systems
  • /opt: Optional software packages
  • /proc: Virtual file system for kernel and process information
  • /root: Home directory for the root user
  • /run: Runtime variable data
  • /sbin: System binaries (administrative commands)
  • /srv: Service data
  • /sys: Virtual file system for kernel objects
  • /tmp: Temporary files
  • /usr: Secondary hierarchy for read-only user data
  • /var: Variable files (logs, spool, cache, etc.)

Explore these directories and their contents to understand the Linux file system hierarchy better.

Manage Files and Directories with Essential Commands

In this step, you will learn how to manage files and directories using essential Linux commands.

First, let's create a new directory in your home directory:

cd ~/project
mkdir my_directory

Now, let's navigate into the new directory and create a file:

cd my_directory
touch my_file.txt

You can verify the file was created:

ls -l

Example output:

total 0
-rw-r--r-- 1 labex labex 0 May 11 06:01 my_file.txt

Next, let's add some content to the file:

echo "This is my file content." > my_file.txt

You can view the file contents:

cat my_file.txt

Example output:

This is my file content.

To copy the file:

cp my_file.txt my_copied_file.txt

And to move the file:

mv my_copied_file.txt my_moved_file.txt

Finally, let's delete the files and the directory:

rm my_file.txt my_moved_file.txt
rmdir my_directory

Verify that the files and directory have been removed:

ls -l ~/project

Example output:

total 0

Understand and Use Linux Permissions

In this step, you will learn about Linux file and directory permissions and how to manage them.

Linux uses a permission system to control access to files and directories. Each file and directory has three types of permissions: read, write, and execute. These permissions can be assigned to the file/directory owner, the group the file/directory belongs to, and all other users.

Let's create a new file and explore its permissions:

cd ~/project
touch my_file.txt
ls -l my_file.txt

Example output:

-rw-r--r-- 1 labex labex 0 May 11 06:10 my_file.txt

The permissions are displayed as a string of 10 characters, where:

  • The first character indicates the file type (- for regular file, d for directory)
  • The next 3 characters indicate the owner's permissions (read, write, execute)
  • The next 3 characters indicate the group's permissions
  • The final 3 characters indicate the permissions for all other users

To change the permissions, you can use the chmod command. For example, to make the file executable for the owner:

chmod u+x my_file.txt
ls -l my_file.txt

Example output:

-rwxr--r-- 1 labex labex 0 May 11 06:10 my_file.txt

You can also use numeric values to set permissions. The values are:

  • 4 for read
  • 2 for write
  • 1 for execute
  • 0 for no permission

For example, to set the permissions to read-write-execute for the owner, read-execute for the group, and read-only for others:

chmod 754 my_file.txt
ls -l my_file.txt

Example output:

-rwxr-xr- 1 labex labex 0 May 11 06:10 my_file.txt

Finally, let's create a directory and set its permissions:

mkdir my_directory
chmod 755 my_directory
ls -ld my_directory

Example output:

drwxr-xr-x 2 labex labex 4096 May 11 06:10 my_directory

The 755 permissions allow the owner to read, write, and execute, while the group and others can only read and execute.

Summary

In this lab, you will first explore the Linux file system hierarchy, learning about the different directories and their purposes. You will then manage files and directories using essential Linux commands, such as ls, cd, mkdir, and rm. Finally, you will understand and use Linux permissions, including the concepts of read, write, and execute permissions for files and directories.

The lab covers the fundamental aspects of navigating and managing the Linux file system, which are essential skills for any Linux user or administrator. By the end of the lab, you will have a solid understanding of the Linux file system structure and the ability to effectively work with files and directories using the command line.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like