Basic linux commands with Examples (Post-04)

Β·

9 min read

Basic linux commands with Examples  (Post-04)

To view what's written in a file.

  1. more: Used to view a text file one page at a time, press the spacebar to go to the next page. πŸ”½

    • Example: more filename
  2. less: Similar to more but with additional features like navigating pages, searching for strings, and using functions similar to the vi editor. ⏬

    • Example: less filename
  3. head: Displays the first few lines of a file (by default, ten lines). ⬆️

    • Examples:

      • head myfile.txt displays the first ten lines of myfile.txt.

      • head -15 myfile.txt displays the first fifteen lines of myfile.txt.

  4. tail: Displays the last part of a file. ⬇️

    • Example: tail filename
  5. cat: Used to concatenate files or display file contents. 🐱

    • Examples:

      • cat 01.txt displays the contents of file 01.txt.

      • cat 01.txt 02.txt displays the contents of both files.

      • cat file1.txt file2.txt > file3.txt combines file1.txt and file2.txt into file3.txt.

      • cat note5 >> notes appends note5 to notes.

      • cat >> file1 adds additional data to file1.

These commands offer different ways to view and manipulate file content in Linux. Feel free to use the command that suits your specific needs.

To change the access permissions of files.

To change the access permissions of files in Linux, you can use the chmod command. The chmod command allows you to modify the read, write, and execute permissions for the owner, group, and other users. Here's how you can use the chmod command:

  1. Symbolic Mode: You can use symbols (+, -, =) to add, remove, or set specific permissions. The symbols are represented by:

    • βž• +: Adds permissions

    • βž– -: Removes permissions

    • ➑️ =: Sets permissions explicitly

Examples:

  • To add read and write permissions for the owner: chmod +rw filename

  • To remove execute permission for the group: chmod g-x filename

  • To set read and execute permissions for others, removing all other permissions: chmod o=rx filename

  1. Numeric Mode: You can use a numeric value to set permissions explicitly using the octal representation.

    • πŸ”’ 4: Read permission

    • ✍️ 2: Write permission

    • ▢️ 1: Execute permission

Examples:

  • To set read and write permissions for the owner and read-only permissions for the group and others: chmod 644 filename

  • To grant full permissions to the owner, read and execute permissions to the group, and read-only permissions to others: chmod 750 filename

Remember, you need appropriate permissions to modify the access permissions of a file. The chmod command can be used with various options and modifiers to change permissions recursively or for multiple files simultaneously. Refer to the chmod command's manual page (man chmod) for more details and additional options.

Please exercise caution when changing access permissions, as improper settings may compromise the security or functionality of your files and system.

To check which commands you have run till now.

To check the commands that you have run so far in a Linux terminal, you can use the history command. The history command displays a list of previously executed commands along with their corresponding line numbers. πŸ”Ž

Here's how you can use the history command:

  1. Open a terminal. πŸ’»

  2. Type history and press Enter. ⌨️

The command history will be displayed, showing the list of commands you have run during your current terminal session. Each command will be listed with a line number, making it easier to reference specific commands if needed. πŸ“œ

You can also use the history command with options to modify its behavior. For example, you can use the -c option to clear the command history or use the -n option to display command numbers. βš™οΈ

Please note that the command history is specific to each terminal session, and it may not reflect the complete command history across multiple sessions or if you have started a new terminal session. ⚠️

Now you can easily keep track of the commands you've used in your Linux terminal! ✨

To remove a directory/ Folder.

To remove a directory or folder in Linux, you can use the rmdir or rm command. The rmdir command is used to remove empty directories, while the rm command can remove both files and directories, including non-empty directories.

Here's how you can use these commands:

  1. rmdir command:

     rmdir directory_name
    

    This command will remove an empty directory. If the directory is not empty, you will receive an error.

  2. rm command:

     rm -r directory_name
    

    The -r option stands for "recursive" and allows the rm command to remove directories and their contents recursively, including non-empty directories. Be cautious when using this command, as it permanently deletes the specified directory and its contents.

Remember to exercise caution when using the rm command, as it permanently deletes files and directories without confirmation.

Now, go ahead and remove the directory or folder you want to delete! πŸ—‘οΈ

To create a fruits.txt file and to view the content.

To create a fruits.txt file, you can use the touch command to create an empty file, and then you can use a text editor like nano or vi to add content to the file. Here's how you can do it:

  1. Create the fruits.txt file:

     touch fruits.txt
    

    This command creates an empty file named fruits.txt.

  2. Open the fruits.txt file with a text editor:

     nano fruits.txt
    

    This command opens the fruits.txt file in the nano text editor.

  3. Add the content to the file: Inside the text editor, you can add the names of fruits. For example:

     Apple
     Banana
     Orange
    
  4. Save and exit the text editor: In the nano text editor, press Ctrl + O to save the file and Ctrl + X to exit.

Now, you have created the fruits.txt file with the desired content.

To view the content of the fruits.txt file, you can use the cat command:

cat fruits.txt

This command displays the content of the fruits.txt file in the terminal.

🍎🍌🍊 Enjoy exploring the content of your fruits.txt file!

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

To add the specified content to the devops.txt file, you can use a text editor like nano or vi. Here's how you can do it:

  1. Open the devops.txt file with a text editor:

     nano devops.txt
    

    This command opens the devops.txt file in the nano text editor. πŸ“

  2. Add the content to the file: Inside the text editor, add the following content, one item per line:

     Apple 
     Mango 
     Banana 
     Cherry 
     Kiwi 
     Orange 
     Guava
    
  3. Save and exit the text editor: In the nano text editor, press Ctrl + O to save the file and Ctrl + X to exit. πŸ’Ύ

Now, you have added the specified content to the devops.txt file, with each item on a separate line.

To view the content of the devops.txt file, you can use the cat command:

cat devops.txt

This command displays the content of the devops.txt file in the terminal. πŸ“„

🍎πŸ₯­πŸŒπŸ’πŸ₯πŸŠπŸˆ Enjoy exploring the content of your devops.txt file!

To Show only top three fruits from the file.

o show only the top three fruits from the devops.txt file, you can use the head command. The head command allows you to display the first few lines of a file. Here's how you can do it:

head -3 devops.txt

This command displays the first three lines of the devops.txt file, which will show the top three fruits.

🍎πŸ₯­πŸŒ Enjoy exploring the top three fruits from your devops.txt file!

To Show only bottom three fruits from the file.

To show only the bottom three fruits from the devops.txt file, you can use the tail command. The tail command allows you to display the last few lines of a file. Here's how you can do it:

tail -3 devops.txt

This command displays the last three lines of the devops.txt file, which will show the bottom three fruits.

πŸ’πŸ₯πŸŠ Enjoy exploring the bottom three fruits from your devops.txt file!

To create another file Colors.txt and to view the content.

To create the Colors.txt file, you can use a text editor like nano or vi to create and add content to the file. Here's how you can do it:

  1. Create the Colors.txt file using a text editor:

     nano Colors.txt
    

    This command opens the Colors.txt file in the nano text editor.

  2. Add the desired content to the file: Inside the text editor, you can add the names of colors or any other content you prefer. For example:

     Red
     Blue
     Green
     Yellow
     Orange
     Purple
    
  3. Save and exit the text editor: In the nano text editor, press Ctrl + O to save the file and Ctrl + X to exit.

Now, you have created the Colors.txt file with the desired content.

To view the content of the Colors.txt file, you can use the cat command:

cat Colors.txt

This command displays the content of the Colors.txt file in the terminal.

🎨 Enjoy exploring the content of your Colors.txt file!

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

To add the specified content to the Colors.txt file, you can use a text editor like nano or vi. Here's how you can do it:

  1. Open the Colors.txt file with a text editor:

     nano Colors.txt
    

    This command opens the Colors.txt file in the nano text editor.

  2. Add the content to the file: Inside the text editor, add the following content, one item per line:

     Red
     Pink
     White
     Black
     Blue
     Orange
     Purple
     Grey
    
  3. Save and exit the text editor: In the nano text editor, press Ctrl + O to save the file and Ctrl + X to exit.

Now, you have added the specified content to the Colors.txt file, with each color on a separate line.

To view the content of the Colors.txt file, you can use the cat command:

cat Colors.txt

This command displays the content of the Colors.txt file in the terminal.

πŸ”΄πŸŒΈβšͺοΈβš«οΈπŸ”΅πŸŠπŸ’œπŸŒ«οΈ Enjoy exploring the content of your Colors.txt file!

To find the difference between fruits.txt and Colors.txt file.

To find the difference between the fruits.txt and Colors.txt files, you can use the diff command. The diff command compares two files and shows the differences between them.

Here's how you can use the diff command to compare the contents of the fruits.txt and Colors.txt files:

diff fruits.txt Colors.txt

This command compares the contents of the two files and displays the lines that differ between them, if any. If the files are identical, no output will be displayed.

By running the diff command, you can identify any variations between the fruits.txt and Colors.txt files and understand the differences in their contents.

πŸŽπŸŒπŸŠπŸ”΄πŸŒΈβšͺοΈβš«οΈπŸ”΅πŸŠπŸ’œπŸŒ«οΈ Explore and discover the differences between your fruits.txt and Colors.txt files!


Thank you for giving your precious time for reading this blog/article and also follow Shahaji Kadam for more such blogs/articles.

HashTags: #90daysofdevops #devops #cloud #aws #awscloud #awscommunity #docker #linux #kubernetes #k8s #ansible #grafana #terraform #github #opensource #90days #challenge #learning

Β