Linux Basic commands and examples

Linux Basic commands and examples

  1. ls:

    • Description: The ls command is used to list files and directories in a directory.

    • Syntax: ls [options] [file/directory]

    • Example:

      • List all files and directories in the current directory:

          $ ls
          file1.txt  file2.txt  directory1
        
      • List files and directories in a specific directory:

          $ ls /path/to/directory
          file1.txt  file2.txt  directory1
        
  2. cat:

    • Description: The cat command is used to concatenate and display the contents of files.

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

    • Example:

      • Display the contents of a file:

          $ cat filename.txt
          This is the contents of the file.
        
      • Concatenate multiple files and display their contents:

          $ cat file1.txt file2.txt
          Contents of file1.txt...
          Contents of file2.txt...
        
  3. find:

    • Description: The find command is used to search for files and directories based on specified criteria.

    • Syntax: find [path] [expression]

    • Example:

      • Find all files with a specific name in the current directory:

          $ find . -name "filename.txt"
          ./directory1/filename.txt
        
      • Find all directories modified in the last 7 days:

          $ find /path/to/directory -type d -mtime -7
          /path/to/directory/directory1
        
  4. grep:

    • Description: The grep command is used to search for a specified pattern in files.

    • Syntax: grep [options] pattern [file(s)]

    • Example:

      • Search for a word in a file:

          $ grep "word" filename.txt
          This line contains the word.
        
      • Search for a pattern recursively in a directory:

          $ grep -r "pattern" /path/to/directory
          /path/to/directory/file1.txt: This line matches the pattern.
          /path/to/directory/file2.txt: Another line that matches the pattern.
        
  5. cut:

    • Description: The cut command is used to extract specific fields or columns from a file.

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

    • Example:

      • Extract the first column from a CSV file:

          $ cut -d ',' -f 1 filename.csv
          Column1
          Data1
          Data2
        
      • Extract a range of characters from a file:

          $ cut -c 1-5 filename.txt
          This
          is
          a
          test
        
  6. sort:

    • Description: The sort command is used to sort lines of text in a file.

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

    • Example:

      • Sort lines in a file in ascending order:

          $ sort filename.txt
          apple
          banana
          cherry
        
      • Sort lines numerically in descending order:

          $ sort -n -r numbers.txt
          100
          50
          10
        
  7. echo:

    • Description: The echo command is used to print arguments or text to the terminal.

    • Syntax: echo [options] [text]

    • Example:

      • Print a message to the terminal:

          $ echo "Hello, world!"
          Hello, world!
        
      • Print the value of a variable:

          $ variable="Value"
          $ echo $variable
          Value
        
  8. head:

    • Description: The head command is used to output the beginning lines of a file.

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

    • Example:

      • Display the first 10 lines of a file:

          $ head filename.txt
          Line 1
          Line 2
          Line 3
          ...
          Line 10
        
      • Display a specific number of lines from a file:

          $ head -n 5 filename.txt
          Line 1
          Line 2
          Line 3
          Line 4
          Line 5
        
  9. tail:

    • Description: The tail command is used to output the ending lines of a file.

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

    • Example:

      • Display the last 10 lines of a file:

          $ tail filename.txt
          Line 91
          Line 92
          Line 93
          ...
          Line 100
        
      • Continuously display new lines added to a file:

          $ tail -f filename.log
          Line 1
          Line 2
          Line 3
          ... (new lines will be displayed in real-time)