DevOps Notes

Home / DevOps Intro & Linux / Lab 01

⚙ Lab 01 — Hands-on Linux

Lab 01 / 03 — Linux and Bash Scripting Practice Session 1

Lab Objectives

  • File & Directory Operations
  • Permissions & Hidden Files
  • User & Group Management

Section 1: Create File and Directories

1
Basic — Create a directory named lab1 and an empty file named file1.txt inside it.
Use mkdir to create directories and touch to create empty files.
mkdir lab1
touch lab1/file1.txt
2
Basic — Create 5 empty files (file1.txt to file5.txt) in a directory named lab2.
You can create multiple files with touch in one command. Use brace expansion {1..5} to generate file names.
mkdir lab2
touch lab2/file{1..5}.txt
3
Basic — Create a nested directory structure: lab3/parent/child.
Use mkdir -p to create parent directories as needed.
mkdir -p lab3/parent/child
4
Basic — Create a directory named docs and a text file with initial content.
Use echo with output redirection (>) or nano to create a file with content.
mkdir docs
echo "Hello World" > docs/file1.txt
cat docs/file1.txt
5
Intermediate — Create a directory named project with specific permissions (read-only for others).
Use chmod after creating the directory. The octal value 754 gives rwx to owner, r-x to group, and r-- to others.
mkdir project
chmod 754 project
6
Intermediate — Create a directory structure and copy it using cp -r.
The -r flag means recursive — it copies directories and all their contents.
mkdir first_dir
touch first_dir/file1.txt
cp -r first_dir second_dir

Section 2: Hidden and Non-Hidden Files

1
Basic — Create a hidden file .hidden1 in your home directory.
In Linux, files starting with a dot (.) are hidden. Just name your file starting with a dot.
touch .hidden1
2
Basic — Create a directory and add both hidden and non-hidden files inside it.
Create a directory with mkdir, then use touch to create both .file1 (hidden) and file2 (visible).
mkdir lab4
cd lab4
touch .file1 file2
3
Basic — List all files (hidden and non-hidden) in a directory and list hidden only.
Use ls -a to show all files including hidden ones. Use ls -d .* to show only hidden files.
ls -a        # list all (hidden + non-hidden)
ls -d .*     # list hidden files only
4
Basic — Delete a hidden file and verify it is removed.
Use rm to delete a file. Then ls -a to verify it's gone.
rm -f .file1
ls -a
5
Intermediate — Create 5 hidden files at once using touch.
Use brace expansion with a dot prefix: .file{1..5}
touch .file{1..5}
ls -a
6
Intermediate — Find all hidden files in a directory using find.
The find command with -name ".*" matches files starting with a dot. Add -type f to limit to files only.
find . -type f -name ".*"

Section 3: Copy and Rename

1
Basic — Copy a file from one directory to another.
Use cp source destination to copy files between directories.
cp lab1/file1.txt lab4/file6
2
Basic — Rename a file in the same directory.
The mv command is used for both moving and renaming files.
cd lab4
mv file6 file1
ls
3
Basic — Copy a directory and all its contents to a new location.
Use cp -r (recursive) to copy an entire directory.
cp -r lab4 lab5
4
Intermediate — Copy a file and rename it during the process.
With cp, just specify a different filename at the destination: cp source dest/newname
cd lab5
cp file1 file3

Section 4: Add Lines to File

1
Basic — Create a file and write a line to it.
Use echo "text" > filename to write to a file. The > operator creates or overwrites the file.
echo "Mohamed Elsayeh" > new_file
cat new_file
2
Basic — Append a second line to the file.
Use >> (double redirect) to append without overwriting the existing content.
echo "DevOps" >> new_file
cat new_file
3
Basic — Use nano to add text to a file manually.
nano is a terminal text editor. Use Ctrl+O to save and Ctrl+X to exit.
touch new_file
nano new_file
# Type your text, then Ctrl+O to save, Ctrl+X to exit
cat new_file
4
Intermediate — Write multiple lines to a file with cat and redirect (heredoc).
Use cat >> filename <<EOF to write multiple lines. Everything until the EOF marker is written to the file.
cat >> new_file2 <<EOF
Software Engineer
Alexandria, Egypt
EOF

cat new_file2

Section 5: Users and Groups

1
Intermediate — Create a new user named user1.
Use sudo adduser (interactive) or sudo useradd (non-interactive) to create users.
sudo adduser user1
2
Intermediate — Create a new group named group1.
Use sudo groupadd to create a new group.
sudo groupadd group1
3
Intermediate — Add user1 to group1.
Use sudo gpasswd -a user group or sudo usermod -aG group user to add a user to a group.
sudo gpasswd -a user1 group1
4
Advanced — Create a user with a specific home directory.
Use sudo useradd -m -d /path/to/home username to specify a custom home directory.
sudo useradd user2 -md /home/custom_dir
5
Intermediate — Create another group (group2) and add user1 to this group.
Use -aG flag with usermod to append a group without removing existing groups.
sudo groupadd group2
sudo usermod user1 -aG group2
6
Intermediate — List all users on the system.
All user accounts are stored in /etc/passwd. Use cat to read it.
cat /etc/passwd
7
Advanced — Remove a user.
Use sudo userdel username. Add -r to also remove their home directory.
sudo userdel user2
8
Advanced — Delete a group.
Use sudo groupdel groupname to delete a group. The group must not be the primary group of any user.
sudo groupdel group2