LEARN 2 LINUX
MODULE 3: FILES & PATHS
Presents:
1
JOIN OUR DISCORD SERVER
• https://discord.gg/Dt4PtWqRVh
• You will be assigned a L2L role upon joining
• This will ensure that you will be notified of updated information and messages in the “learn- 2-linux” channel
RECAP
• Went over the basic commands with demonstrations
• Explained the command structure (syntax)
• Special characters and their uses in Linux
• Keyboard shortcuts
• Ran through the bandit challenges on OverTheWire
3
LINUX ENVIRONMENT: FILE STRUCTURE
•In Linux, everything is a file (directories, files, and peripherals).
• And if it's not a file, it's a process
•Unlike Windows where files and directories are stored in drives (C: for example), Linux files are ordered in a tree structure starting with the root directory
LINUX ENVIRONMENT: FILE TYPES
•Using the command "ls –l" the very first character will outline the type of file
•Regular files: start with -
•Directories: start with d
•There are more types out there, but these are the most common found and for beginners
5
LINUX ENVIRONMENT: FILES
• General Files
• Can be images, videos, programs, or text
• Represented in either ASCII or Binary format
• Directory Files
• Same as folders in Windows
• Device Files
• In Windows there are devices like Printers, Drives, Peripherals, etc and are represented by a letter like C: or D:
• In Linux they are files (as mentioned before)
LINUX ENVIRONMENT: FILE EXTENSIONS
•Unlike Windows files, Linux files do not need an extension to define what file type they are (although you can add one for a reference if you wish to)
•Instead, files are defined by the file header (Hexadecimal values)
7
LINUX FILESYSTEM
/bin
Contains binaries, these are applications and programs you can run such as "ls" and other commands
/boot
This directory contains the files required for booting up your system DO NOT TOUCH these files, doing so can cause Linux not to run properly
/dev
This contains the device files, many of these files are generated at boot time or when a peripheral device is plugged in (external HDDs, USBs, etc.)
/etc
Contains system-wide configuration files, such as username and password lists
/home
Same as the "users" folder found in Windows, this contains the user's on the system's personal directories
/lib
Contains library files that contain code that applications use (similar to .dll files found in Windows)
/media
This is the location where external storage is mounted to when plugged into your system
/root
Home directory for root user
REFERENCE: https://www.linux.com/training-tutorials/linux-filesystem-explained/
8
LINUX ENVIRONMENT: FILES
•https://www.youtube.com/watch?v=42iQKuQodW4
SOME BASIC FILE/FOLDER COMMANDS
• touch – Used to create a blank file
• Here I have chained the ls command to check that the blank file “new_file” has been made
• rm – Deletes files
• Here I am chaining the ls command to confirm that “new_file” has been deleted
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
10
SOME BASIC FILE/FOLDER COMMANDS CONT
• echo – Used to echo output to the terminal. Can be used to redirect output to files
If we want to save our echoed output, we need to write it to a file. Notice if you plan on writing an echoed output to a file which doesn’t exist, you don’t need to use the touch command to make the file first. Touch command would be useful to create a blank file if something were to be written into it later.
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
SOME BASIC FILE/FOLDER COMMANDS CONT
• cat – Read the contents of a file
• file – Find the file type of a file (binary, text, image, etc.)
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
12
SOME BASIC FILE/FOLDER COMMANDS CONT
• mkdir – Creates an empty folder
• rmdir – Deletes folders but only if they are empty
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
SOME BASIC FILE/FOLDER COMMANDS CONT
• In the last slide I mentioned the “rmdir” command and how it can only delete folders that are empty
• But to delete folders that aren’t empty we can use the rm command with the arguments –rf
• -r means recursive and will remove directories and their files
• -f means it will remove read only files without being asked
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
14
SOME BASIC FILE/FOLDER COMMANDS CONT
• find –Used to locate a file
Here I am saying to start searching in the current directory (as noted by the “.”) and specifying the
“-name” argument to say I want to search by filename, next I am using a regular expression (the *) to say find me all file that start with L2L (and may have other characters after that).
So, this whole command translates to: Starting from current directory and searching by file name, find me all files that begin with “L2L”
As you can see the output (returned value) is the path to where the file is located
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
SOME BASIC FILE/FOLDER COMMANDS CONT
• tar –Compress a file into a compressed archive or extract files from a compressed archive
Here I have used the –c flag to say I want to compress (make archive) and the –f flag to specify a file name, the “.” is simply to say all files in the current directory. I get an error because the archive file has been created yet attempts to place it into an archive of the same name. ls to confirm archive has been made
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
16
SOME BASIC FILE/FOLDER COMMANDS CONT
Here I have created an empty folder “Files_from_Desktop” where I want to extract the files in the archive we created in the previous slide into this new folder. I use the –x flag to specify I want to extract from an archive and again the –f flag to say I want to specify the archive by file name.
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
PATHS
•In Linux systems a path is a unique location to a file or folder in the filesystem of the OS (Just like in Windows and MacOS)
•Linux has two types of paths: Absolute and Symbolic
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
18
ABSOLUTE PATHS
•This is where the path name is defined by specifying the location of a file or directory from the root directory (/).
•The absolute path is the complete path starting from the root directory
•Absolute paths begin with the /
Here I am in a directory that is used to store webpage content, the var directory is in the home directory just like our user’s directory (ubuntu). Notice how since the home directory is not in the html directory, I have to use an absolute path to get back to my Desktop directory
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
RELATIVE PATHS
•Relative paths are defined as the path related to the present working directory (which can be found by using the pwd command)
•Relative paths don't begin with the /
Here I started in the ubuntu directory (or whatever you user directory is called) and I have gone into the Desktop directory. Notice how Desktop directory is in the ubuntu directory, I can just use a relative path to get into the Desktop directory. The full path here is “/home/ubuntu/Desktop”
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
20
SUMMARY
•Went over the Linux file structure
•Explained how file types are displayed/defined
•Filesystem directories
•File/Folder commands
•Paths
REFERENCE: https://www.hostinger.com/tutorials/linux-commands
ACTIVITY TIME: LINUX JOURNEY
•Go to https://linuxjourney.com/
•Going to be covering over the Command Line section, but you are free to explore and try other activities.
22