Leaving presentation mode.

Manage Files from the Command Line

Dennis Kibbe

Mesa Community College

Keyboard Shortcuts

Welcome everyone. In this module, we’ll explore how to manage files using the Linux command line. It’s essential for both server administration and everyday Linux use. This slide presentation was created by B6Plus. The audio accompanying this presentation is AI-generated.

Module Outline

This outline shows our roadmap: Linux filesystem concepts, specifying files by name, file manipulation with command-line tools, creating links, pattern matching, quizzes, guided exercises, a graded lab, and the module summary.

Learning Objectives

After completing the work in this module you will be able to:

By module's completion, you should be able to describe the directory hierarchy, navigate using absolute and relative paths, create, copy, move, delete files and directories, set up hard and symbolic links, and use shell expansions for efficiency.

Describe Linux File System Hierarchy Concepts

We begin with understanding the structure of the Linux filesystem—rooted in a single inverted tree, starting at a forward slash (/). This differs from Windows drive-letter systems.

The File-system Hierarchy

Unlike Windows Linux organizes files into a single inverted tree-like structure even when files exist on a separate storage device. Drive letters such as C, D, and E are not used. The top of the file system is represented by a / (forward slash).

The boot Directory

The /boot holds files essential for booting—like the Linux kernel (vmlinuz) and initial ramdisk images, needed before mounting the root filesystem.

The dev Directory

The /dev is a virtual directory housing device files—interfaces to hardware devices. For example, /dev/sda represents a physical disk.

The etc Directory

The etc directory holds system-wide configuration files. It's where you configure services such as SSH (/etc/ssh/sshd_config) and system settings.

The home Directory

Under the home directory each unprivileged user has a personal directory. This is where personal files, configurations, and documents reside. For example, the personal files of user student are stored in /home/student. If a user megan exists her files would be under /home/megan.

The lib Directory

Libraries shared by programs—such as those needed for spell-checking—live in /lib or /usr/lib. These libraries support executable binaries.

The proc Directory

The proc directory is a virtual directory created when the system boots to provide information about system resources. For example, /proc/meminfo contains detailed information about system memory.

The root Directory

The root directory is the personal directory for the administrator or superuser - not to be confused with the filesystem root /. It's used when the root user logs in. However, following best practices the system administrator will work as as an unprivileged user and only gain root privileges when needed.

The run Directory

The run directory is a temporary directory in memory for storing various information such as lock files and sockets.

The tmp Directory

The tmp directory stores temporary files from both users and programs. It's cleared periodically or on reboot and is writable by all users.

The usr Directory

Installed software and shared libraries are found in the usr directory. The /bin and /sbin directories are symbolically linked to /usr/bin and /usr/sbin.

The var Directory

Dynamic data—like logs, caches, mail spools—resides in /var. For example, system logs are typically found in /var/log.

Graded Quiz

Describe Linux File System Hierarchy Concepts

Time for a quick graded quiz: identify and describe the purpose of these key directories based on what we've just covered.

Specify Files by Name

Next, we'll learn how to access files using absolute and relative paths—crucial when navigating or executing commands.

Absolute Paths

[student@servera ~]$ ls /etc/ssh/sshd.config
[student@servera ~]$ pwd
/home/student/
      

The absolute path to a file always starts at the top of the file system represented by a forward slash (/) and can be used to navigate to a file from anywhere on the system. The pwd or Print Working Directory command shows the absolute path to the directory you are currently in.

The Current Working Directory and Relative Paths

[student@servera /etc/ssh/]$ sudo vim sshd.config      
      

Using a relative path to a file requires less typing then using the absolute path. In this example, student is already in the /etc/ssh/ directory as you can see by the prompt so only the file name is needed, but that will not work from anywhere else on the system.

Navigate Paths in the File System

[student@servera ~]$ cp /etc/passwd .    
    

Linux provides a number of shortcuts to help you navigate the system. A single period (.) represents the current directory and two periods (..) represents the parent directory. These shortcuts can be used in commands.

In this example a file is copied to the current directory from the /etc directory. Note the single period as the destination. A single period represents the current working directory in this case student's home directory.

Move a File to the Parent Directory

[student@servera Documents]$ mv file.txt ../
[student@servera Documents]$ ls ../
Documents Music Desktop Pictures Videos file.txt      
      

In this example, file.txt is moved to the parent directory of the Documents directory. ../ represents the parent directory in both commands.

Graded Quiz

Specify Files by Name

After reading this section in the textbook complete the quiz in Canvas.

Manage Files with Command-line Tools

Create, copy, move, and remove files and directories

Creating, copying, moving, and removing files are common operations. In this section you will learn command line tools to perform these tasks.

Command-line File Management

Creating, copying, moving, and removing files are common operations. In this section you will learn command line tools to perform these tasks.

Create Empty Files

[student@servera ~]$ touch file file1 file2 file3
[student@servera ~]$ ls file*
file file1 file2 file3
      

The touch command is useful for creating one or more empty files when you need files to test a command. If used with files that already exist touch will update the timestamp on the files. This can be useful if you want all the files to have the same modification date when distributing them in an archive.

Create Directories

[student@servera ~]$ mkdir -v 'My Stuff'
mkdir: created directory 'My Stuff'
      

The mkdir command creates one or more new directories. If the directory name contains a space the name must be enclosed in quotes. The -v option confirms that the MyStuff directory was created.

Copy Files and Directories

[student@servera ~]$ cp file1 file2
      

The cp command copies a file to a new file. If the new file name already exists it is overwritten. The source file is always the first argument to the command followed by the destination file name. If the last argument is a directory the file or files are copied into that directory.

Move Files and Directories

[student@servera ~]$ mv file1 file2 Documents/
[student@servera ~]$ ls Documents/
file1 file2
      

The mv command serves two purposes. It can move a file to a new location or rename a file. Here file1 is moved from student's home directory to the Documents directory and renamed to file3.

Remove Files and Directories

[student@servera ~]$ rm file2
      
[student@servera ~]$ rmdir MyStuff
rmdir: failed to remove 'MyStuff': Directory not empty
      
[student@servera ~]$ rm -rf MyStuff
      

The rm command will delete a file or if used with the recursive option it removes a directory and its contents. The deletion is permanent. There is no recycle bin or undo on the command line.

Guided Exercise

Manage Files with Command-line Tools

Lab Notes

Make Links Between Files

You can refer to a file by creating multiple links. There are two types of links, hard links and symbolic or "soft" links.

Create Hard Links Between Files

[student@servera ~]$ ln file1 file2
[student@servera ~]$ ls -li file1 file2
1643415 -rw-r--r-- 2 user user 0 Jun 29 06:43 file1
1643415 -rw-r--r-- 2 user user 0 Jun 29 06:43 file2
      

A hard link points directly to the data on the file system not to a file name. A file name is just a reference to where the data is stored on the file system. You can create multiple hard links to the data. As long as one link still exists the data is accessible. Only when the last link is removed is the data no longer accessible.

In this example a hard link is created from file1 to file2. The ls -li command shows that each file has two links and the inode numbers for each file are the same. An inode tells the system where the data is on the storage device.

Limitations of Hard Links

A hard link can not span multiple file systems and a hard link to a directory is not possible.

Create Symbolic Links

[student@servera ~]$ ln -s soft_link file1
[student@servera ~]$ ls -l soft_link
lrwxrwxrwx 1 student student 8 Jun 29 soft_link -> file1
      

A symbolic or "soft" link points to a file name not to an inode. If the referenced file is deleted the link is broken and the data is lost.

In this example a soft link named soft_link is created to file1. The ls -l command shows that soft_link is a link. If file1 is deleted the link is broken and the data is lost.

Guided Exercise

Make Links Between Files

Lab Notes

Step 2.2: Using a relative path and tab completion this command can be entered by typing far fewer characters.

Match File Names with Shell Expansions

Often you can save time by using pathname expansion or pattern matching to perform operations on multiple files with a single command.

Command-line Expansions

Work lazy; work smart

When you type a command at the Bash shell prompt, the shell processes that command line through multiple expansions before running it. You can use these shell expansions to perform complex tasks that would otherwise be difficult or impossible.

Pathname Expansion and Pattern Matching

Pattern Matches
* Zero or more characters
? A single character
[[abc]] Any one character in the enclosed class
[[!abc]] Any one character not in the enclosed class
[[:alpha:]] Any alphabetic character
[[:alnum:]] Any alphabetic character or digit
[[:digit:]] Any single digit from 0 to 9

Pathname expansion expands a pattern of special characters that represent wild cards or classes of characters into a list of file names that match the pattern. See table in textbook.

Brace Expansion

[student@servera ~]$ touch file{1..5}
[student@servera ~]$ ls file*
file1 file2 file3 file4 file5
      

In this example brace expansion is used to create a sequence of files with a minimum of typing.

Tilde Expansion

[student@servera Documents/MyStuff]$ ls ~/Videos
video1.mp4 video2.mp4 video3.mp4
      

The tilde (~) represents the user's home directory and is a useful shortcut to that directory from anywhere on the system.

Variable Expansion

student@servera ~]$ echo "The system language is set to $LANG."
The system language is set to en_US.UFT-8.
      

The $ in front of LANG tells the shell that this is a variable. The shell will use the value of the varible in the echo command.

Protecting Arguments from Expansion

[student@servera ~]$ echo "The language is set with \$LANG."
The language variable $LANG sets the system language.
      

A backslash (\) before a reserved character tells the shell to treat the character as an ordinary character.

Command Substitution

[student@servera ~]$ tar -cf backup-$(date +%F).tar
[student@worstation ~]$ ls backup*
backup-2023-11-01.tar
      

Similar to variable expansion the value from the date command is added to the file name making it easy to know when the backup was created.

Graded Quiz

Match File Names with Shell Expansions

Complete this quiz in Canvas.

Module Summary

Manage Files from the Command Line

  1. Files on a Linux system are organized in a tree-like, hierarchical format.
  2. An absolute path always starts with a forward slash (/) and defines the exact location of a file on the system regardless of the current directory.
  3. A relative paths does not start with a forward slash and specifies a file's location only in relation to the current working directory.
  4. The dot (.), the double dot (..), and the tilde (~) can be used in commands to define locations on the system.
  5. mkdir, rmdir, cp, mv, and rm are key commands to manage files in Linux.
  6. Hard and symbolic (soft) links create pointers directly to data on disk or to a file name.
  7. Bash pattern matching can be used to run commands more efficiently.

Resources

Graded Lab

Manage Files from the Command Line

Lab Notes

Thanks for Watching

MCC logo

Thanks for watching. This is the end of the presentation.

Created on 17 February 2025 by Dennis Kibbe. Last modified on 9 June 2025 09:21:00 by DNK.