Day 3 Task: Basic Linux Commands

Neha palNeha pal
4 min read

Table of contents

Commands:

  1. sudo: This command executes only that command with root/ superuser privileges.

  2. ls: List the subdirectories and files available in the present directory

    Examples:

    • ls -l--> List the files and directories in long list format with extra information

    • ls -a --> List all including hidden files and directory

    • ls *.sh --> List all the files having .sh extension.

    • ls -i --> List the files and directories with index numbers Inodes

    • ls -d */ --> List only directories. (We can also specify a pattern)

  3. whoami: Shows the current login username.

  4. uname: Shows the OS kernel.

  5. uname -r: To check the kernel version.

  6. cat [filename]: To view what's written in a file.

  7. history: To check which commands you have run till now.

  8. mkdir [directory-name]: Creating the multiple directories.

  9. cd [directoryname]: Change the current directory.

  10. cd ..: To go one step back from the current working directory.

  11. rm [filename]: To remove a file.

  12. rmdir [foldername]: To remove a directory/folder.

    Note: The directory should be empty.

  13. cat [filename]: To create a file and to view the content.

    cat > filename --> create a file

    cat filename --> view the content of file

    cat >> filename --> append the content of the file

  14. vim [filename]: Add content to the file

    • 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.

      Note: :wq--> Save the file

      :q--> Quit without saving the file

  1. pwd: To check the present working directory.

    If we are working in multiple directories, we can check the path with this command.

  2. cp: To copy the content of the file/directory.

    cp [source-file] [dest-file]

    Note: If the destination file is not there then it will create the file and paste the content into that particular file. For example: You can see a new file is created with "newfile.txt".

  3. mv: To move a file from a directory/file to another directory/file. In the below example a file named ‘python’ is being moved into a new file ‘newfile.txt’.

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

    We can use the "touch" command to create a "fruit.txt" file. It will create an empty file. For example:

    To view the content inside the file we can use the "cat" command.

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

    To add the fruits "Apple", "Mango", "Banana" and "Cherry" to a file called "devops.txt", with each fruit on a separate line, you can use the following commands:

    To add content to the file, you can use a text editor such as "nano" or "vim", or you can use the "echo" command to write text to the file directly from the command line. To view content inside the file "devops.txt" we can use the above "cat" command.

  6. To Show only top three fruits from the file.

    To show only the top three fruits from the "devops.txt" file, you can use the "head" command to display the first few lines of the file, like this:

    The "-n" option specifies the number of lines to display.

  7. 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 to display the last few lines of the file, like this:

  8. To create another file Color.txt. Add content in Color.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

    To create a file called "Color.txt" and add the colours "Red", "Pink", "White", "Black", "Blue", "Orange", "Purple", and "Grey" to a file called "Colors.txt", with each colour on a separate line, you can use the following commands:

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

    To find the difference between two files, you can use the "diff" command in Linux. In this case, to find the difference between the "fruits.txt" and "Colors.txt" files, you can use the following command:

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

0
Subscribe to my newsletter

Read articles from Neha pal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Neha pal
Neha pal