Skip to main content

Command Palette

Search for a command to run...

"DevOps Odyssey": Day2.1 - Basic Linux Commands

Updated
4 min read
"DevOps Odyssey": Day2.1 - Basic Linux 
   Commands
  1. Linux:

    • Linux is an open-source operating system that originated from Unix.

    • It provides a robust and stable platform for running various applications and services.

    • Linux is highly customizable and offers a wide range of distributions (e.g., Ubuntu, CentOS, Fedora) to suit different needs.

  2. Shell:

    • A shell is a command-line interpreter that acts as a user interface to interact with the operating system.

    • The shell processes commands and executes programs or scripts.

    • In Linux, the default shell is typically Bash (Bourne Again Shell), but there are others like Zsh and Fish.

  3. Shell Scripting:

    • Shell scripting involves writing a series of commands in a file (script) that the shell can execute.

    • Shell scripts are written in plain text using a text editor.

    • They can automate repetitive tasks, execute system commands, and combine multiple commands into complex workflows.

  4. Script Execution:

    • To execute a shell script, you need to make it executable using the chmod command (e.g., chmod +x script.sh).

    • Run a shell script by entering its filename or path in the terminal (e.g., ./script.sh).

  5. Variables:

    • Shell scripts use variables to store data. Variable names are case-sensitive and typically uppercase by convention.

    • You can assign values to variables using the assignment operator (e.g., name="John").

    • Access the value of a variable using the $ symbol followed by the variable name (e.g., $name).

  6. Control Structures:

    • Shell scripts support control structures like conditional statements (if-else) and loops (for, while).

    • Conditional statements help make decisions based on certain conditions.

    • Loops allow executing a set of commands repeatedly until a condition is met.

  7. Input/Output:

    • Shell scripts can read input from users using the read command.

    • Output can be displayed on the terminal using the echo command.

    • You can redirect input/output using special characters, such as > (output to a file) and | (pipe output to another command).

  8. Command Substitution:

    • Command substitution allows capturing the output of a command and using it as a value.

    • It can be done using the $() syntax or backticks () (e.g., result=$(command) or result=command).

  9. Functions:

    • Shell scripts can define functions to group a set of commands and reuse them.

    • Functions are declared using the function keyword or just the function name.

    • They can accept arguments and return values.

  10. Debugging:

    • You can enable debugging mode in a shell script by adding set -x at the beginning.

    • Debugging mode displays each executed command, which helps in troubleshooting issues.

Basic Commands

Listing Commands

  • ls - List the subdirectories and files available in the present working directory.

  • ls -l - List the files and directories in long list format with extra information, such as permissions, owner, group, size, and modification date.

  • ls -a - List all files and directories, including hidden files and directories (those whose names begin with a dot '.')

  • ls *.sh - List all the files having a .sh extension in the present working directory.

  • ls -i - List the files and directories along with their index numbers (inodes).

  • ls -d */ - List only directories in the present working directory, using a specific pattern to filter them.

Directory Commands

  • pwd - Print the present working directory, i.e., the directory you are currently in.

  • cd path_to_directory - Change the current directory to the specified path.

  • cd ~ or just cd - Change the current directory to the home directory.

  • cd - - Go to the last working directory (the directory you were in before the last cd command).

  • cd .. - Move up one level in the directory hierarchy (change to the parent directory).

  • cd ../.. - Move up two levels in the directory hierarchy (change to the grandparent directory).

  • mkdir directoryName - Make a new directory with the given name in the present working directory.

  • mkdir .NewFolder - Make a hidden directory (beginning with a dot) in the present working directory.

  • mkdir A B C D - Make multiple directories with the names 'A', 'B', 'C', and 'D' in the present working directory.

  • mkdir /home/user/Mydirectory - Make a new directory named 'Mydirectory' in the specified location (/home/user/ in this example).

  • mkdir -p A/B/C/D - Make a nested directory structure (create directories 'A', 'A/B', 'A/B/C', and 'A/B/C/D') even if the parent directories don't exist.

💡
We have covered the basics of Linux, exploring its various applications across diverse domains. As we delve deeper into the world of Linux in our upcoming blogs, we hope to enhance our understanding and proficiency with this powerful operating system. Thank you for joining me on this journey, and until next time, goodbye and happy learning!
S

Very informative, thanks for sharing! Do check out my blogs on these topics too😄