Table of Contents
Learn Linux - Part 2
File and Directory Management

Getting the hang of the Linux filesystem can be a bit tricky at first, but it’s a super handy skill to have in your toolkit. I put together this guide mainly for myself—something I can refer back to whenever I need a refresher. But if it helps someone else along the way, that’s a bonus!

Introduction

Imagine you’re the system administrator for a tech company. Your task is to set up the directory structure for a new project called “AlphaProject.” This involves organizing the workspace for developers, designers, and quality assurance testers, each with different access needs. It’s your job to create a smooth, secure system that everyone can use effectively. Let’s dive into the steps to get this done.

Setting Up The Directory Structure

First things first, let’s create the main project directory and subdirectories for each team. We can do this in one swoop with the mkdir command:

				
					mkdir -p /projects/AlphaProject/{developers,designers,qa}
				
			

This command sets up the main directory /projects/AlphaProject and three subdirectories: developers, designers, and qa all at once.

Creating Files and Directories

Within these directories, we’ll create some files and additional subdirectories. For example, developers need a subdirectory for scripts, and designers need one for assets:

				
					mkdir /projects/AlphaProject/developers/scripts
mkdir /projects/AlphaProject/designers/assets
				
			

To create files, use the touch command:

				
					touch /projects/AlphaProject/developers/scripts/init.sh
touch /projects/AlphaProject/designers/assets/logo.png
				
			

These commands create an empty script file and an image file, ready for content.

Accessing Hidden Files and Directories

In Linux, hidden files and directories start with a dot (.). To list all files, including hidden ones, use the ls -a command:

				
					ls -a /projects/AlphaProject/developers
				
			

This command will show you everything, including hidden files like .hidden_config.

Viewing and Changing File and Directory Permissions

Permissions are crucial for controlling access. To view detailed information, including permissions, use:

				
					ls -l /projects/AlphaProject/developers/scripts
				
			

This output shows the permissions, owner, and group for each file:

				
					total 0
-rw-r--r-- 1 user group 0 Jan 1 12:00 init.sh
				
			

To modify permissions, use the chmod command. For example, to give execute permission to the script file:

				
					chmod +x /projects/AlphaProject/developers/scripts/init.sh
				
			

You can also set specific permissions using numeric mode. For instance, setting read, write, and execute permissions for the owner, and read and execute for the group and others:

				
					chmod 755 /projects/AlphaProject/developers/scripts/init.sh
				
			

This command sets the permissions as rwxr-xr-x.

Setting Ownership

Use the chown command to change the owner and group of a file or directory. For example, to set the owner to devuser and the group to devgroup:

				
					sudo chown devuser:devgroup /projects/AlphaProject/developers/scripts/init.sh
				
			

This command updates the ownership to reflect the correct user and group.

Accessing Secured Files

Sometimes, you need to access files with restricted permissions. Use sudo to execute commands with superuser privileges:

				
					sudo cat /etc/securefile
				
			

This command lets you view the contents of a file that requires elevated permissions.

Understanding Permissions and Access Control

To understand who has access to a file or directory, look at the permissions string. For example:

				
					ls -l /projects/AlphaProject/developers/scripts/init.sh
-rwxr-xr-x 1 devuser devgroup 0 Jan 1 12:00 init.sh
				
			

This string consists of:

  • -: Type indicator (- for files, d for directories)
  • rwx: Owner permissions (read, write, execute)
  • r-x: Group permissions (read, execute)
  • r-x: Others permissions (read, execute)

Each set of permissions is represented by three characters: r (read), w (write), and x (execute). A dash (-) means the permission is not granted.

Managing Large Files and Directories

To handle large files or directories, use the du (disk usage) command to check sizes:

				
					du -sh /projects/AlphaProject/
				
			

This command provides a summary of the disk usage in a human-readable format.

To find the largest files and directories, use:

				
					du -ah /projects/AlphaProject/ | sort -rh | head -n 10
				
			

This lists the top 10 largest files and directories within the project.

Moving and Copying Files

Use the mv command to move or rename files and directories. To move a script to a different directory:

				
					mv /projects/AlphaProject/developers/scripts/init.sh /projects/AlphaProject/qa/
				
			

To rename a directory:

				
					mv /projects/AlphaProject/developers/scripts /projects/AlphaProject/developers/tools
				
			

The cp command copies files and directories. To copy a file:

				
					cp /projects/AlphaProject/designers/assets/logo.png /projects/AlphaProject/qa/
				
			

To copy an entire directory, use the -r (recursive) option:

				
					cp -r /projects/AlphaProject/developers /projects/Backup/
				
			

This command copies the developers’ directory and its contents to the backup location.

Deleting Files and Directories

To remove files, use the rm command. For example, to delete a file:

				
					rm /projects/AlphaProject/qa/init.sh
				
			

To remove an empty directory, use rmdir:

				
					rmdir /projects/AlphaProject/qa/temp
				
			

To remove a directory and its contents, use rm with the -r option:

				
					rm -r /projects/AlphaProject/qa/temp
				
			

Finding Files Based on Permissions

To find files with specific permissions, use the find command. For example, to find all files with 777 permissions:

				
					find /projects/AlphaProject -type f -perm 0777
				
			

This command searches for files with read, write, and execute permissions for everyone.

Working with Links

Linux supports symbolic (soft) and hard links. A symbolic link is like a shortcut to another file, while a hard link is an additional name for an existing file. To create a symbolic link:

				
					ln -s /projects/AlphaProject/designers/assets/logo.png /projects/AlphaProject/logo_link.png
				
			

To create a hard link:

				
					ln /projects/AlphaProject/developers/scripts/init.sh /projects/AlphaProject/init_link.sh
				
			

Symbolic links can point to directories, while hard links cannot. Use ls -l to view link details:

				
					ls -l /projects/AlphaProject/logo_link.png
				
			

This displays:

				
					lrwxrwxrwx 1 user group 40 Jan 1 12:00 /projects/AlphaProject/logo_link.png -> /projects/AlphaProject/designers/assets/logo.png
				
			

Archiving and Compressing Files

To archive and compress files, use the tar command. Create a tarball (compressed archive) of the project directory:

				
					tar -czvf /projects/AlphaProject.tar.gz /projects/AlphaProject
				
			

To extract the contents of a tarball:

				
					tar -xzvf /projects/AlphaProject.tar.gz -C /projects/

				
			
  • -c: Creates an archive
  • -x: Extracts it
  • -z: Compresses/uncompresses with gzip
  • -v: Shows progress
  • -f: Specifies the filename

File Integrity and Security

To ensure file integrity, use the md5sum or sha256sum commands. Generate a checksum for a file:

				
					md5sum /projects/AlphaProject/designers/assets/logo.png
				
			

This outputs a unique hash, which can be used to verify the file’s integrity later:

				
					d41d8cd98f00b204e9800998ecf8427e /projects/AlphaProject/designers/assets/logo.png
				
			

Scheduling Regular Tasks

To automate file and directory management tasks, use crontab to schedule commands. Edit the crontab file:

				
					crontab -e
				
			

Add a job to back up the project directory every day at midnight:

				
					0 0 * * * tar -czvf /backup/AlphaProject_$(date +\%F).tar.gz /projects/AlphaProject
				
			

This cron job creates a compressed backup with the current date in the filename.

Monitoring FIle and Directory Changes

To monitor changes in real-time, use inotifywait from the inotify-tools package. Install it using:

				
					sudo apt install inotify-tools
				
			

Monitor changes in the project directory:

				
					inotifywait -m /projects/AlphaProject
				
			

This command watches for modifications, deletions, and other events in the specified directory.

Through the real-world example of organizing a project’s directory structure, you’ve learned how to safely handle files, see and change permissions, create and manage directories, and access hidden files. These skills are fundamental for efficient system administration and will be expanded upon in later chapters. As you continue, remember that the “AlphaProject” is a great way to put everything you’ve learned into practice and deepen your understanding of these commands.

Leave a Comment