top of page

Linux commands

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Dec 10, 2024
  • 2 min read

1. cd (Change Directory)

  • Usage: cd [directory_path]

  • Function: Changes the current working directory.

  • Examples:

    • cd /home/user → Moves to the /home/user directory.

    • cd .. → Moves up one directory level.

    • cd → Moves to the user's home directory.


2. mkdir (Make Directory)

  • Usage: mkdir [directory_name]

  • Function: Creates a new directory.

  • Examples:

    • mkdir new_folder → Creates a directory named new_folder.

    • mkdir -p dir1/dir2 → Creates a nested directory structure.


3. rm (Remove Files/Directories)

  • Usage: rm [file_or_directory_name]

  • Function: Removes files or directories.

  • Examples:

    • rm file.txt → Deletes the file file.txt.

    • rm -r folder → Deletes the directory folder and its contents.

    • rm -i file.txt → Prompts for confirmation before deletion.


4. mv (Move/Rename Files)

  • Usage: mv [source] [destination]

  • Function: Moves or renames files or directories.

  • Examples:

    • mv file.txt /tmp/ → Moves file.txt to the /tmp/ directory.

    • mv old_name.txt new_name.txt → Renames the file.


5. cp (Copy Files)

  • Usage: cp [source] [destination]

  • Function: Copies files or directories.

  • Examples:

    • cp file.txt /tmp/ → Copies file.txt to /tmp/.

    • cp -r dir1 dir2 → Copies the directory dir1 to dir2.


6. ls (List Files)

  • Usage: ls [options] [directory]

  • Function: Lists the contents of a directory.

  • Examples:

    • ls → Lists files in the current directory.

    • ls -l → Lists files in a long format with details.

    • ls -a → Lists all files, including hidden ones.


7. cat (Concatenate and View Files)

  • Usage: cat [file_name]

  • Function: Displays the contents of a file, or concatenates files.

  • Examples:

    • cat file.txt → Displays the contents of file.txt.

    • cat file1.txt file2.txt > combined.txt → Combines two files into combined.txt.


8. find (Search for Files/Directories)

  • Usage: find [path] [options]

  • Function: Searches for files and directories in a specified path.

  • Examples:

    • find / -name "file.txt" → Searches for file.txt from the root directory.

    • find . -type d → Finds directories in the current location.


9. grep (Search Text in Files)

  • Usage: grep [pattern] [file_name]

  • Function: Searches for a specific pattern of text in files.

  • Examples:

    • grep "hello" file.txt → Searches for the word hello in file.txt.

    • grep -r "function" /path/to/code → Searches recursively for function in a directory.

  • Examples:

    • grep "error" logfile.txt: Searches for the word error in logfile.txt.

    • grep -i "warning" logfile.txt: Case-insensitive search for warning.

    • grep -r "TODO" /project/: Recursively searches for TODO in /project/.





 
 
 

Comments


bottom of page