# Manage Files from the Command Line ## Dennis Kibbe ### Mesa Community College Note: This slide presentation was created using Reveal.js. You can access a transcript of this presentation by pressing S for speaker notes. You can access navigation help by pressing the question mark key. Audio for this presentation is artificially generated. --- # Module Outline 1. Introduction 1. Describe Linux File System Hierarchy Concepts 1. Specify Files by Name 1. Manage Files with Command-line Tools 1. Make Links Between Files 1. Match File Names with Shell Expansions 1. Key Takeaways 1. Resources 1. Graded Lab Note: --- # Learning Objectives *After completing the work in this module you will be able to:* 1. Describe how Linux organizes files into a single hierarchical file system. 1. Use absolute and relative paths to locate a file. 1. Use commands to create, copy, move, and delete files. 1. Create hard and soft (symbolic) links to files. 1. Use pattern matching to perform actions on multiple files. Note: --- # Introduction ## Manage Files from the Command Line --- # Describe Linux File System Hierarchy Concepts ![image of a manual page](../images/fsh.png) Note: 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 /. --- # `boot` Directory ![Linux file system hierarchy](../images/fsh_boot.png) Note: The `boot` directory contains files necessary to boot the system. These include the Linux kernel and an initial file system that is made available before the root directory is mounted. --- # `dev` Directory ![Linux file system hierarchy](../images/fsh_dev.png) Note: The `dev` or device directory contains files that represent hardware devices such as storage drives, serial devices, etc. --- # `etc` Directory ![Linux file system hierarchy](../images/fsh_etc.png) Note: The `etc` directory contains system-wide configuration files. --- # `home` Directory ![Linux file system hierarchy](../images/fsh_home.png) Note: The `home` directory is where personal files for unprivileged users are stored. 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`. --- # `lib` Directory ![Linux file system hierarchy](../images/fsh_lib.png) Note: The `lib` or library directory contains files shared between applications such as a spell-checking library. --- # `proc` Directory ![Linux file system hierarchy](../images/fsh_proc.png) Note: 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. --- # `root` Directory ![Linux file system hierarchy](../images/fsh_root.png) Note: The `root` directory is the personal directory for the administrator or superuser. However, following best practices the system administrator will log in as an unprivileged user and only gain root privileges when needed. --- # `run` Directory ![Linux file system hierarchy](../images/fsh_run.png) Note: The `run` directory is a temporary directory in memory for storing various information such as lock files and sockets. --- # `tmp` Directory ![Linux file system hierarchy](../images/fsh_tmp.png) Note: The `tmp` directory is for temporary files and the contents are not permanent. All users can write to this directory. --- # `usr` Directory ![Linux file system hierarchy](../images/fsh_usr.png) Note: 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`. --- # `var` Directory ![Linux file system hierarchy](../images/fsh_var.png) Note: Files that dynamically update such as cache and log files are found in the `var` directory. --- # Graded Quiz ## Describe Linux File System Hierarchy Concepts ![clipboard](../images/clipboard-check.png) Note: After reading this section in the textbook complete the quiz in Canvas. --- # Specify Files by Name Note: In this section you’ll learn how to access files on the system using an absolute or relative path. --- # Absolute Paths ``` [student@workstation ~]$ ls /etc/ssh/sshd.config ``` Note: 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@workstation /etc/ssh/]$ sudo vim sshd.config ``` Note: 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 a relative path will not work from anywhere else on the system. --- # Navigate Paths in the File System Note: 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. --- # Copy a File to the Current Directory ``` [student@workstation ~]$ cp /etc/passwd . ``` Note: 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@workstation Documents]$ mv file.txt ../ [student@workstation Documents]$ ls ../ Documents Music Desktop Downloads Pictures Videos file.txt ``` Note: In this example, file.txt is moved to the parent directory. ../ represents the parent directory in both commands. --- # Graded Quiz ## Specify Files by Name ![clipboard](../images/clipboard-check.png) Note: After reading this section in the textbook complete the quiz in Canvas. --- # Manage Files with Command-line Tools Note: 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@workstation ~]$ touch file1 file2 file3 [student@workstation ~]$ ls file* file1 file2 file3 ``` Note: 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@workstation ~]$ mkdir -v 'My Stuff' mkdir: created directory 'My Stuff' ``` Note: 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@workstation ~]$ cp file1 file2 ``` Note: 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@workstation ~]$ mv file1 Documents/file3 ``` Note: 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@workstattion ~]$ rm file2 ``` Note: 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. --- # Remove an Empty Directory ``` [student@workstation ~]$ rmdir -v 'My Stuff' rmdir: removing directory 'My Stuff/' ``` Note: The `rmdir` command removes an empty directory. If the directory has any content incluing hidden files the `rm -r` command must be used. The `-v` option confirms that the directory and its contents were removed. --- # Guided Exercise ## Manage Files with Command-line Tools ![clipboard](../images/homework.png) Note: In this exercise, you create, organize, copy, and remove files and directories. --- # Make Links Between Files Note: 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 Note: 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. --- # Hard Link Example ``` [student@workstation ~]$ ln file1 file2 [student@workstation ~]$ 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 ``` Note: In this example a hard link is created from `file1` to `file2`. Using 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 Note: A hard link can not span multiple file systems and a hard link to a directory is not possible. --- # Create Symbolic Links Note: A symbolic or ”soft” link points to a file name. If the referenced file is deleted the link is broken and the data is lost. --- # Soft Link Example ``` [student@workstation ~]$ ln -s soft_link file1 [student@workstation ~]$ ls -l soft_link lrwxrwxrwx 1 student student 8 Jun 29 soft_link -> file1 ``` Note: In this example a soft link named `soft_link` is created to `file1`. The `ls -l` command shows the link. If `file 1` is deleted than the link is broken and the data is lost. --- # Guided Exercise ## Make Links Between Files ![Clipart of two students studying](../images/homework.png) Note: In this exercise, you create hard links and symbolic links and compare the results. --- # Pathname Expansion and Pattern Matching Note: Often you can save time by using pathname expansion or pattern matching to perform operations on multiple files with a single command. --- # Brace Expansion ``` [student@workstation ~]$ touch file{1..5} [student@workstation ~]$ ls file* file1 file2 file3 file4 file5 ``` Note: In this example brace expansion is used to create multiple files with a minimum of typing. --- # Tilde Expansion ``` [student@workstation Documents/MyStuff]$ ls ~/Videos video1.mp4 video2.mp4 video3.mp4 ``` Note: The tilde represents the user's home directory and is a useful shortcut to that directory from anywhere on the system. --- # Variable Expansion ``` student@workstation ~]$ echo "The system language is set to $LANG." The system language is set to en_US.UFT-8. ``` Note: 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. --- # Command Substitution ``` [student@workstation ~]$ tar -cf backup-$(date +%F).tar [student@worstation ~]$ ls backup* backup-2023-11-01.tar ``` Note: 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. --- # Protecting Arguments from Expansion ``` [student@workstation ~]$ echo "The language variable \$LANG sets the system language." The language variable $LANG sets the system language. ``` Note: A backslash (\) before a reserved character tells the shell to treat the character as an ordinary character. --- # Graded Quiz ## Match File Names with Shell Expansions ![clipboard](../images/clipboard-check.png) Note: After reading this section in the textbook complete the quiz in Canvas. --- # Key Takeaways 1. Files on a Linux system are organized in a tree-like, hierarchical format. 1. An absolute path always starts with a forward slash (/) and defines the exact location of a file on the system. 1. A relative path does not start with a forward slash (/) and defines the location of a file only in relation to the current directory. 1. The dot (.), the double dot (..), and the tilde (~) can be used in commands to define locations on the system. 1. Hard and soft or symbolic links can be used to create multiple pointers to data on disk or file names. 1. Bash pattern matching can be used to run commands more efficiently. Note: --- # Resources - [cp(1)](https://www.man7.org/linux/man-pages/man1/cp.1.html) - [mkdir(1)](https://www.man7.org/linux/man-pages/man1/mkdir.1.html) - [mv(1)](https://www.man7.org/linux/man-pages/man1/mv.1.html) - [rm(1)](https://www.man7.org/linux/man-pages/man1/rm.1.html) - [rmdir(1)](https://www.man7.org/linux/man-pages/man1/rmdir.1.html) - [ln(1)](https://www.man7.org/linux/man-pages/man1/ln.1.html) - [rmdir](https://youtu.be/6Gb_gAf17sw) (video) - [tree](https://youtu.be/Tw5OGULwqrE) (video) Note: Here are some resources to help you with this module. --- # Graded Lab ## Manage Files from the Command Line ![Screenshot of a sample grading script](../images/lab_grade_example.png) Note: In this lab, you efficiently create, move, and remove files and directories by using the shell and various file name matching techniques. When you complete the lab submit a screenshot of the output of the `lab grade files-review` command. The screenshot shown here is for reference only. --- ![Mesa Community College logo](../images/mcc_logo.png "Mesa Community College")