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:
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.Using the $
printf
command:$ printf "Your text here \n" file1.txt
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 exitnano
.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":
Open the terminal.
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:
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".Using pipes: Instead of redirecting the output to a file, you can use a pipe to send the output of
command1
as input tocommand2
. The pipe symbol (|
) is used for this. For example:$ command1 | command2
This takes the output of
command1
and passes it as input tocommand2
.You can chain multiple commands together using pipes. For example:
$ command1 | command2 | command3
Here, the output of
command1
is passed tocommand2
, and the output ofcommand2
is passed tocommand3
.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:
ls: Lists files and directories in a directory.
$ ls
cd: Changes the current directory.
$ cd directory_path
mkdir: Creates a new directory.
$ mkdir directory_name
rm: Removes files and directories.
$ rm file_name $ rm -r directory_name # Removes recursively
cp: Copies files and directories.
$ cp source_file destination_file $ cp -r source_directory destination_directory # Copies a directory recursively
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
touch: Creates an empty file or updates the modification timestamp of an existing file.
$ touch file_name
cat: Displays the content of a file.
$ cat file_name
head: Displays the beginning lines of a file.
$ head file_name
tail: Displays the last lines of a file.
$ tail file_name
less: Allows you to view the content of a file interactively.
$ less file_name
chmod: Modifies the permissions of a file or directory.
$ chmod permissions file_name
chown: Changes the ownership of a file or directory.
$ chown owner:group file_name
find: Searches for files and directories based on specified criteria.
$ find directory_path -name "filename"