Discovering the World of Linux: A Guide to the Command Line, Shell Scripts, and Customization

Teendifferent
13 min readMar 23, 2023

--

Learn to Navigate and Master the GNU Bash Shell, Access the Linux Console, and Explore the Endless Possibilities of Open Source Software.

Photo by Arnold Francisca

The Power of Shell Scripts

Welcome to the wonderful world of Linux, where the skies are blue, the grass is green, and the command line is king. Yes, that’s right, the command line. The thing that strikes fear into the hearts of many a new user, but has the power to make seasoned veterans feel like wizards. But fear not, dear reader, for the command line is not as scary as it may seem. In fact, it can be quite liberating. No more clicking through endless menus and submenus to find the one setting you need. With the command line, you can accomplish most tasks with a few simple keystrokes.

And let’s not forget the shell scripts. Ah, the shell scripts. The unsung heroes of the Linux world. These little bundles of joy can automate just about anything you can think of. Need to back up your files? There’s a script for that. Need to update your system? There’s a script for that too. Want to make a sandwich? Okay, maybe not that one, but you get the idea.😅

But let’s talk about getting to the shell. There are a few different ways to do this, but one of the easiest is to switch your system to text mode. This will give you a simple shell CLI on your monitor, just like the good old days. And who doesn’t love a good throwback? Now, some of you may be thinking, “But I don’t want to leave my fancy graphical desktop environment!” Fear not, my friend. You don’t have to give up your pretty icons and windows just to get to the command line. Most Linux distributions come with a terminal emulator that you can access from within your graphical desktop. It’s like having your cake and eating it too.

Ways to Access the Shell

How do you access the Linux console? Well, when your system starts up, it automatically creates several virtual consoles. These are essentially terminal sessions that run in system memory.

  1. Terminal Emulators The most common way to access the shell is through a terminal emulator. This is a program that provides a command-line interface within a graphical user interface (GUI). There are many terminal emulators available for various operating systems, including Windows, macOS, and Linux. Some popular terminal emulators include GNOME Terminal, iTerm2, and PuTTY.
  2. SSH If you want to access the shell on a remote machine, you can use SSH (Secure Shell). SSH is a network protocol that allows you to securely connect to another computer over an unsecured network. Once you have established an SSH connection, you can access the shell on the remote machine just as if you were sitting in front of it. To use SSH, you will need to have SSH client software installed on your computer, as well as access to a remote machine that is running an SSH server.
  3. Shell Scripts Shell scripts are text files that contain a series of commands that can be executed in the shell. You can create shell scripts using a text editor, and then execute them by running the script file in the shell. Shell scripts are a powerful way to automate tasks and perform complex operations in the shell.
  4. Virtual Consoles Virtual consoles are another way to access the shell. These are text-based interfaces that allow you to interact with your computer without a graphical user interface. You can access virtual consoles by pressing Ctrl+Alt+F1 through Ctrl+Alt+F6 on Linux systems, or Ctrl+Alt+F1 through Ctrl+Alt+F8 on macOS systems. Each virtual console runs a separate instance of the shell, so you can have multiple instances running at the same time.

Once you’ve accessed the Linux console, you’ll be greeted with a simple shell prompt. From here, you can enter any command that you would normally enter from within a terminal emulator. And if you’re feeling really fancy, you can even start writing your own shell scripts. Just be careful not to accidentally launch any missiles or anything.

The Different Types of Shells

But let’s talk about the different types of shells. There’s the ash shell, which is a simple, lightweight shell that’s great for low-memory environments. Then there’s the korn shell, which is compatible with the Bourne shell but also supports advanced programming features. And let’s not forget about the tcsh shell, which incorporates elements from the C programming language into shell scripts. And if you really want to go all out, there’s the zsh shell, which incorporates features from bash, tcsh, and korn, providing advanced programming features, shared history files, and themed prompts. It’s like the Swiss Army Knife of shells.

This article draws heavily from a book on cyber security and Linux. I strongly recommend this book to anyone interested in these topics, as it offers a fresh perspective and insightful knowledge. Additionally, I have included an affiliated link that will allow you to purchase the book easily and support me with a small tip at no extra cost to you. This arrangement benefits us both, and I encourage you to take advantage of it and explore this valuable resource. Let’s delve into the world of cyber security and Linux together.

Customization and Flexibility in Linux

And remember, Linux is all about customization and flexibility. So don’t be afraid to experiment and try new things. Want to create a custom prompt that displays the time and weather? Go for it. Want to create a script that automatically downloads and installs your favorite programs? Why not? The possibilities are endless, and that’s what makes Linux so great.

But let’s not get too carried away. We don’t want to turn into Linux fanatics and start preaching about the superiority of open-source to anyone who will listen. Or do we? Okay, maybe let’s just stick to spreading the word to those who are interested.

Navigating and Working with Directories in the Command Line Interface

The GNU bash shell is a program that provides interactive access to the Linux system, and with a little guidance, anyone can navigate and work with directories in the Command Line Interface (CLI).

First things first, if you’re lost and don’t know what to do, use the man command. This provides access to the manual pages stored on the Linux system. Just type “man” followed by the command you want to learn about. Can’t remember the command name? Use keywords with the “-k” parameter to search the man pages.

For example, to find commands dealing with terminals, type “man -k terminal”.

Now that you know how to find your way, let’s talk about traversing the filesystem. The Linux virtual directory structure contains a single base directory called the root, and directories and files beneath the root directory are listed based on the directory path used to get to them. To move to a specific location using an absolute directory reference, specify the full pathname in the cd command.

For example, “cd /usr/bin”. To display the shell session’s current directory location, use the pwd command.

Navigating the Filesystem

Using a relative directory reference allows you to specify a destination directory reference relative to your current location. A relative directory reference doesn’t start with a forward slash (/).

For example, “cd Documents”. The single dot (.) represents the current directory, and the double dot (..) represents the parent directory. Use the double dot when trying to traverse a directory hierarchy. For example, “cd ../Downloads”.

Now that you know how to navigate, let’s talk about listing files and directories. The ls command displays the files and directories located in your current directory. The most basic form of the ls command is simply “ls”. This will produce output like this:


Desktop Downloads Music Pictures Templates Videos
Documents examples.desktop my_script Public test_file

To distinguish files from directories, use the “-F” parameter. This adds a slash to the end of directory names. To display hidden files, use the “-a” parameter. The “-R” parameter allows you to see a recursive listing of the files and directories in your current directory. The “-l” parameter produces a long listing format, providing more information about each file in the directory.

Managing Files and Directories

The ls command can also use filters to search for specific files or patterns. Use the “?” wildcard to represent one character, “” to represent any number of characters, and “[]” to specify a list or range of characters.

For example, “ls -l my_scr?pt” matches “my_scrapt” and “my_script”, while “ls -l my_s*t” matches files that start with “my_s” and end with “t”.

Managing files and directories is easy with the touch command. Use “touch <filename>” to create an empty file with the specified filename, or to update the modification time of a file without changing the contents. Use “touch -a <filename>” to update the access time of a file.

Best Practices

In summary, interacting with the shell doesn’t have to be scary. Use the man command to learn about specific commands, navigate the filesystem using absolute and relative directory references, and list files and directories using filters. Managing files and directories is easy with the touch command. Now go forth and conquer the CLI with confidence! And remember, when in doubt, just type “man”.

Oh, and let’s not forget about shell codes! Shell codes are small pieces of code that can be used to exploit vulnerabilities in systems. They can be used for good, such as testing the security of a system, or for malicious purposes, such as gaining unauthorized access. So be careful out there and always use shell codes responsibly!

Bonus Tips and Tricks for Supercharging Your Command Line Skills!

Copying files and directories is a common task in Linux, and the cp command is the go-to tool for this job. With the cp command, you can create a new copy of a file or directory without affecting the original. In this guide, we’ll cover some tips and tricks for using the cp command effectively.

To copy a file with the cp command, use the following syntax:

```bash
$ cp source_file destination_file
```

For example, to create a copy of a file named test_one and save it as test_two in the current working directory, you can use the following command:

```bash
$ cp test_one test_two
```

The new file acts like a brand new file, with an updated modification time. To verify the details of the copied files, you can use the ls command with the wildcard (*) option, like this:

```bash
$ ls -l test_*
```

This command shows the details of both files named test_one and test_two in the current working directory, including the modification time.

If the destination file already exists, the cp command may not prompt you to this fact. To force the shell to ask whether you want to overwrite a file , add the -i option, like this:

```bash
$ cp -i source_file destination_file
```

For example, this command copies the file test_one to test_two and asks for confirmation before overwriting the existing file:

```bash
$ cp -i test_one test_two
```

You can also copy a file into a preexisting directory by using the destination_directory/ notation, like this:

```bash
$ cp -i test_one /home/christine/Documents/
```

This command copies the file test_one to the preexisting directory /home/christine/Documents/ and asks for confirmation before overwriting any existing files.

But what if you want to copy an entire directory, including all its subdirectories and files? That’s where the -R option comes in. To copy the entire directory named “Scripts” into a new directory named “Mod_Scripts”, you can use the following command:

```bash
$ cp -R Scripts/ Mod_Scripts
```

After executing this command, you can use the ls command to verify that a new directory named “Mod_Scripts” has been created and contains the same files as the original “Scripts” directory, but with updated modification times.

Now, let’s talk about some tips and tricks for using the cp command more effectively. One useful technique is to use wildcard metacharacters like * to copy multiple files with similar names using a single command. For example, to copy all files with the extension .txt in the current directory, you can use the following command:

```bash
$ cp *.txt destination_directory/
```

Another time-saving trick is to use the tab auto-complete feature in the shell to quickly complete filenames and directories names by pressing the Tab key after typing a few characters. This can save time and reduce errors in typing long or complex filenames.

To get more detailed information about files and directories, you can use the ls command with the -l option, which shows a detailed listing of files and directories, including file permissions, ownership, modification time, and size. The -F option adds a symbol to each file and directory name to indicate its type, such as a forward slash / for directories or an asterisk * for executable files.

When creating a symbolic link or a hard link, remember that the original file must pre-exist. To create a symbolic link, use the ln command with the -s option, like this:

```bash
$ ln -s data_file sl_data_file
```

To create a hard link, use the ln command without any parameter, like this:

```bash
$ ln code_file hl_code_file
```

In both cases, the new link will share the same inode number and have the same file size as the original file. With a symbolic link, you can create a link to a file in a different location without moving it. With a hard link, you can create a new name for an existing file without creating a new copy.

Finally, when deleting files or directories, be careful as there is no “undo” option and the deleted files cannot be recovered easily. To remove a file without prompting for confirmation, use the rm command without any option, like this:

```bash
$ rm filename
```

To prompt for confirmation before removing a file, use the -i option, like this:

```bash
$ rm -i filename
```

To create a new directory, use the mkdir command, like this:

```bash
$ mkdir directory_name
```

To create multiple directories and subdirectories at once, use the -p option, like this:

```bash
$ mkdir -p parent_directory/subdirectory/child_directory
```

To delete a directory, use the rmdir command. However, the directory must be empty before it can be deleted. To remove a directory and its contents recursively, use the rm command with the -r option, like this:

```bash
$ rm -r directory_name
```

As a Linux user, you might often find yourself dealing with directories and files in the command line. Whether you’re creating, removing, or just viewing their contents, knowing the right shell commands can make your life a lot easier.

Let’s start with removing directories. It’s called “removing” in the Linux world, and the basic command to remove files is `rm`. But be careful, because using `rm` can be dangerous. It’s like playing with fire, except fire doesn’t delete your important files.

To be safe, you can use `rm -i file_name`, which asks for confirmation before removing the file. After removing a file, its corresponding entry is removed from the file system directory. So, if you accidentally delete a file with `rm`, it’s gone forever unless you have a backup.

Creating directories, on the other hand, is easy using the `mkdir` command. It creates a new directory in the current location. You can use `ls -ld` to show the long listing of the newly created directory. The `d` at the beginning of the listing indicates that it’s a directory and not a file.

To create multiple directories and subdirectories at once, use `mkdir -p`. The `p` option creates all the parent directories as needed, which saves you time and effort.

But what if you need to remove a directory that’s not empty? By default, `rmdir` only removes empty directories. If a directory is not empty, `rmdir` will fail with an error. To remove a directory that’s not empty, first remove all files within the directory, and then use the `rmdir` command.

Alternatively, you can use `rm -r` to remove entire non-empty directories. The `r` option allows the command to remove files within the directory and the directory itself. But be careful with `rm -r`, because if you accidentally delete the wrong directory, it’s gone forever.

Now that we’ve covered the basics of creating and removing directories, let’s move on to viewing file contents. Before displaying a file, it’s important to know its type to avoid issues. The `file` command can be used to peek inside a file and determine its type.

For example, if you run `file my_file`, it will tell you that the file is a text file in ASCII format. The `file` command can also distinguish a directory, identify symbolic links and even tell you to which file it is symbolically linked, and detect executable scripts and tell you if they are executable.

When it comes to displaying the contents of a text file, the `cat` command is your go-to. By default, it displays the contents of the file in the terminal. But if you want to number all the lines in the file, use `cat -n filename`. And if you only want to number the lines that have text in them, use `cat -b filename`. You can also use `cat -T filename` to replace tab characters with the `^I` character combination.

But let’s be honest, working with directories and files in the command line can be intimidating, especially for beginners. That’s why I like to add a bit of humor to my commands to make it more fun.

For example, when removing a file, I like to use `rm -YouSure file_name` instead of `rm -i file_name`. It adds a bit of personality to the command and makes me feel like I’m talking to the computer.

Or when creating a directory, instead of `mkdir new_directory`, I like to use `mkdir -TaDa new_directory`. It’s a small touch, but it makes me smile every time I use it.

In conclusion, the command line and shell scripting may not be for everyone, but they are powerful tools that can make your Linux experience even better. So embrace the shell, try out different shells, and don’t be afraid to get your hands dirty with some scripting. Who knows, you may just discover a whole new world of possibilities. And if all else fails, there’s always the sandwich-making script.

Join the ‘Teendifferent’ brigade on our Instagram. Check out our latest articles, tips, and tricks on cyber security, productivity, computer science, and more. Don’t miss out on the fun, follow us today!

We welcome the responsible use of AI-assistive technology on Medium. To promote transparency, and help set reader expectations, we require that any story created with AI assistance be clearly labeled as such.

--

--

Teendifferent

Artificial Intelligence | Technology | Cybersecurity | Productivity