Manage Files from the Command Line

Dennis Kibbe

Mesa Community College

Module Outline

  • Describe Linux File System Hierarchy Concepts
  • Graded Quiz
  • Specify Files by Name
  • Graded Quiz
  • Manage Files with Command-line Tools
  • Guided Exercise
  • Make Links Between Files
  • Guided Exercise
  • Match File Names with Shell Expansions
  • Graded Quiz
  • Graded Lab
  • Module Summary

Learning Objectives

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

  • Describe how Linux organizes files and directories in a tree-like hierarchy.
  • Explain how absolute path and the relative path to a file differ.
  • Change the working directory and list the contents of directories.
  • Create, copy, move, and remove files and directories.
  • Create hard and soft (symbolic) links to files.
  • Use pattern matching to efficiently run commands.

Describe Linux File System Hierarchy Concepts

The File System Hierarchy

Hierarchical directory structure of a file system with various directories and files depicted in a flowchart format.

The boot Directory

Hierarchical directory structure of a file system with various directories anASU AI Voiceover Generatord files depicted in a flowchart format.

The dev Directory

Hierarchical directory structure of a file system with various directories and files     depicted in a flowchart format.

The etc Directory

Hierarchical directory structure of a file system with various directories and files         depicted in a flowchart format.

The home Directory

Hierarchical directory structure of a file system with various directories and files             depicted in a flowchart format.

The lib Directory

The proc Directory

The root Directory

The run Directory

The tmp Directory

The usr Directory

The var Directory

Graded Quiz

Describe Linux File System Hierarchy Concepts

Specify Files by Name

Absolute Paths

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

The Current Working Directory and Relative Paths

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

Navigate Paths in the File System

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

Move a File to the Parent Directory

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

Graded Quiz

Specify Files by Name

Manage Files with Command-line Tools

Create, copy, move, and remove files and directories

Create Empty Files

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

Create Directories

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

Copy Files and Directories

[student@servera ~]$ cp file1 file2
      

Move Files and Directories

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

Move and Rename Files and Directories

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

Remove a Directory

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

Remove a Directory Containing Files

[student@servera ~]$ rm -rf MyStuff
      

Guided Exercise

Manage Files with Command-line Tools

Make Links Between Files

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
      

Limitations of Hard Links

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
      

Guided Exercise

Make Links Between Files

Match File Names with Shell Expansions

Command-line Expansions

Work lazy; work smart

[student@workstation ~]$ ls Pictures/*.jpg | wc -l
46
[student@workstation ~]$
      

Pathname Expansion and Pattern Matching

Pattern Matches
* Zero or more characters
? A single character
* Zero or more characters
[[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

Brace Expansion

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

Tilde Expansion

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

Variable Expansion

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

Protecting Arguments from Expansion

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

Command Substitution

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

Graded Quiz

Match File Names with Shell Expansions

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

MCC logo