Skip to main content

Command Palette

Search for a command to run...

"DevOps Odyssey": Day 3 - Basic Linux Commands

Updated
4 min read

Commands and Usage

View what's written in a file

cat: The cat command is used to concatenate and display the contents of one or more files. It is commonly used to view the contents of a file.

cat -b: This adds line numbers to non-blank lines

cat -n: This adds line numbers to all lines

cat -s: This squeezes blank lines into one line

cat –E: This shows $ at the end of the line

This will display the contents of myfile.txt on the terminal.

Permissions

Imagine you have a room (file) that contains a valuable treasure (information). You want to control who can access this treasure, who can modify it, and who can execute certain actions related to it. Linux permissions work similarly:

Read (r) Permission:

Imagine the treasure is kept in a locked box (file), and the "Read" permission allows someone to see what's inside the box without changing or taking anything. Example: You have a document file, and you allow your friend to read it, but they can't edit or delete it. Write (w) Permission:

Imagine the "Write" permission gives someone the key to the locked box, allowing them to add, modify, or remove things from it. Example: You give your colleague "Write" permission on a project file so that they can make changes and update it. Execute (x) Permission:

Imagine the "Execute" permission is like giving someone special instructions on how to interact with the treasure. For example, allowing them to use a specific tool or action on it. Example: You have a script (a set of instructions) to analyze data. You give the "Execute" permission to your team members so they can run the script to process the data. Now, let's consider the three categories of users:

Owner:

The owner is like the person who owns the room and the treasure inside it. The owner can decide who else can have access to the treasure and what actions they can perform. Group:

The group is like a group of friends or colleagues who share access to the room and its treasure. The group permission allows a set of people to have similar access rights to the treasure. Others:

"Others" represent everyone else who is not the owner or part of the group. This category covers all other users on the system who don't have a direct connection to the room (file) or its treasure. The permission string (e.g., "rw-r--r--") represents what level of access each category has to the file:

The first character indicates the file type (e.g., "d" for a directory or "-" for a regular file). The next three characters ("rw-") represent the owner's permissions. The following three characters ("r--") represent the group's permissions. The last three characters ("r--") represent the permissions for others. So, with the permission string "rw-r--r--":

The owner has read and write permissions (can view and modify the file). The group has read-only permission (can only view the file). Others have read-only permission (can only view the file). These permissions ensure that only authorized users can access and interact with files, safeguarding your data and maintaining privacy and security on your Linux system.

Check which commands you have run till now

Remove a directory/folder

The rm command is used to remove or delete files or directories. Be careful when using this command, as it permanently deletes the specified files, and there is no "Trash Bin" in Linux

Create a fruits.txt file and view the content

vim is a text editor of "VI Improved"

Mostly used modes in vim:

  • Normal mode: This is the default mode in which vim starts. In normal mode, you can use various commands to navigate and edit the text.

  • Insert mode: In insert mode, you can type text into the file. To enter insert mode, press the "i" key. To exit insert mode and return to normal mode, press the "Esc" key.

  • Command mode: In command mode, you can enter commands to perform various actions, such as saving the file or quitting vim. To enter command mode, press the ":" key.

Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

\n - new line

Show only top three fruits from the file

The head command is generally used to display the top lines in a text file

  • head: Print the first 10 lines of each FILE to standard output.

  • head -n20 or head -20: Print the first NUM lines instead of the first 10.

  • head -c20: Print the first NUM bytes of each file.

  • head -q: Never print headers giving file names.

Show only bottom three fruits from the file.

The tail command shows the last lines in a text file

-n - number of lines

Create another file Colors.txt and view the content

Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

Find the difference between fruits.txt and Colors.txt file

diff stands for difference. This command is used to display the differences in the files by comparing the files line by line