| Terminology |
Definition |
| Distro's |
One of several fully functional operating systems and members of the Linux family that run on the Linux kernel, GNU software, and additional components |
| File System Hierarchy Standard (FHS) |
A set of guidelines for the names of files and directories and their locations on Linux systems |
| Path |
A Reference to a specific location on a file system |
| Absolute Path |
A reference to a specific location on a file system irrespective of the current working directory or combined paths |
| Relative Path |
A Reference to a specific location on a file system that is relative to the current working directory |
| Current Working Directory |
The Directory that the user is currently, accessing |
| inodes |
An object that stores metadata about a file or directory on a file system. |
| ext2 |
A non-journaled Linux Filesystem introduced in 1993, and now the filesystem on some flash storage media |
| ext3 |
Standard Linux file system that includes journaling and has since been replaced with ext4 |
| ext4 |
One of the default file systems in modern Linux versions that supports journaling and large volumes |
| XFS |
One of the default file systems in modern Linux versions that supports journaling and efficiency in handling large files |
| Hard Link |
A reference to a file that enables a file's data to have multiple names on the same file system. |
| Symbolic Line |
A reference to a file or directory that can span multiple file systems |
| Present Working Direcotry |
The directory that the user is currently accessing |
Even though there are so many different Linux distros, they all usually share a common file structure. Such are the /etc, /home, /var/log, and more.
¶ Standard Subdirectories
- /bin Stores essential command-line utilities and binaries
- /boot Stores the files necessary to boot the Linux operating system
- /dev Stores hardware and software device drivers
- /etc Stores basic configuration files
- /home Stores users' home directories,
- /lib Stores shared program libraries required by the kernel, command-line utilities, and binaries
- /media Stores mount points for removable media
- /mnt Stores mount points for temporarily mounting file systems from remote file servers
- /opt Stores optional files of large software packages
- /proc Virtual File System (VFS) that represents continually updated kernel information to the user in a typical file format
- /root The Root Users directory
- /sbin Stores binaries used for completing the boot process and executables used by the root user
- /sys Another VFS that stores information about devices
- /tmp Stores temporary files that may be lost on system shutdown
- /usr A read-only directory that stores user programs and files accessible to all users
- /usr/bin Includes executable programs that all users can run
- /usr/local Includes custom-built applications
- /usr/lib Includes object libraries and internal binaries that are needed by executable programs
- /usr/lib64 serves the same purpose as /usr/lib but for 64-bit systems
- /usr/share Includes read-only architecture-independent files
- /var Stores variable files that are expected to constantly change; the system runs
When you examine files, it is usually important to review the file's metadata, such as permissions, using stat and file.
- The stat command displays file metadata such as file size, access information, and storage data in a relatively user-friendly structure.
- The file command displays different metadata, specifically, it examines the file type to provide information
- A file name is a string of characters that identifies a file.
- On an ext4 file system, a file may be up to 255 bytes long and contain any byte except NULL or the forward slash. They can also not be single. or two ..
- While Files can names may contain spaces, it is a standard convention for words to be separated with - or _, as they are easier to manage in the command line.
¶ Absolute and Relative Paths
A path defines the series of directories to be traversed to find a specific file or subdirectory
The Absolute Path defines all directories to traverse, starting from the root of the filesystem. The Files system root is depicted using the forward slash character
- You can use the ~ character to notate the path of your home directory
The Relative Path Depends on the user's current location in the Filesystem. If you were calling the file /home/yourusr/Documents/test.txt and you were already in your home direcotry you could just call the file with Documents/test.txt
File links can be used for files that are commonly accessed, to provide the user with a much easier way to call the file.
File links rely on the inodes markers. An Index Node is an object that stores metadata about a file or directory on a file system.
- A unique integer called an inode number identifies each inode in a file system.
- Whenever the system or application tries to access a file, it calls an inode table to search for a specific inode number to call the file
- The inode table maps an inode number to its corresponding file or directory name
- Some file systems set a maximum number of inodes when that file system is created. Though file systems like XFS use a dynamic inode allocation system that scales as a percentage of the file system's capacity
- You can use ls -i to list the inode numbers of files and directories
- Some of the common options for the ln command are:
- --backup
- -f Removes existing destination files
- -s Makes a symbolic link instead of hard links
- -i Prompts the user to remove destination files
- -v Prints the name of a file before linking
- A hard link is a reference to another File. Using a hard linka file can have more than one name in different locations of the same file system.
- A symbolic Link is a reference to a file that can span multiple file systems. If you delete the original file or directory after you create a symbolic link, the link is broken.
# Create a Hard Link
ln /usr/share/wordlist/rockyou.txt /home/jsmith/rockyoulink.txt
# Create a Symbolic Link
ln -s /usr/share/wordlist/rockyou.txt /home/jsmith/rockyoulink.txt
¶ Search Commands
Even though things are pretty well organized in Linux due to the FHS, users have plenty of file management options like mkdir, mv, grep, find, locate, and which
¶ The Find Command
- find searches the filesystem for files that match the given parameters. Some of the common parameters for the find command are
- -name searches by file name
- -type f searches by resource type file
- -type d searches by resource type directory
- -perm searches by permissions level
¶ The locate and updateb command
-
The locate command can also search for files and directories along a specified path, though the locate command relies on an index database for the search
-
The updateb command builds and updates the index database of files for the locate command
¶ The which command
- The which command displays the complete path to a specified command by searching the directories assigned to the PATH variable
- The which command can be useful for locating where a program is installed. It can also help to identify which version of a command you are using.
¶ File Manipulation Commands
- You can use Echo to display a line of text on the terminal, or to write text to a file
- The printf command is similar to echo but provides a way for the user to have more control of how the output is formatted, adding things like \n for new line
- The tr command translates a string of characters. It is predominantly used to change the case of letters in a file
- The wc command counts the number of words, lines, and characters in a textfile; it also has some options, such as:
- -c Displays the byte count
- -m Displays the character count
- -l Displays the newline count
- -w Displays the word count
- The sort command arranges the lines within a file. Some of its options are:
- -l{column numbers} Specifies field values
- -n Compares and sorts lines based on string numerical value
- -r sorts fields in descending order
- -t{delimiter} seperates one field from another
- The cut command extracts specified lines of text from a file, and some of its options are:
- -c specifies the number of characters to cut from each line
- -d{delemiter} Seperates one field from another
- -f{field numbers} Specifies the number to cut
- -s suppresses a line if the delimiter is not found
- The paste command can merge lines from a text file horizontally
- The diff command compares text files. It displays two text files and the differences between them. Here are some of its options:
- -b ignores spacing differences
- -i ignores case differences
- -t expands tab characters in output lines
- -w ignores spacing and tabs
- -c Displays a list of differences with three lines of context
- -u Outputs the results in unified mode.
- The sdiff is functionally the same as the diff command, but displays the content in a more user-friendly way
- The uniq is used to filter out duplicate adjacent lines from a file; here are some of its options:
- -c Display the number of occurrences on a line
- -d Only print duplicate lines
- -u Only prints unique lines
- -i ignores case when comparing lines
- awk performs pattern matching on files, and is based on the AWK programming language
- The sed command ot the stream editor is a program that can modify text files according to various parameters, some of its options are:
- d Deletes the lines that match a specific pattern or line number
- -n,p Prints only the lines that contain the pattern
- s Substitutes the first occurrence of the string in the file
- s,g Globally substitutes the original string with the replacement string for each occurrence.
¶ Apply File Management Commands
One of the most important things about Directory Navigation is understanding where you currently are, and you can achieve this with the pwd command.
-
Some Linux distros are configured to display the name of the current directory as a part of the command prompt, although this is not the absolute path, it can be helpful for understanding where you are in the Linux filesystem
-
Understanding the file tree can be difficult in the command prompt, but you can use the tree command to make it more familiar. Some of the options for the tree command are:
- -a Displays all files, including hidden ones
- -d Displays directories only
- -s Displays file sizes
- -D Displays the date of the last modification
- The cd command changes your present working directory to another directory
- Absolute and relative paths clearly outline how to travel between directories
- Over the years, shortcuts have been created to make directory navigation easier, such as:
- to signify the present working directory
- .. signifies the parent directory a
- ~ Signifies the home directory of the current user
- The best way to identify your existing directories is to use the ls comamnd, the ls command list the present working directory or another if you specify the path. Here are some of the common options for the ls command:
- -a lists all directory contents, including hidden resources
- -l lists all contents in a long format, useful for displaying permissions
- -d limits the output to directories
- -R lists the contents of the subdirectories
- -s Displays the file sizes
- -al combines -a and -l
- You can make a directory with mkdir, and you can make files within these directories with touch.
¶ File relocation and Display
- You can use the cp command to duplicate or copy files; it has the syntax of cp sourcefile newfile
- Sometimes you might have put a file in the wrong directory, in which case using the mv command would be more helpful. It has a syntax if mv sourcelocation destenationlocation
- You can display files with the cat command (which is short for concatenate), but it is usually used to display the contents of a single file.
¶ Directory and File removal
There is no concept of a trash can in CLI; do not use these commands unless you are sure that you need to delete that file.
-
The rm command is used to delete files. Some of the common options for the rm command are:
- -f Instructs the system to never prompt the user for confirmation
- -i Sets the interactive mode and prompts the user for confirmation before deleting
- -R Recursively removes a non-empty directory and its contents
-
rm does not remove directories. To remove a directory, you need to use the rmdir command.
¶ File Content Display Commands
¶ The less and more commands
If you use cat to display the contents of a file, then there is a possibility that not all of the contents will fit on the page. You can get around this by using the less and more commands
-
The less commandcan be used in two different ways, to display a file and to organize the output of other commands better. To use it for a command, you can do ls /etc | less, which will make the output of listing the /etc directory better
-
The more command is similar to less, but while less lets you page up and down more only lets you page down
¶ The head and tail commands
- These commands allow you to see either the top of a file or the end of a file, and you can use the -n option to specify the number of lines you would like to see at the beginning or the end of a file.
¶ String Display and searching
¶ The grep command
While the head and tail commands allow you to see a particular part of a file, sometimes you need something more granular than this, and this is where the grep command comes in.
- The grep command is great for parsing logs or to see if a file contains a particular string
- grep is case sensititve you can use the -i option to make the search case insenstitve
- You can use the grep command with other commands, much like you can with more or less. This is helpful for when a command is expected to output a lot of information, but you are only interested in the output that meets specific criteria
-
A text stream is a sequence of one or more lines of text that applications can use to read from or write to a particular device or system component. Most Linux shells have three stream types,
- Standard input (stdin) - A text stream that acts as the source for command input.
- Standard Output (stdout) - A text stream that acts as the destination for command output
- Standard Error (stderr) - A text stream that is used as the destination for error messages
-
Redirection is the process of accepting input data from a source other than the keyboard and sending output data to a destination other then display device.
- Input Redirection < - Reads the input from a file rather than from the keyboard or mouse
- Output Redirection> Redirects the standard output of a command to a file ( This will overwrite the file if it already exists )
- Output Redirection >> Appends the standard output of a command to the end of a file
- Error Redirection 2> Send error messages to a file, overwriting the file
- Error Redirection 2>> Appends the standard error message to the end of a file
- Combine Standard Output and Error &> - Redirects both the standard output and the standard error message to a file, overwriting the file
- Pipe | Send the output of one command to another command
- Here Document <<{string} Provides input data from the current source, stopping when a line containing the supplied string occurs
¶ Command Modifiers
¶ Background a command
- Using a & causes a command to execute in the background. This way, instead of taking up the shell you are using when you run the command, it will execute without taking up the shell
¶ Chain commands- Instead of just running one command at a time, you can run multiple
- You can use a | command modifier to take the output of one command and put it into another\
- You can use the; command modifier to run one command, then run the other
- You can use the && command modifier to run a command after another command has been successful.- You can use the || command modifier to only run a command if the first command fails
- You can use the! command modifier to negate the expression
¶ The tee command
- tee reads the standard input and sends the output to the default output device and copies the output to a specified file
¶ The xargs command
- The xargs command reads from standard input and executes a command for each argument provided. With the syntax of command {options} {arguments} | xargs {options} {command} Some of the options for the xargs command are:
- -I {Replacement String} Considers each line in the standard input as a single argument
- -L {number of lines} Reads a specified number of lines from the standard input and concatenates them together
- -p prompts the user before each command - -n {number of arguments} Read the maximum number of arguments from the standard input, and insert them at the end of a command template
- -E {End of String} Represents the end of the standard input
- -t write each command to the standard error output before executing the command
- -s {maximum Size} Set the maximum allowable size of an argument list to a specified number of characters.