Maya McRae

Maya McRae

My Linux Survival Kit

2023-11-01

Alt Text

Hey there 👋🏽,

If you're anything like me, you might've spent most of your life steering clear of the command line interface. But guess what? Learning to use the command line will open up a world of endless possibilities. Plus you'll look cool typing into a terminal!

So, what exactly is this command line thing?

Think of it as the skeletal framework of your computer—a tool that lets you input commands directly for specific tasks. The magic happens within the "shell," the user interface where you wield this power. While there are various shells out there, the Born Again SHell (Bash) reigns supreme, especially on Linux and Mac systems. If you're venturing into the Windows realm, you'll need to beef it up with the Linux CLI feature. For our guide, we'll stick with Bash, mainly because it's more user-friendly and potent compared to its Windows counterpart.

Now, let's dive into some basic command line maneuvers:

Getting Started

First things first—accessing the command line. On a Mac, simply hit the command key and spacebar, then type in "terminal." For Linux users, search for it and launch your terminal.

Listing Files

Ever wondered how to peek into directories? Meet the ls command, short for "List Files." What makes linux CLI so powerful is the amount of options you can pass to a single command. You can even consult its manual (man ls) to uncover a trove of functionalities.

Try typing man ls.

As you can see there are quite a few, you can even link some together. For the sake of this article, the most common commands are the following:

  1. ls -ll - displays files and folders in a detailed format, including permissions,
  2. ls -a - displays all files and folders including hidden folders.

Navigating Directories

You can change directories using the cd command. Changing directories is basically moving to another file, and since everything in Linux is a file, it is probably the most used command. I strongly recommend that you get familiar with the cd command as it is even used in Windows.

Pro tips:

  • cd / takes you to the root directory
  • cd .. moves you to the previous folder
  • Specifying a path (cd /path/path) lands you exactly where you want, regardless of your current location.

File Creation and Deletion

  • Creating a blank file? You can do so with touch <filename>.
  • When it's time to bid adieu, rm handles file deletion.
  • Want to delete every file in a folder? Use the * (asterisk) wildcard. rm * is your handy cleanup tool.

If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force.

This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below: rm –rf <filename>.

Editing Plain Text Files

Editing text files varies across platforms. On Ubuntu Linux, nano is beginner-friendly. - nano /path/to/file

Otherwise, the vim editor is available on just about any system and can be invoked with the vi <filename> syntax.

Displaying File Contents

You can use cat to display file contents which is cool, but for larger files, more or less allows for better navigation. No more texts whizzing past your screen!

For instance: more <filename> will display the contents of a file on the screen, and prompt you to scroll through the file one screen at a time.

Command Redirection

Each command line application can accept standard input and writes to standard output. You can use the > or | operators to redirect output from one command into another. This lets you chain commands together into much more powerful ones.

For instance, if you want to use ls –l to display a list of files but it keeps scrolling off the screen, you can pipe the output from the ls –l command into the input of the more command by using the | character: ls –l | more

If you wanted to save the output of that list directly into a file instead of displaying on the console, you could use the > operator to redirect the output into a file instead: ls -l > filename.list

You could then use the cat command to display the contents of that file, pipe that into the grep command (detailed further below), and then redirect that output into a separate file:

  • cat filename.list | grep keyword > filefound.list

Running a Script in the Current Folder

If you have an application or shell script in the current folder, you can't simply type the name of the command and expect it to start. You'll need to add a ./ to the beginning of the command in order to start it. Why? Because in the Bash shell, the current directory, or "." folder, is not included in the system path. So to launch scriptname.sh in the current folder, you'll need to use: ./scriptname.sh

History and Shortcuts

Relive past glory with the up/down arrows to loop through the history of your ternimal commands. You could also use the history or Ctrl+R for command search. It shows a list of all the recently used commands. The Ctrl+R shortcut key will start a search mode where you can type the first few characters of a command to search through your recent history.

Looping Over a Set of Files

If you want to loop through a set of filenames and perform an action on each one, you can use the for command to loop through a set of files. For instance, to loop through all the .txt files in the current directory and display them on the console, you could use: for f in *.txt;do echo $f;done

Find Anything From the Terminal

Oldie but goodie terminal command: you can use the very powerful find command to locate literally any file on your system. For instance, if you wanted to find all files with .txt in their name that were modified exactly 5 days ago in the current directory, you would use this command:

  • find . –name "*.txt" –mtime 5

  • For .txt files modified within the last 5 days: find . -name "*.txt" -mtime -5 *note the with a negative sign before the number ** find /path/to/directory -name "*.txt" -mtime -5 Specifies the directory where the search will be performed.

Find a Text String in Files

The grep command can be used to quickly find text within files, even searching through subdirectories. For instance, if you wanted to search through all files in the current directory and below it for "text string", you could use this command:

  • grep –ir "text string" *

Batch Rename Files

You can use the rename command to quickly rename files using a regular expression pattern. For instance, if you wanted to rename all files containing foo to contain bar instead, you could use a command like this one:

  • rename –v 's/foo/bar/g' *

Using Bash Shortcut Keys

There are a number of very useful shortcut keys you can use in the bash shell to navigate like a pro, and it pays to master them all. Here's a couple to get you started:

  • Ctrl + U: Clears the line from the cursor point back to the beginning.
  • Ctrl + A: Moves the cursor to the beginning of the line.
  • Ctrl + E: Moves the cursor to the end of the line.
  • Ctrl + R: Allows you to search through the previous commands.

The command line might seem daunting at first, but fear not! Embrace it, experiment with commands, and soon you'll be orchestrating your computer like a maestro.

Happy coding!