Linux - Managing Files from the Command Line | Mostafa Mahmoud | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Linux - Managing Files from the Command Line

teacher avatar Mostafa Mahmoud, Data Scientist/ML Engineer/Linux Expert

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

5 Lessons (42m)
    • 1. 00 Class Two Overview

      0:56
    • 2. 01 The Linux File System Hierarchy

      6:36
    • 3. 02 Managing Files Using Command Line Tools

      17:10
    • 4. 03 Matching File Names Using Path Name Expansion

      11:44
    • 5. Exercise 3

      5:20
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

24

Students

--

Projects

About This Class

RHEL 8 / CentOS 8 Linux Administration - RHCSA 8 - Class Two

Linux - Managing Files from the Command Line

Hi, I'm Mustafa Mahmoud. A Senior Linux Administrator and Online Instructor. I have been working as Linux System Administrator for more than ten years, currently devoted to teaching. I like to share my knowledge with others and help them advance in their careers.

Students testimonials - See what others say!

  • Siddharth Kumar: I really loved the course content and the way all details have been explained by the trainer, it will certainly help me or anyone else to improve their Linux administration skills.
  • Eric Voigt: Excellent overview of the basic skills, well organized and taught.
  • Suman Mandal: This course was useful to me. I have learned many things that were not clear to me. Thank you.

What you should know before starting

In this class you will learn:

  • The Linux file system hierarchy.

  • The Linux file system tree-like structure.

  • The most important directories on the Linux system.

  • Command-line file management.

  • Creating, deleting, copying, and moving files and directories.

  • The touch command.

  • More options for cd and ls commands.

  • Matching File Names Using Path Name Expansion 'Globbing and Wildcards'.

  • The common meta-characters and pattern classes.

  • Simple pattern matches using ( ? ).

  • Tilde expansion.

  • Brace expansion.

  • Command substitution.

  • Protecting arguments from expansion.

What's next?

RHEL 8 / CentOS 8 Linux System Administration - RHCSA 8 - Class Three

Meet Your Teacher

Teacher Profile Image

Mostafa Mahmoud

Data Scientist/ML Engineer/Linux Expert

Teacher

Hello, I'm Mostafa. A data scientist, ml engineer, and Linux expert. I worked for ten years as a Linux systems administrator at Express, then I had the opportunity to turn to data science. Because of my passion for this field and my keen attention to detail, I got my Udacity certifications to work as a data scientist and machine learning engineer. The most recent projects I worked on were Finding Donors for CharityML, a full exploratory and explanatory analytics work project for Ford Go Bike company trips data, and creating a logistic regression to predict absenteeism. I'm working on improving my skills and looking for job opportunities that will help me in this direction.

Skills: Python, SQL, Linux
Applications: Jupyter Notebook, Weka, Excel, Pycharm,... See full profile

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 00 Class Two Overview: In this class you will learn managing files from the command line, which will include, the Linux file system hierarchy. The most important directories on the Linux system, the command-line file management, creating, deleting, copying, and moving files and directories. The touch command, more options for cd and Ls commands. Matching file names using pathname expansion. The common metacharacters and pattern classes. Simple pattern matches using question-mark, tilde expansion, brace expansion, commands substitution, predicting arguments from expansion. 2. 01 The Linux File System Hierarchy: The Linux file system hierarchy A simple description of the Linux system is that, on a Linux system everything is a file if something is not a file it is a process. A Linux system makes no difference between a file and a directory since a directory is just a file containing names of other files. Programs, services, texts, images and so forth are all files. Input and output devices and generally all devices are considered to be files according to the system. In order to manage all those files in an orderly fashion, we will think of them in an orderly tree like structure on the hard disk. All files on a Linux system are stored on file systems which are organized into a single inverted tree of directories known as a file system hierarchy. This tree is inverted because the root of the tree is said to be at the top of the hierarchy and the branches of the directories and subdirectories stretch below the root. The directory forward slash is the root directory at the top of the file system hierarchy. The slash character is also used as a direct separator in filenames, for example, if etc is a subdirectory of the root directory, we could call that directory /etc Likewise, if the /etc directory contained a file named hosts, we could refer to that file as /etc/hosts. Subdirectories of the route directly are used for standardized purposes to organize files by type and purpose. This makes it easier to find files. For example, in the root directory, the Subdirectories /boot is used for storing files needed to boot the system. The following terms are encountered in describing filesystem directory contents. Static is content that remains unchanged until explicitly edited or reconfigured. Dynamic or variable, is content typically modified or appended by active processes. Persistent is content, particularly configuration settings that remain after a reboot. Runtime is a process or system-specific content or attributes cleared during reboot. The most important directories on the Linux system. User directory. /usr directory for installed software, shared libraries, include files, and static read-only program data. /usr important sub directories include, bin directory for user commands sbin directory for system administration commands, And local directory for locally customized software. /etc directory for configuration files specific to this system. /var directory for variable data specific to this system that should persist between boots. Files that dynamically change. For example, databases, cache directories, log files, printer-spooled documents, and website content, may be found under /var. /run directory, for runtime data for processes, started since the last boot. This includes process id files and log files among other things. The contents of this directory are recreated on reboot. /home directory for home directories where regular users store their personal data and configuration files. /root directory for home directory for the administrative superuser, root. /tmp directory is a world-writable space for temporary files. Files which are more than 10 days old are deleted from this directory automatically. Another temporary directory exists is /var/tmp in which files that haven't been accessed, changed, or modified in more than 30 days are deleted automatically. /boot directory for files needed in order to start the boot process. /dev directory contains special device files that are used by the system to access hardware. /bin directory, /lib directory, /lib64 directory, and /sbin directory. In older versions of CentOS and Red Hat Enterprise Linux, these will distinct directories containing different sets of files, In CentOS 8 and Red Hat Enterprise Linux 8, These directories in the root directory are symbolic links to the matching directories in the /usr directory. I hope this has been informative for you and I would like to thank you for viewing. 3. 02 Managing Files Using Command Line Tools: Managing files using command-line tools Command-line file management File management involves creating, deleting, copying and moving files. Additionally, directories can be created, deleted, copied, and moved to help organize files logically. When working at the command line, file management requires awareness of the current working directory to choose either absolute or relative path syntax as most efficient for the immediate task. Creating files The touch command normally updates a file's timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files, which can be used for practice, since "touching" a file name that doesn't exist causes the file to be created. Here we are in the user mo home directory. Now for listing files in directories. In this directory, we will use the ls command followed by -l option for long listing format For our example. We will work on the text to file. To check the content of the text file, we will use the cat command. Now enter the command touch text to update the text file timestamp. Now to check the update. As you can see, the text file timestamp is updated to the current date and time. And to check the content. Here, as you can see, the text file timestamp is updated to the current date and time without otherwise modifying it. Also, you can use a text ed program like VIM or Nano to create a new file or modify the existing one, for example. nano fruits. The new file will open, you can add some data. Then save and exit. To check. And to show the data in the fruits file using the cat command. Although a space is an acceptable character in Linux filenames, a space is the delimiter used by the command shell for command syntax interpretation. New administrators are advised to avoid using spaces in filenames since file names that include spaces frequently result in undesired command execution behavior. The ls command has multiple options for displaying attributes on files. The most common and useful options are -a option for displaying all files, including hidden files. The two special directories at the top of the listing refer to the current directory with ( . ) and the parent directory with ( . . ). These special directories exist in every directory on the system. Their usefulness will become apparent when file management commands are practiced. File names beginning with a dot ( . ) indicate files hidden from normal view using ls and other commands. This is not a security feature. Hidden files keep necessary user configuration files from cluttering home directories. Many commands process hidden files only with specific command-line options, preventing one user's configuration from being accidentally copied to other directories or users. To protect file contents from improper viewing requires the use of file permissions which I will discuss later. The hidden directory ( . ) specifies the current directory on commands in which the current location is either the source or destination argument, avoiding the need to type out the directory's absolute path name. when using the cd double dot command it uses the double dot hidden directory to move up one level to the parent directory without needing to know the exact parent name. For example. Here we are in the user mo home directory By using the cd double dot command, we have moved up one level to the parent directory slash home. The -R recursive option to include the content of all subdirectories. Here, as you can see, the contents of the downloads directory and the contents of the pictures directory. -l option for long listing format By adding the –l option to ls command, it can list all the files, directories, and their mode, number of links, owner of the file, file size, modified date and time, and filename. Also you can use ll instead of ls –l as ls –l is aliased to ll. You can check using the ls command Creating directories The mkdir command creates one or more directories or subdirectories, generating errors if the file name already exists or when attempting to create a directory in a parent directory that doesn't exist. The -p parent option creates missing parent directories for the requested destination. Be cautious when using mkdir -p, since accidental spelling mistakes create unintended directories without generating error messages. For example. To create 2019 directory: mkdir 2019 To check. And to create jan directory as a subdirectory for the 2020 directory. mkdir -p 2020/jan The mkdir failed due to existing one of two reasons, first 2020 was misspelled or "2020" doesn't exist as a location in which to create the jan subdirectory, as in this case. Here, if a ‘- p’ were used, we would not have received an error message and we will have the two directorates, 20/20 and Jane created, for example. mkdir -p day by 2020/jan To check. Here, the - p parent option created the missing parent directory 2020.and the subdirectory jan. Copying files. The cp command copies one or more files to become new, independent files. The syntax allows copying an file to a new file in the current or another directory, or copying multiple files into another directory. In any destination, new file names must be unique. If the new file name isn't unique, the copy command will overwrite the existing file, for example. Here, I will work on the text file. To show the text file content. Now to copy the text file to the 2019 directory. To check. The copying has succeeded. And to check that the contents of the text file haven't been modified. Now if we modified the original text file using the nano program then save and exit. copy it again to the 2019 directory and check the data in the text file in the 2019 directory using the cat command. As you can see The old text file will be replaced with the new one because they are using the same name. When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. Conflicting file names that exist at a destination may be overwritten. To protect users from accidentally overwriting directories with contents, multiple file cp commands ignore directories specified as a source. Copying non-empty directories, with contents, requires the -r recursive option. For example. To copy the fruits file to the 2020 / jan directory. cp fruits 2020/jan To check. Now to copy the 2020/jan directory under the 2019 directory, I will use the dash Recursive Option. To check. As you can see, the jan directory and its contents have been covered under the 2019 directory. Moving files. The mv command renames files in the same directory, or relocates files to a new directory. File contents remain unchanged, files moved to a different file system required creating a new file by copying the source file, then deleting the source file. Although normally transparent to the user, large files may take noticeably longer time to move. For example. mv text To check. Here, I used the mv command to rename the text file to test file. And to use the mv command to relocate the test file into the Documents directory type. To check. Removing files and directories. Default Syntax for the rm command deletes files but not directories, deleting a directory and potentially many subdirectories and files below it requires the -r recursive option. There is no command line undelete feature nor a trash bin for which to restore, for example. To delete the file test in the Documents directory. rm Documents/test To check. And to delete the 2019 directory and the files below it, we will need to use the -r recursive option or we will get an error. As you can see, rm cannot remove 2019, 2019 is a directory. We will try again using the -r recursive option. To check. As you can see, after rm failed to delete the 2019 directory, the -r recursive option succeeded. The rm -r command parsed into each subdirectory first, individually deleting contained files before removing each directory. Note that, using the -i option will interactively prompt for each deletion. This is essentially the opposite of the -f option, which will force the deletion without prompting the user. The rmdir command deletes directories only if empty, Removed directories cannot be undeleted, for example. As you can see, the 2020 directory isn't empty. If we try the rmdir on it. Here rmdir command failed to delete non-empty 2020 Directory. If we created a new empty directory 2021 using the mkdir command. And try the rmdir command on it. To check. As you can see here, when deleting the empty 2021 directory, the rmdir command succeeded. I hope this has been informative for you, and I would like to thank you for viewing. 4. 03 Matching File Names Using Path Name Expansion: Matching file names using pathname expansion. File globbing. The Bash shell has a pathname-matching capability historically called globbing, abbreviated from the "global command" file pathname expansion program of early UNIX. The Bash globbing feature commonly called pattern matching or wildcards makes managing large numbers of files easier. Using metacharacters also referred to as wildcards that expand to match file pathnames being sought. Commands perform on a focused set of files at once. Pattern matching Globbing is a shell command-parsing operation that expands a wildcard pattern into a list of matching path names. Command-line meta-characters are replaced by the match list prior to command execution. Patterns, especially square-bracketed character classes, that do not return matches display the original pattern request as literal text. These are the common meta-characters and pattern classes. You can Take a look at them before starting the examples. A sample set of files is useful to demonstrate expansion. First, create a directory called "glob" using the mkdir command. "mkdir glob" and make it the current working directory using the "cd" command "cd glob". Now, I will create some files starting with different characters using the "touch" command. Now, let's try some examples, first example of simple pattern matches using an asterisk. An asterisk matches one or more occurrences of any character including no character. For example, "ls a*" "ls a*" This will show all files starts with the "a" character. Another example, this will show any file includes "a" in its name. And here, this will show any file starts with the letter "a" or letter "c". Example of simple pattern matches using Questionmark, Questionmark represents or matches a single occurrence of any character. For example. This command will show all files contains only three characters. And this will show all files contain only four characters in its name. Till the expansion. The tilde character when followed by a slash delimiter, matches the current user's home directory and when followed by a string of characters up to a slash, it will be interpreted as a username if one matches. If no username matches, then an actual Tilde followed by the string of characters will be returned. For example. Here Tilda represents the path to the current user's home directory "/home/mo" because it was followed by a slash delimiter. And here, Tilda represents the path to the current user's home directory "/home/mo" because it was followed by a string of characters matched the user name "mo" But here, an actual Tilde followed by the string glop appears because no username matches Tilde followed by plus sign expense to the value of the "PWD" variable which holds the current working directory. And Tilde followed by dash sign, expands to the value of the OLDPWD variable which holds the previous working directory. If OLDPWD is unset tilde "~-" dash is not expended. Brace expansion. Brace expansion is used to generate discretionary strings of characters. Braces contain a comma-separated list of strings or a sequence expression inside curly braces "{}". A sequence consists of a starting and ending item separated by two periods "..". The result includes the text preceding or following the brace definition. Brace expansions may be nested, one inside another. Let's take some examples. Common substitution. Command substitution allows the output of a command to replace the command itself. Command substitution occurs when a command is enclosed with a beginning dollar sign and parenthesis, or with backticks. The form with backticks is older and has two disadvantages. First, it can be easy to visually confuse backticks with single quote marks, and second backticks cannot be nested inside backticks. The $(command) form can nest multiple command expansions inside each other. For example. Here I used the %A for locale’s full weekday name. And Here I used the %T to show time as %H:%M:%S Protecting arguments from expansion. Many characters have special meaning in the Bash shell. To ignore meta-character special meanings, single quoting and escaping are used to protect them from shell expansion. The backslash (\) is an escape character in Bash, protecting the single following character from special interpretation. To protect longer character strings, single ( ' ) quote is used to enclose strings. Use double quotation marks to suppress globbing and shell expansion, but still allow command and variable substitution. Variable substitution is a concept usually identical to command substitution, but may use optional brace syntax. For example. Here using the ‘ single quote character prevented the $ character from interpretation, meaning that it prevented parameter expansion. Also here using the \ escape character prevented the $ character from interpretation meaning that it prevented parameter expansion. But here, using the double quotation allowed the $ character to interpret. And here using the double quote prevented pathname expansion “globbing”. I hope this has been informative for you and I would like to thank you for viewing. 5. Exercise 3: The root subdirectories using a long listing format option. Two new files, test1 and test2 in my home directory using the touch command. As I am already present in my home directory, I will directly create the two files to check the test1 file using the nano editor. Test1 file using the get command. This station diode one in my home country and want to check this one and this two files to three in one step to create a new directory there to under to tick. Then one directory under the D2 directory. Check to change their name to dare to slash dire 20-20. To check this 25 from there, 20-20 to heat are used to represent the current directory there to check this one pipe from tao2, 20-20 type. Check from a to Z using the echo command and the brace expansion username on the terminal using the equipment and the user variable. Thanks for viewing.