1
9
2
1
submitted 1 year ago* (last edited 1 year ago) by celestineschrunk@lemmy.run to c/linuxadmin@lemmy.run

Inspired by another post here -> https://lemmy.run/post/46724

Introduction to Tmux

Tmux is a terminal multiplexer that allows you to run multiple terminal sessions within a single window. It enhances your productivity by enabling you to create and manage multiple panes and windows, detach and reattach sessions, and more. In this tutorial, we'll cover the basic usage of Tmux.

Installation

To install Tmux, follow the instructions below:

macOS

brew install tmux

Ubuntu/Debian

sudo apt-get install tmux

CentOS/Fedora

sudo dnf install tmux

Starting a Tmux Session

To start a new Tmux session, open your terminal and enter the following command:

tmux new-session

This will create a new Tmux session with a single window.

Key Bindings

Tmux uses key bindings to perform various actions. By default, the prefix key is Ctrl + b, which means you need to press Ctrl + b before executing any command.

For example, to split the current window vertically, you would press Ctrl + b followed by %.

Panes

Panes allow you to split the current window into multiple sections, each running its own command. Here are some commonly used pane commands:

  • Split the window vertically: Ctrl + b followed by %
  • Split the window horizontally: Ctrl + b followed by "
  • Switch between panes: Ctrl + b followed by an arrow key (e.g., Ctrl + b followed by Left Arrow)
  • Resize panes: Ctrl + b followed by Ctrl + arrow key

Windows

Windows in Tmux are like tabs in a web browser or editor. They allow you to have multiple terminal sessions within a single Tmux session. Here are some window commands:

  • Create a new window: Ctrl + b followed by c
  • Switch between windows: Ctrl + b followed by a number key (e.g., Ctrl + b followed by 0 to switch to window 0)
  • Close the current window: Ctrl + b followed by &

Session Management

Tmux allows you to detach and reattach sessions, which is useful when you need to switch between different machines or disconnect from your current session.

  • Detach from the current session: Ctrl + b followed by d
  • List all sessions: tmux list-sessions
  • Reattach to a detached session: tmux attach-session -t <session-name>

Configuration

Tmux can be customized by creating a .tmux.conf file in your home directory. You can modify key bindings, customize the status bar, and more. Here's an example of how to change the prefix key to Ctrl + a:

  1. Create or edit the .tmux.conf file in your home directory.
  2. Add the following line to the file: set-option -g prefix C-a
  3. Save the file and exit.

After making changes to your configuration file, you can either restart Tmux or reload the configuration by running the following command within a Tmux session:

tmux source-file ~/.tmux.conf

Conclusion

Congratulations! You've learned the basics of using Tmux. With Tmux, you can work more efficiently by managing multiple terminal sessions within a single window. Explore more features and commands by referring to the Tmux documentation.

3
4
submitted 1 year ago by root@lemmy.run to c/linuxadmin@lemmy.run

In this tutorial, we will explore how to use sed (stream editor) with examples in the Markdown language. sed is a powerful command-line tool for text manipulation and is widely used for tasks such as search and replace, line filtering, and text transformations. What is described below barely scratches the surface what sed can do.

Table of Contents

  1. Installing Sed
  2. Basic Usage
  3. Search and Replace
  4. Deleting Lines
  5. Inserting and Appending Text
  6. Transformations
  7. Working with Files
  8. Conclusion

1. Installing Sed

Before we begin, make sure sed is installed on your system. It usually comes pre-installed on Unix-like systems (e.g., Linux, macOS). To check if sed is installed, open your terminal and run the following command:

sed --version

If sed is not installed, you can install it using your package manager. For example, on Ubuntu or Debian-based systems, you can use the following command:

sudo apt-get install sed

2. Basic Usage

To use sed, you need to provide it with a command and the input text to process. The basic syntax is as follows:

sed 'command' input.txt

Here, 'command' represents the action you want to perform on the input text. It can be a search pattern, a substitution, or a transformation. input.txt is the file containing the text to process. If you omit the file name, sed will read from the standard input.

3. Search and Replace

One of the most common tasks with sed is search and replace. To substitute a pattern with another in Markdown files, use the s command. The basic syntax is:

sed 's/pattern/replacement/' input.md

For example, to replace all occurrences of the word "apple" with "orange" in input.md, use the following command:

sed 's/apple/orange/' input.md

4. Deleting Lines

You can also delete specific lines from a Markdown file using sed. The d command is used to delete lines that match a particular pattern. The syntax is as follows:

sed '/pattern/d' input.md

For example, to delete all lines containing the word "banana" from input.md, use the following command:

sed '/banana/d' input.md

5. Inserting and Appending Text

sed allows you to insert or append text at specific locations in a Markdown file. The i command is used to insert text before a line, and the a command is used to append text after a line. The syntax is as follows:

sed '/pattern/i\inserted text' input.md
sed '/pattern/a\appended text' input.md

For example, to insert the line "This is a new paragraph." before the line containing the word "example" in input.md, use the following command:

sed '/example/i\This is a new paragraph.' input.md

6. Transformations

sed provides various transformation commands that can be used to modify Markdown files. Some useful commands include:

  • y: Transliterate characters. For example, to convert all uppercase letters to lowercase, use:

    sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' input.md
    
  • p: Print lines. By default, sed only prints the modified lines. To print all lines, use:

    sed -n 'p' input.md
    
  • r: Read and insert the contents of a file. For example, to insert the contents of insert.md after the line containing the word "insertion point" in input.md, use:

    sed '/insertion point/r insert.md' input.md
    

These are just a few examples of the transformation commands available in sed.

7. Working with Files

By default, sed modifies the input in-place. To make changes to a file and save the output to a new file, you can use input/output redirection:

sed 'command' input.md > output.md

This command runs sed on input.md and saves the output to output.md. Be cautious when using redirection, as it will overwrite the contents of output.md if it already exists.

8. Conclusion

In this tutorial, we have explored the basics of using sed with Markdown files. You have learned how to perform search and replace operations, delete lines, insert and append text, apply transformations, and work with files. sed offers a wide range of capabilities, and with practice, you can become proficient in manipulating Markdown files using this powerful tool.

4
1
submitted 1 year ago by root@lemmy.run to c/linuxadmin@lemmy.run

Beginner's Guide to grep

grep is a powerful command-line tool used for searching and filtering text in files. It allows you to find specific patterns or strings within files, making it an invaluable tool for developers, sysadmins, and anyone working with text data. In this guide, we will cover the basics of using grep and provide you with some useful examples to get started.

Installation

grep is a standard utility on most Unix-like systems, including Linux and macOS. If you're using a Windows operating system, you can install it by using the Windows Subsystem for Linux (WSL) or through tools like Git Bash, Cygwin, or MinGW.

Basic Usage

The basic syntax of grep is as follows:

grep [options] pattern [file(s)]
  • options: Optional flags that modify the behavior of grep.
  • pattern: The pattern or regular expression to search for.
  • file(s): Optional file(s) to search within. If not provided, grep will read from standard input.

Examples

Searching in a Single File

To search for a specific pattern in a single file, use the following command:

grep "pattern" file.txt

Replace "pattern" with the text you want to search for and file.txt with the name of the file you want to search in.

Searching in Multiple Files

If you want to search for a pattern across multiple files, use the following command:

grep "pattern" file1.txt file2.txt file3.txt

You can specify as many files as you want, separating them with spaces.

Ignoring Case

By default, grep is case-sensitive. To perform a case-insensitive search, use the -i option:

grep -i "pattern" file.txt

Displaying Line Numbers

To display line numbers along with the matching lines, use the -n option:

grep -n "pattern" file.txt

This can be helpful when you want to know the line numbers where matches occur.

Searching Recursively

To search for a pattern in all files within a directory and its subdirectories, use the -r option (recursive search):

grep -r "pattern" directory/

Replace directory/ with the path to the directory you want to search in.

Using Regular Expressions

grep supports regular expressions for more advanced pattern matching. Here's an example using a regular expression to search for email addresses:

grep -E "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" file.txt

In this case, the -E option enables extended regular expressions.

Conclusion

grep is a versatile tool that can greatly enhance your text searching and filtering capabilities. With the knowledge you've gained in this beginner's guide, you can start using grep to quickly find and extract the information you need from text files. Experiment with different options and explore more advanced regular expressions to further expand your skills with grep. Happy grepping!

5
2
submitted 1 year ago by root@lemmy.run to c/linuxadmin@lemmy.run

Hello r/linuxadmin reddit refugees to c/linuxadmin.

I moved to Lemmy and was missing one of my favorite sub.

After missing it, I decided to create and make it available to others like me.

Welcome all and let's create a healthy environment for discussion and sharing tips.

6
3
submitted 1 year ago* (last edited 1 year ago) by root@lemmy.run to c/linuxadmin@lemmy.run
  1. Introduction to awk:

    awk is a powerful text processing tool that allows you to manipulate structured data and perform various operations on it. It uses a simple pattern-action paradigm, where you define patterns to match and corresponding actions to be performed.

  2. Basic Syntax:

    The basic syntax of awk is as follows:

    awk 'pattern { action }' input_file
    
    • The pattern specifies the conditions that must be met for the action to be performed.
    • The action specifies the operations to be carried out when the pattern is matched.
    • The input_file is the file on which you want to perform the awk operation. If not specified, awk reads from standard input.
  3. Printing Lines:

    To start with, let's see how to print lines in Markdown using awk. Suppose you have a Markdown file named input.md.

    • To print all lines, use the following command:
      awk '{ print }' input.md
      
    • To print lines that match a specific pattern, use:
      awk '/pattern/ { print }' input.md
      
  4. Field Separation:

    By default, awk treats each line as a sequence of fields separated by whitespace. You can access and manipulate these fields using the $ symbol.

    • To print the first field of each line, use:
      awk '{ print $1 }' input.md
      
  5. Conditional Statements:

    awk allows you to perform conditional operations using if statements.

    • To print lines where a specific field matches a condition, use:
      awk '$2 == "value" { print }' input.md
      
  6. Editing Markdown Files:

    Markdown files often contain structured elements such as headings, lists, and links. You can use awk to modify and manipulate these elements.

    • To change all occurrences of a specific word, use the gsub function:
      awk '{ gsub("old_word", "new_word"); print }' input.md
      
  7. Saving Output:

    By default, awk prints the result on the console. If you want to save it to a file, use the redirection operator (>).

    • To save the output to a file, use:
      awk '{ print }' input.md > output.md
      
  8. Further Learning:

    This guide provides a basic introduction to using awk for text manipulation in Markdown. To learn more advanced features and techniques, refer to the awk documentation and explore additional resources and examples available online.

Remember, awk is a versatile tool, and its applications extend beyond Markdown manipulation. It can be used for various text processing tasks in different contexts.

7
1
submitted 1 year ago by root@lemmy.run to c/linuxadmin@lemmy.run

Linux Command Line Cheat Sheet

Linux Admin : Resources for Linux SysAdmin

0 readers
1 users here now

General Discussion for topics for Linux SysAdmin

founded 1 year ago
MODERATORS