top of page

Linux File & Directory Management

Writer: compnomicscompnomics

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:

  1. Create a Directory:

$ mkdir test_dir
  1. Navigate into the Directory:

$ cd test_dir
  1. Create a File:

$ touch example.txt
  1. Add Text to a File:

$ echo "This is a test" > example.txt
$ echo "Adding more text" >> example.txt
  1. 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 (~).

    1. Create a directory named "documents".

    2. Navigate into "documents".

    3. Create a file named "report.txt".

    4. Add the line "This is my report." to "report.txt".

    5. Return to your home directory.

    6. Use ls -l and the absolute path to display detailed information about "report.txt".

    7. use cat and the relative path to display the content of report.txt from your home directory.


Suggested Answer/Framework:

  1. $ mkdir documents

  2. $ cd documents

  3. $ touch report.txt

  4. $ echo "This is my report." > report.txt

  5. $ cd ..

  6. $ 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

  7. $ cat documents/report.txt

    • Output:

      This is my report.

 
 
 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page