Upon completion of this lesson, you will be able to:
Welcome to the world of Linux! If you’re new to this operating system, one of the first things you’ll notice is the power of the command-line interface, commonly referred to as the shell. This tutorial will introduce you to the basics of the Linux shell, including navigating directories, manipulating files, and executing commands.
Most of the commands introduced in this tutorial also apply to MacOS ‘terminal’.
The shell is a command-line interface that allows users to interact directly with the operating system. While Linux also has graphical user interfaces (GUIs), the shell provides a more direct way to manage files, run programs, and configure the system.
Accessing the Terminal: To start using the shell, open the Terminal application. This is where you’ll enter all your commands.
Prompt: Once you open the terminal, you’ll see something like:
[username@hostname ~]$
This is called the shell prompt. It’s waiting for your command!
mkdir (Make Directory): Create a new directory.
$ mkdir new_directory_name
rmdir (Remove Directory): Delete an empty directory.
$ rmdir directory_name
touch: Create a new empty file.
$ touch new_file.txt
rm (Remove): Delete a file.
$ rm file_name.txt
cat (Concatenate and Display): Display the contents of a file.
$ cat file_name.txt
less: View the content of a file page by page (press q
to exit).
$ less file_name.txt
grep (Global Regular Expression Print): Search for specific text within files.
$ grep "search_term" file_name.txt
Tab
to auto-complete file and directory names.Up Arrow
key to scroll through previously entered commands.clear
to clear the terminal screen..bashrc
is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. This means that when you start a new terminal or a new shell session, the commands and configurations in .bashrc
are executed to set up your environment.
.bashrc
Setting up PATH: You can modify the PATH
environment variable in .bashrc
to add new directories that contain executables.
Aliases: You can define shortcuts for commands using aliases. For example:
Now, whenever you type ll
in the terminal, it will execute ls -lah
.
Functions: You can define shell functions for more complex operations.
Prompt Customization: You can customize how your shell prompt looks.
This would make your prompt look something like username@hostname:current_directory$
.
Environment Variables: You can set or modify environment variables.
Initialization: Run any commands you want to execute whenever a new shell session starts.
.bashrc
Editing: You can open .bashrc
in your favorite text editor (like nano
or vim
) and add or modify commands.
Reloading: After making changes to .bashrc
, you need to reload it to see the effects in the current shell session.
Alternatively, you can use:
Backup: It’s a good idea to make a backup of your .bashrc
before making significant changes.
.bashrc
vs .bash_profile
While .bashrc
is executed for interactive non-login shells, .bash_profile
(or .profile
in some systems) is executed for login shells. When you log into a system (e.g., through SSH) or when you start a new shell session from a graphical login, .bash_profile
is executed.
Typically, for users who have both files, you’ll see that .bash_profile
sources .bashrc
to ensure that the interactive configurations are also loaded during a login shell:
This setup ensures consistent behavior between login and non-login interactive shell sessions.
grep
grep
and fgrep
are both commands used for searching text, but they interpret patterns differently. Here’s a breakdown of their differences:
Pattern Interpretation:
grep
: Stands for “global regular expression print.” It searches for a pattern using regular expressions (regex) by default. So, if you provide a pattern with special regex characters, such as .
(dot) or *
, grep
will interpret them as regex metacharacters. For instance, .
will match any character, and *
will match the preceding character zero or more times.
In the above command, the pattern “a.b” will not match “ab” because the dot expects any character to be present between “a” and “b”.
fgrep
: Stands for “fixed grep” or “fast grep.” It treats the pattern as a fixed string. This means it does not recognize regular expression metacharacters as special; they are treated as regular characters. Thus, fgrep
is useful when you want to search for a string that might contain regex metacharacters and you don’t want them to be interpreted as such.
Here, the pattern “a.b” matches the string “a.b” because fgrep
doesn’t interpret the dot as a regex character.
Speed:
fgrep
can be faster than grep
when searching for fixed strings because it doesn’t need to process regex metacharacters.Alternatives:
You can achieve fgrep
behavior with grep
using the -F
option:
Multiple Patterns:
One advantage of fgrep
(or grep -F
) is the ability to search for multiple fixed strings efficiently by placing each string on a new line in a pattern file.
In the above command, each line in pattern_file.txt
is treated as a fixed string, and fgrep
searches for any of these lines in target_file.txt
.
In short, use grep
when you need the power and flexibility of regular expressions. Opt for fgrep
(or grep -F
) when you want to search for exact, fixed strings, especially when these strings might contain characters that have special meanings in regex.
grep
is a powerful tool for searching text patterns within files. It stands for “global regular expression print.” Below, I’ll expand on how to use grep
effectively with various options and examples.
+
, ?
, |
, {}
, and ()
without backslashes). Often available as egrep
.Or using extended regular expressions:
grep
installations highlight the matched text. If yours doesn’t, you can use the --color
option.In summary, grep
is a versatile and powerful tool that can be used to search through text data quickly. Mastering grep
can considerably speed up text-processing tasks and help in debugging, analyzing logs, or finding specific content in large datasets.
Absolutely! Regular expressions (often abbreviated as “regex” or “regexp”) are sequences of characters that define a search pattern. They can be used for string matching and replacement. Let’s delve into the details:
regex hello
.
):
.
character matches any single character except a newline. regex h.llo
[]
):
[]
) makes up a character class. This matches any single character in the set. regex [aeiou] # matches any single vowel
[^ ]
):
^
symbol inside the square brackets, only characters not in the character class will be matched. regex [^aeiou] # matches any single non-vowel character
\d
: Matches any digit. Equivalent to [0-9]
.\D
: Matches any non-digit. Equivalent to [^0-9]
.\w
: Matches any word character (alphanumeric and underscore). Equivalent to [a-zA-Z0-9_]
.\W
: Matches any non-word character.\s
: Matches any whitespace character (spaces, tabs, line breaks).\S
: Matches any non-whitespace character.*
: Matches 0 or more of the preceding element. regex ab*c # matches "ac", "abc", "abbc", etc.
+
: Matches 1 or more of the preceding element. regex ab+c # matches "abc", "abbc", but not "ac"
?
: Matches 0 or 1 of the preceding element. regex ab?c # matches "ac" or "abc"
{n}
: Matches exactly n
occurrences of the preceding element.
{n,}
: Matches n
or more occurrences.
{n,m}
: Matches between n
and m
occurrences. regex a{2,4} # matches "aa", "aaa", or "aaaa"
^
: Matches the start of a line.$
: Matches the end of a line. regex ^Hello # matches "Hello" at the beginning of a line world$ # matches "world" at the end of a line
|
):
regex cat|dog # matches "cat" or "dog"
()
):
regex (abc)+ # matches "abc", "abcabc", etc.
\1
refers to the text of the first capture group, \2
for the second, and so on. regex (a)b\1 # matches "aba"
\
. regex \. # matches a literal dot \\ # matches a backslash \[ # matches a square bracket
Lookaheads allow you to match a group ahead of your main pattern without including it in the result. regex q(?=u) # matches the "q" in "queue" but not the "u"
Lookbehinds are similar, but they look behind. regex (?<=a)b # matches the "b" after an "a"
(?:...)
to create a non-capturing group. regex (?:abc)+ # matches "abc", "abcabc", etc., but does not capture the match
regex (?i)abc # matches "abc", "ABC", "AbC", etc. (case-insensitive)
Regular expressions are powerful but can be complex. Mastering them requires practice. Test your regular expressions using tools like regex101 to ensure they match what you intend and to understand their behavior better.
Navigating a system from the command line is a key skill that all programmers and software developers should possess. This tutorial covered just the basics of the Linux shell, but there’s a vast world of commands and functions to explore. As you become more familiar with the shell, you’ll find it’s an incredibly powerful tool that can simplify many tasks and offer fine-grained control over your Linux system.
Keep practicing, explore further, and soon, you’ll master the Linux shell!
None.