In the world of DevOps, knowing how to efficiently interact with Linux systems is crucial. Whether you're managing servers, automating tasks, or deploying applications, you'll often find yourself using common Linux commands to perform various operations. In this post, we'll cover some of the essential commands like cat
, vi
, touch
, mkdir
, chmod
, and how to run a script on a Linux machine.
1. cat - Viewing File Contents
The cat
command is used to view the contents of a file directly from the terminal. It's a simple and effective way to quickly check the contents of a file.
Example:
cat filename.txt
- This command will display the contents of
filename.txt
in the terminal window. It's especially useful when you need to inspect configuration files or log files.
2. vi - Editing Files
The vi
command opens a text editor directly from the command line. It's one of the most widely used editors in Linux environments, especially for configuration management and script editing.
Basic Usage:
To edit a file, run:
vi filename.txt
- Once inside vi, you start in command mode. Press
i
to enter insert mode, where you can start typing and editing the file. - When you're done editing, press
Esc
to return to command mode. - To save and exit, type
:wq!
and hitEnter
.
Quick Tips for vi
:
:w
saves the file.:q
quits the editor.:wq!
saves and quits (force save if file is read-only).
3. touch - Creating Empty Files
The touch
command is used to create empty files or update the timestamp of an existing file. If the file does not already exist, it will create an empty file with the specified name.
Example:
touch newfile.txt
- This command creates an empty file called
newfile.txt
. You can then edit this file usingvi
or any other text editor.
4. mkdir - Creating Directories
The mkdir
command is used to create directories. This is particularly useful when you're organizing files or creating a new structure for your project.
Example:
mkdir new_directory
- This creates a new directory called
new_directory
in the current working directory. If you need to create multiple nested directories, use the-p
flag to create parent directories as needed.
mkdir -p parent_directory/child_directory
- This command creates
parent_directory
andchild_directory
within it (if they do not already exist).
5. chmod - Changing File Permissions
The chmod
command is used to change the permissions of a file or directory. Linux file permissions are represented by three types of users: the file's owner, the group that owns the file, and others (everyone else).
Permissions can be represented numerically (e.g., 755
) or symbolically (e.g., rwxr-xr-x
), and you use chmod
to set these permissions.
Numeric Permissions:
- r (read) = 4
- w (write) = 2
- x (execute) = 1
You can add the values for each permission to set specific access rights.
Example:
chmod 755 script.sh
- 7 = read (4) + write (2) + execute (1) =
rwx
(owner has full permissions) - 5 = read (4) + execute (1) =
r-x
(group has read and execute permissions) - 5 = read (4) + execute (1) =
r-x
(others have read and execute permissions)
This sets the following permissions for script.sh
:
- Owner: rwx
- Group: r-x
- Others: r-x
Example 2: Granting Full Permissions
chmod 777 file.txt
- This command gives read, write, and execute permissions to the file for owner, group, and others. Use this command with caution, as it grants full access to everyone.
Running a Script After Creating and Modifying It
Now, let’s go through an example where we create a simple script, modify it, and run it.
Step 1: Create a new script
First, use touch
to create a new script file:
touch myscript.sh
Step 2: Edit the script with vi
Now, use vi
to edit the script:
vi myscript.sh
-
Add the following simple shell script:
#!/bin/bash echo "Hello, DevOps!"
- Save and exit the editor (
:wq!
).
Step 3: Change permissions with chmod
Make the script executable by setting the appropriate permissions:
chmod 755 myscript.sh
- This makes the script readable and executable by the owner, and readable and executable by the group and others.
Step 4: Run the Script
Finally, run the script using the following command:
./myscript.sh
- This will execute the script, and you should see the output:
Hello, DevOps!
.
Conclusion
In this blog post, we've covered some of the most common and essential Linux commands that are indispensable for anyone working in DevOps. Understanding commands like cat
, vi
, touch
, mkdir
, chmod
, and how to run a script will make your job much easier when you're managing servers, automating tasks, or deploying applications.
These commands are just the beginning, but mastering them will give you a strong foundation for working efficiently in any Linux-based DevOps environment. Happy scripting!