Linux — The Operating System of Choice — Part VI

Linux — The Operating System of Choice — Part VI

ADDING TEXT TO FILE IN LINUX:

To add text to a file in Linux, you can use several methods.

Here are a few commonly used approaches:

  1. Using the $echo command:

     $ echo "Your text here"  file1.txt
    

    This appends the specified text to the end of the file file1.txt. If the file doesn't exist, it will be created.

  2. Using the $printf command:

     $ printf "Your text here \n"  file1.txt
    
  3. Using a text editor:

     $ nano file1.txt
    

    This opens the nano text editor, where you can navigate to the desired location and add your text. Press Ctrl+O to save the changes, and Ctrl+X to exit nano.

  4. To view what is there inside a file.

     $ cat  filename.txt
    

    ABOUT $vi COMMAND:

    In Linux, the command "vi" is a text editor used to create, view, and edit files. Here's how you can use the "vi" command to open a file named "filename":

    1. Open the terminal.

    2. Type the following command and press Enter:

       $ vi filename
      

Once you execute the command, the "vi" editor will open with the contents of the file "filename" displayed. You can then navigate and edit the file as needed.

Here are some basic commands within the "vi" editor:

  • To navigate the file:

    • Use the arrow keys to move the cursor.

    • Press "j" to move the cursor down.

    • Press "k" to move the cursor up.

    • Press "h" to move the cursor left.

    • Press "l" to move the cursor right.

    • Alternatively, you can use the "Page Up" and "Page Down" keys to scroll.

  • To enter editing mode:

    • Press "i" to enter the Insert mode. This allows you to edit the text.

    • Press "a" to enter the Append mode. This is similar to Insert mode but appends text after the cursor.

    • Press "o" to enter the Open line mode. This inserts a new line below the current line and enters Insert mode.

    • Press "O" (uppercase) to enter the Open line mode above the current line.

  • To save and exit:

    • Press the "Esc" key to exit the Insert mode (if you're in it).

    • Type ":w" and press Enter to save the changes.

    • Type ":q" and press Enter to quit the editor.

    • If you have unsaved changes and want to quit, use ":q!" to forcefully exit without saving.

REDIRECTS IN LINUX:

PIPES:

In Linux, pipes allow you to connect the output of one command to the input of another command, enabling you to chain commands together to perform more complex operations.

The pipe symbol (|) is used to create a pipe between two commands.

Here's how pipes work in Linux:

  1. Command output redirection: You can redirect the output of a command to a file using the > symbol. For example:

     $ command1 > output.txt
    

    This redirects the output of command1 to the file "output.txt".

  2. Using pipes: Instead of redirecting the output to a file, you can use a pipe to send the output of command1 as input to command2. The pipe symbol (|) is used for this. For example:

     $ command1 | command2
    

    This takes the output of command1 and passes it as input to command2.

    You can chain multiple commands together using pipes. For example:

     $ command1 | command2 | command3
    

    Here, the output of command1 is passed to command2, and the output of command2 is passed to command3.

    This allows you to combine the functionality of different commands and create powerful and flexible command-line operations.

FILE MAINTENACE COMMANDS:

In Linux, there are several file maintenance commands available to manage and manipulate files. Here are some commonly used file maintenance commands:

  1. ls: Lists files and directories in a directory.

     $ ls
    
  2. cd: Changes the current directory.

     $ cd directory_path
    
  3. mkdir: Creates a new directory.

     $ mkdir directory_name
    
  4. rm: Removes files and directories.

     $ rm file_name
     $ rm -r directory_name  # Removes recursively
    
  5. cp: Copies files and directories.

     $ cp   source_file   destination_file
     $ cp -r   source_directory   destination_directory  # Copies a directory recursively
    
  6. mv: Moves or renames files and directories.

     $ mv   old_name   new_name  # Renames a file or directory
     $ mv   source_file   destination_directory  # Moves a file to a new directory
    
  7. touch: Creates an empty file or updates the modification timestamp of an existing file.

     $ touch file_name
    
  8. cat: Displays the content of a file.

     $ cat file_name
    
  9. head: Displays the beginning lines of a file.

     $ head file_name
    
  10. tail: Displays the last lines of a file.

    $ tail file_name
    
  11. less: Allows you to view the content of a file interactively.

    $ less file_name
    
  12. chmod: Modifies the permissions of a file or directory.

    $ chmod permissions file_name
    
  13. chown: Changes the ownership of a file or directory.

    $ chown owner:group file_name
    
  14. find: Searches for files and directories based on specified criteria.

    $ find directory_path -name "filename"