Appearance
Day 2: Basic Command Line Operations
Objectives
- Learn basic command line operations.
- Understand file and directory management.
- Get familiar with permissions and how to modify them.
1. Basic Command Line Operations
Navigating the File System
Print Working Directory
- Display the current directory:bash
$ pwd
- Display the current directory:
Listing Directory Contents
- List files and directories in the current directory:bash
$ ls
- List detailed information including hidden files:bash
$ ls -al
- List files and directories in the current directory:
Changing Directories
- Change to a specific directory:bash
$ cd /path/to/directory
- Change to the home directory:bash
$ cd ~
- Change to the previous directory:bash
$ cd -
- Change to a specific directory:
File and Directory Management
Creating Directories
- Create a new directory:bash
$ mkdir new_directory
- Create a new directory:
Creating Files
- Create an empty file:bash
$ touch new_file
- Create an empty file:
Copying Files and Directories
- Copy a file:bash
$ cp source_file destination_file
- Copy a directory and its contents:bash
$ cp -r source_directory destination_directory
- Copy a file:
Moving/Renaming Files and Directories
- Move or rename a file or directory:bash
$ mv source destination
- Move or rename a file or directory:
Deleting Files and Directories
- Delete a file:bash
$ rm file_name
- Delete a directory and its contents:bash
$ rm -r directory_name
- Delete a file:
2. Understanding Permissions
File Permissions Basics
- Each file and directory has permissions that control who can read, write, or execute them.
- Permissions are divided into three groups:
- Owner: The user who owns the file.
- Group: Users who are members of the file’s group.
- Others: All other users.
Viewing Permissions
- Use the
ls -l
command to view detailed information, including permissions:bash$ ls -l
Permission Symbols
- Permissions are represented by a string of characters, e.g.,
-rwxr-xr--
.-
: File type (e.g.,-
for a regular file,d
for a directory).r
: Read permission.w
: Write permission.x
: Execute permission.- The first set of three characters represents the owner's permissions, the second set represents the group's permissions, and the third set represents others' permissions.
Changing Permissions
- Use the
chmod
command to change permissions.- Add permission:bash
$ chmod +x file_name
- Remove permission:bash
$ chmod -w file_name
- Set specific permissions (e.g.,
rwxr-xr--
):bash$ chmod 754 file_name
- Add permission:
Changing Ownership
Use the
chown
command to change the owner of a file or directory:bash$ sudo chown new_owner file_name
Change the group of a file or directory:
bash$ sudo chown :new_group file_name
Using sudo
for Superuser Privileges
sudo
allows a permitted user to execute a command as the superuser or another user.- Run a command with superuser privileges:bash
$ sudo command
- Example: Update package lists:bash
$ sudo apt update
- Run a command with superuser privileges:
Practical Exercises
Exercise 1: Basic Navigation and Management
- Open the terminal.
- Navigate to the home directory:bash
$ cd ~
- Create a new directory named
practice
:bash$ mkdir practice
- Navigate to the
practice
directory:bash$ cd practice
- Create an empty file named
example.txt
:bash$ touch example.txt
- List the contents of the directory:bash
$ ls
Exercise 2: File Permissions
- View the permissions of
example.txt
:bash$ ls -l example.txt
- Add execute permission to the owner:bash
$ chmod u+x example.txt
- Verify the changes:bash
$ ls -l example.txt
- Remove write permission from others:bash
$ chmod o-w example.txt
- Verify the changes:bash
$ ls -l example.txt
Exercise 3: Superuser Commands
- Update the package list using
sudo
:bash$ sudo apt update
- Install a package (e.g.,
curl
):bash$ sudo apt install curl