0% found this document useful (0 votes)
26 views4 pages

Directories and Files

linux rhel 9 files and directories

Uploaded by

vikrantschohaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Directories and Files

linux rhel 9 files and directories

Uploaded by

vikrantschohaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Linux Directories Tutorial

Introduction to Linux Directories


In Linux, directories are like folders on your computer that help you organize files. Let's
explore what directories are, how to navigate them, and how to manage files!

1. Default Directories in Linux

When you open a Linux system, you will see several default directories:

• / (Root Directory): The top-level directory containing everything in your Linux


system.
• /home: Contains home directories for all users. Each user has a unique folder here to
store personal files.
• /etc: Contains configuration files for the system.
• /var: Holds variable files such as logs and temporary files.
• /usr: Contains user programs and utilities, where most installed software is located.

2. Understanding Root Path and Home Directory

• Root Path: Represented by /, it is the starting point of the file system. All other
directories are within this path.
• Home Directory: Located at /home/username, it is a special place for you to store
your files.

3. Relative Path vs. Absolute Path

• Absolute Path: Starts from the root directory and provides the complete location of a
file or directory. Example:
/home/username/documents/file.txt
• Relative Path: Starts from your current directory. If you are in your home directory,
you can refer to the documents folder as:
documents/file.txt

4. Navigating Directories

You can use the cd command (which stands for "change directory") to navigate through
directories. Here are various commands for navigating:

• To go to your home directory:

bash
Copy code
cd ~

• To go to the root directory:

bash
Copy code
cd /

• To go back to the previous directory:

bash
Copy code
cd -

• To go up one directory level:

bash
Copy code
cd ..

• To go up two directory levels:

bash
Copy code
cd ../..

• To go up three directory levels:

bash
Copy code
cd ../../..

• To navigate to a specific path:

bash
Copy code
cd /home/username/documents

5. Creating, Deleting, and Renaming Directories

Creating a Directory: Use the mkdir command followed by the directory name.

bash
Copy code
mkdir new_directory

Deleting a Directory: Use the rmdir command followed by the directory name to delete an
empty directory.

bash
Copy code
rmdir new_directory

Note: The directory must be empty to delete it.

If the directory is not empty and you want to delete it along with all its contents, you can use
the rm command:

• Delete a Directory and its Contents:


bash
Copy code
rm -r directory_name

The -r option stands for "recursive," which means it will delete the directory and
everything inside it.

• Force Delete a Directory:


If you want to delete a directory without being prompted for confirmation, use the -f
option with rm:

bash
Copy code
rm -rf directory_name

The -f option stands for "force," and it will ignore non-existent files and never
prompt for confirmation.

Renaming a Directory: Use the mv command followed by the old directory name and the
new directory name.

bash
Copy code
mv old_directory new_directory

6. Creating and Writing Files

You can create files using several methods. Here’s how to use the cat command:

Using cat to Create a File:

bash
Copy code
cat > myfile.txt

• Type your content.


• Press CTRL + D to save and exit.

Viewing File Contents: To see the contents of a file, use the cat command:

bash
Copy code
cat myfile.txt

Appending Text to a File: To add more text to an existing file without deleting the old
content, use the >> operator:

bash
Copy code
echo "This is additional text." >> myfile.txt

Adding More Lines Using cat: You can append to a file using the cat command as well:
bash
Copy code
cat >> myfile.txt

• Type new lines of content.


• Press CTRL + D to save and exit.

7. Using vim to Create and Edit Files

Open a File with vim: You can create a new file or open an existing one:

bash
Copy code
vim myfile.txt

Basic vim Commands:

1. Insert Mode: Press i to enter insert mode, allowing you to type and edit the file.
2. Save Changes: After editing, press ESC to exit insert mode, then type :w and press
Enter to save.
3. Exit vim: Type :q and press Enter to quit. To save and exit at the same time, use
:wq.
4. Force Quit: If you want to exit without saving changes, type :q! and press Enter.

8. Summary

Now you know about Linux directories, paths, and how to manage files! Here’s a quick
recap:

• Directories help organize files.


• Root is the top-level directory, and Home is where your personal files are stored.
• Use absolute paths for complete file locations and relative paths for shortcuts.
• Navigate using cd, and manage directories with mkdir, rmdir, and rm -r/rm -rf.
• Create files using cat, view them with cat, append using echo, and edit them with
vim.

Practice Exercises

1. Create a new directory named school_projects.


2. Navigate to the school_projects directory.
3. Create a new file named homework.txt using the cat command and write "Math
homework" in it.
4. Open homework.txt using vim, add a new line saying "Science project," and save the
changes.
5. Rename homework.txt to math_homework.txt.
6. Delete the school_projects directory (after ensuring it is empty) using rmdir.
7. Create a new directory called temp_projects, add some files in it, and then delete it
using rm -rf.

You might also like