#Day 3 :Basic Linux Commands

#Day 3 :Basic Linux Commands

Β·

6 min read

  • How to view what's written in a file? πŸ€”

To view the contents of a file in Linux, you can use several commands. The most common commands for viewing file contents are cat, less, and more. Here's how you can use them:

Cat:

  • Purpose: The cat command (short for "concatenate") is used to concatenate and display the contents of one or more files in the terminal.

  • Syntax: cat [options] [file(s)]

  • Usage:

    • cat file.txt: Displays the contents of file.txt in the terminal.

    • cat file1.txt file2.txt: Concatenates and displays the contents of file1.txt followed by file2.txt.

  • Options:

    • -n: Number all output lines.

    • -b: Number only non-blank output lines.

    • -E: Display a dollar sign ($) at the end of each line.

    • -v: Display non-printing characters.

less:

  • Purpose: The less command is a pager that allows viewing file contents one screen at a time, enabling backward and forward navigation.

  • Syntax: less [options] [file(s)]

  • Usage:

    • less file.txt: View the contents of file.txt using less.
  • Navigation:

    • Use the arrow keys to scroll up and down.

    • Press Spacebar to move forward one screen.

    • Press B to move backward one screen.

    • Press Q to quit less.

  • To change the access permissions of files? πŸ”’

chmod:

  • Purpose: The chmod command, short for "change mode," is used to change the permissions (mode) of files and directories in Linux and Unix-like operating systems.

  • Syntax: chmod [options] mode file(s)

  • Usage:

    • chmod mode file: Changes the permissions of the specified file according to the specified mode.

    • chmod mode directory: Changes the permissions of the specified directory according to the specified mode.

  • Modes:

    • There are three types of permissions: read (r), write (w), and execute (x).

    • Permissions are assigned to three categories of users: owner, group, and others.

    • Modes can be represented symbolically (e.g., u+x, g-w) or numerically (e.g., 755, 644).

  • Symbolic Representation:

    • u stands for the user/owner.

    • g Stands for the group.

    • o Stands for others.

    • + Adds the specified permission.

    • - Removes the specified permission.

    • = Sets the specified permission explicitly.

  • Numerical Representation:

    • Each permission is represented by a numeric value:

      • 4 for read (r),

      • 2 for write (w),

      • 1 for execute (x).

    • The sum of these values represents the permissions. For example:

      • 7 (4 + 2 + 1) represents read, write, and execute.

      • 6 (4 + 2) represents read and write.

      • 5 (4 + 1) represents read and execute.

  • Options:

    • -R: Recursively change permissions of directories and their contents.
  • Symbolic representation:

      bashCopy codechmod u+x file.txt    # Adds execute permission for the owner of file.txt
      chmod g-w file.txt    # Removes write permission for the group of file.txt
      chmod o=r file.txt    # Sets read-only permission for others on file.txt
    
  • Numerical representation:

      bashCopy codechmod 755 file.txt    # Gives read, write, and execute permissions to the owner, and read/execute permissions to the group and others
      chmod 644 file.txt    # Gives read/write permissions to the owne
    
  • To check which commands you have run till now? πŸ•΅οΈβ€β™‚οΈ

History command will show you the history of commands you have used.

  • To remove a directory/ Folder? πŸ—‘οΈ

rm -r will remove the directory.

  • To create a fruits.txt file and to view the content? πŸ“πŸŽ

nano:

  • Purpose: nano is a simple and user-friendly text editor for Unix-like operating systems, designed to be easy to use for both beginners and experienced users.

  • Usage: nano [options] [file]

    • If file is provided, nano opens that file for editing. If file is not provided, nano opens a new buffer for editing

  • Add content in fruit.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava? 🍏πŸ₯­πŸŒ

Using nano command.

  • To Show only the top three fruits from the file? πŸ”

head -n3 fruits.txt

head:

  • Purpose: The head command displays the beginning (head) portion of a file or piped data.

  • Syntax: head [options] [file(s)]

  • Usage:

    • head file: Displays the first 10 lines of the specified file.

    • head -n N file: Displays the first N lines of the specified file.

  • Options:

    • -n N: Specifies the number of lines to display. For example, -n 5 will display the first 5 lines.

    • -c N: Displays the first N bytes instead of lines.

  • To Show only the bottom three fruits from the file? πŸ”½

tail:

  • Purpose: The tail command displays the end (tail) portion of a file or piped data.

  • Syntax: tail [options] [file(s)]

  • Usage:

    • tail file: Displays the last 10 lines of the specified file.

    • tail -n N file: Displays the last N lines of the specified file.

  • Options:

    • -n N: Specifies the number of lines to display from the end of the file. For example, -n 5 will display the last 5 lines.

    • -f: Follows the end of the file in real-time, similar to tail -f a log file, showing appended data as it is written.

  • To create another file, Colors.txt, and to view the content? πŸ“πŸŽ¨

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

  • To find the difference between fruits.txt and Colors.txt file? ❓

diff

  • Purpose: The diff command is used to compare the contents of two files line by line and display the differences between them.

  • Syntax: diff [options] file1 file2

  • Usage:

    • diff file1 file2: Compares file1 and file2 and displays the differences between them.
  • Output:

    • If there are differences between the files, diff will output a list of changes needed to make file1 identical to file2.

    • Lines unique to file1 are prefixed with <.

    • Lines unique to file2 are prefixed with >.

    • Common lines are displayed without any prefix.

  • Options:

    • -q: Only reports whether the files differ without showing the actual differences.

    • -r: Recursively compares directories and their contents.

    • -u: Unified output format, which shows the differences in a more human-readable format.


"Thank you for joining me on this journey into DevOps! Your curiosity is the driving force behind this exploration. If you found value in these insights, I'm thrilled! Keep the DevOps flame alive, and stay tuned for more exciting adventures in the world of technology. Happy Learning! πŸš€βœ¨"

"Closing the gap between usβ€”let's connect and make our virtual bond stronger on social media!

Click on -Umesh Salunkhe

Β