Last modified: July 24, 2023
This article is written in: 🇺🇸
One of the fundamental skills is to navigate and manage files and directories effectively. Here, we focus on the crucial concepts that will facilitate your work within the file system.
TODO: - rsync
The two main types of paths are Absolute Path and Relative Path.
An absolute path specifies the complete location of a file or directory from the root directory, indicated by a forward slash (/
). It provides the full pathway from the root to the target file or directory, making it independent of the current working directory. As a result, absolute paths remain consistent regardless of the user's current position within the file system.
Example of Absolute Path:
/home/user/notes/file_name.txt
This path directs to the file file_name.txt
located in the notes
directory, which is under the user
directory within the home
directory, starting from the root of the file system (/
).
A relative path describes the location of a file or directory based on the current working directory. Unlike absolute paths, relative paths do not start from the root. Instead, they specify the path from the user's current location in the file system.
Example of Relative Path:
notes/file_name.txt
This path points to the file file_name.txt
located in the notes
directory under the current working directory.
Key Differences:
Linux provides several commands to navigate through the file system and manipulate files and directories. Here are a few fundamental commands:
pwd
)The pwd
command displays the absolute path of the current directory.
pwd
The cd command allows you to change your current working directory.
To move to a different directory, specify its path:
cd /path/to/directory
To navigate to your home directory, use:
cd ~
The ls command lists the contents of a directory.
To view the directory's contents, use:
ls
For a more detailed listing, including hidden files and additional information (file permissions, the number of links, the owner, the size, and the last modification date), use:
ls -al
To display specific file groups using wildcards, for example, to list all .txt files in the /home/mydirectory directory, use:
ls /home/mydirectory/*.txt
Working with files and directories is a key aspect of Linux. Various commands facilitate the creation, manipulation, and inspection of these resources.
In Linux, you can use the touch
command to create an empty file. For example, to create a new file named sample.txt
, you would type:
touch sample.txt
Creating a new directory involves the mkdir command. To create a directory called example_dir, you would run:
mkdir example_dir
The cp command is employed to copy files and directories from one location to another. For instance, to copy a file named file.txt to a directory named directory, you would use:
cp file.txt directory/
If you need to copy a directory and its contents, the -r (recursive) option is crucial:
cp -r source_dir destination_dir
There are several options that can modify the behavior of cp:
-a
option, also known as the archive option, preserves file attributes and symbolic links within the copied directories.-v
option, known as the verbose option, provides detailed output of the operation.The mv command helps with moving or renaming files and directories. To rename a file from oldname.txt to newname.txt, you would execute:
mv oldname.txt newname.txt
The same mv command helps you move a file from one directory to another. For instance, to move file.txt from the current directory to another directory called dir1, you would use:
mv file.txt dir1/
Files and directories can be removed with the rm command. To remove a file named file.txt, you would type:
rm file.txt
To remove an entire directory and its contents, you need to include the -r (recursive) option:
rm -r directory_name
Warning: The rm command is powerful and potentially destructive, especially when used with the -r (recursive) and -f (force) options. Use it with caution.
There are various ways to view and inspect the contents of files in Unix-like operating systems. We can use the cat
, more
, less
, head
, and tail
commands to achieve this.
cat
The cat
(concatenate) command is a standard tool used to display the entire contents of a file. It writes the contents of a file to standard output (the terminal). For instance, to display the content of a file named file.txt
, use the following command:
cat file.txt
However, keep in mind that cat is not ideal for large files because it dumps all content to the terminal at once.
For more manageable file viewing, particularly for larger files, the more and less commands are useful. They display content page by page, making it easier to digest.
The more command shows the content of a file, pausing after each screenful:
more file.txt
On the other hand, the less command, which is a more advanced and flexible version of more, allows both forward and backward navigation through the file:
less file.txt
While viewing files with more or less, you can use these commands:
Enter
command moves forward by one line.Space
command moves forward by one page.b
command moves one page backward, applicable in the "less" pager only.q
command quits the pager, usable in both "more" and "less" pagers./pattern
command searches for the next occurrence of the specified text "pattern," available in the "less" pager only.The head and tail commands are designed to output the beginning and the end of files, respectively.
The head command outputs the first part of files. It writes the first ten lines of each file to standard output. If more than one file is specified, it precedes each set of output with a header identifying the file. For instance, to display the first ten lines of file.txt, use:
head file.txt
Conversely, the tail command outputs the last part of files. It writes the last ten lines of each file to standard output. If more than one file is specified, it precedes each set of output with a header identifying the file. For instance, to display the last ten lines of file.txt, use:
tail file.txt
The number of lines can be adjusted using the -n option followed by the desired number of lines. For example, to view the last 20 lines of file.txt, you would use:
tail -n 20 file.txt
Brace expansion and globs are powerful tools in Linux for dealing with filenames that conform to certain patterns or templates. They are conceptually different and serve different purposes, but both can be used to save time and effort when working with files.
Brace expansion is a powerful feature in Unix-like shells that allows you to generate a series of strings from a pattern. This feature can be particularly useful for creating sequences of commands or filenames efficiently. Brace expansion uses a list of comma-separated values enclosed in curly braces {}
, which can be prefixed or suffixed with additional text.
The following command demonstrates a simple use of brace expansion:
echo a{b,c}d
This command will produce the output:
abd acd
Here's how it works:
{b,c}
is expanded into b
and c
.a
and the suffix d
, producing abd
and acd
.Brace expansion can be used to generate multiple strings from a single pattern, which can be particularly useful for batch operations. For example, to create a series of files with a common base name but varying in both number and a secondary identifier, you can use:
touch file{1..4}{a..f}
This command will create 24 files, named from file1a
through file4f
. The {1..4}
range generates numbers 1 through 4, while {a..f}
generates letters from a
to f
. The touch
command then creates a file for each combination.
Brace expansion can also handle nested patterns, allowing for complex combinations. For instance:
echo {A,B{1..3},C}
This command will expand to:
A B1 B2 B3 C
Here, the inner brace {1..3}
is expanded first, resulting in B1
, B2
, and B3
. The final list includes the strings A
, B1
, B2
, B3
, and C
.
Globs are pattern matching tools used in Unix-like systems to match filenames or directories based on wildcard characters. Unlike brace expansion, which generates new strings, globs are used to find existing files and directories that match a specific pattern. This feature is particularly useful for performing operations on multiple files with similar names or extensions.
*
(Asterisk): Matches any number of characters, including none. For example, *.txt
matches all files with a .txt
extension in the current directory.?
(Question Mark): Matches exactly one character. For example, file?.txt
matches file1.txt
but not file12.txt
.[...]
(Square Brackets): Matches any one character within the brackets. For example, file[12].txt
matches file1.txt
and file2.txt
but not file3.txt
.I. Listing Files:
ls *.txt
This command lists all files with a .txt
extension in the current directory.
II. Copying Files:
cp image?.png /backup/
This command copies files like image1.png
, image2.png
, etc., to the /backup/
directory.
While globs use wildcard characters to match patterns, they differ from regular expressions (regex) in syntax and behavior. Understanding these differences is crucial for using each tool appropriately.
Wildcard | Description in Globs | Description in Regex |
* |
Matches any number of characters | Matches zero or more of the preceding element |
? |
Matches exactly one character | Makes the preceding element optional |
. |
Matches the dot character literally | Matches any single character except newline |
*
can match any number of characters, while ?
matches exactly one character. For example, file*
matches file
, filename
, file123
, etc.*
matches zero or more occurrences of the preceding element, and ?
makes the preceding element optional. For example, file.*
matches file
followed by any character sequence, and colou?r
matches both color
and colour
./
), /var/log
, and your desktop using the cd
command. Try employing both relative and absolute paths. Explain the differences between these two types of paths.ls
command to enumerate the files in your current directory. Try using different options to reveal hidden files, sort files by their modification dates, and list files in a long detailed format. Discuss the output in each case.more
, less
, and cat
. Discuss the differences between the outputs of these commands.touch
command. Use the ls
command and redirect its output into each of these files. Then, display the contents of the files on the terminal.temp
directory to your home directory. Explain the options you used with the cp
command to accomplish this.>
operator. Now, move this temporary directory to a new location. Discuss the steps you took to complete this task.