Navigating the Linux command line efficiently hinges on your ability to manage files and directories. This guide will equip you with the essential commands: touch, mkdir, cd, cat, echo, ls, and pwd, along with a clear understanding of absolute and relative filenames.
Core Commands:
mkdir (Make Directory): Creates new directories.
Example: $ mkdir my_folder
touch: Creates empty files.
Example: $ touch my_file.txt
cd (Change Directory): Navigates between directories.
Example: $ cd my_folder
cat (Concatenate): Displays file contents.
Example: $ cat my_file.txt
echo: Displays text.
$ echo "Hello World"
$ echo "Append this" >> my_file.txt (appends text)
$ echo "Overwrite" > my_file.txt (overwrites content)
ls (List): Lists files and directories.
$ ls (lists current directory)
$ ls -l (lists with detailed information)
pwd (Print Working Directory): Displays the current directory path.
$ pwd
Absolute and Relative Filenames:
Absolute: Full path from the root (/).
Example: /home/user/my_folder/my_file.txt
Relative: Path relative to the current directory.
Example: my_folder/my_file.txt (if you're in /home/user/)
Step-by-Step Guide with Output:
Create a Directory:
$ mkdir test_dir
Navigate into the Directory:
$ cd test_dir
Create a File:
$ touch example.txt
Add Text to a File:
$ echo "This is a test" > example.txt
$ echo "Adding more text" >> example.txt
View File Contents:
$ cat example.txt
Output: This is a test Adding more text
List files in the current directory:
$ ls
Output: example.txt
List files in long format:
$ ls -l
Output (example): -rw-r--r-- 1 user user 31 Aug 15 10:00 example.txt
Display current working directory:
$ pwd
Output (example): /home/user/test_dir
Navigate Back:
$ cd ..
Absolute Path Example:
$ cat /home/user/test_dir/example.txt
Output: This is a test Adding more text
Practice Question:
You are in your home directory (~).
Create a directory named "documents".
Navigate into "documents".
Create a file named "report.txt".
Add the line "This is my report." to "report.txt".
Return to your home directory.
Use ls -l and the absolute path to display detailed information about "report.txt".
use cat and the relative path to display the content of report.txt from your home directory.
Suggested Answer/Framework:
$ mkdir documents
$ cd documents
$ touch report.txt
$ echo "This is my report." > report.txt
$ cd ..
$ ls -l /home/user/documents/report.txt (Replace /home/user/ with your actual home directory path.)
Output (example):
-rw-r--r-- 1 user user 18 Aug 15 10:15 /home/user/documents/report.txt
$ cat documents/report.txt
Output:
This is my report.
Comments