Linux Commands

1. Basic Navigation Commands
  • pwd (Print Working Directory)

    • Purpose: Displays the full path of the current directory you are in.

    • Example: $PWD (returns something like /home/user/documents)

  • ls (List)

    • Purpose: Lists the contents of a directory.

    • Common Options:

    • -l: Long listing format (shows permissions, owner, group, size, date, name).

    • -a: All files (includes hidden files, which start with a dot .)

    • -h: Human-readable sizes (e.g., 1extK,234extM,2extG1 ext{K}, 234 ext{M}, 2 ext{G})

    • Example: ls -lah

  • cd (Change Directory)

    • Purpose: Changes the current working directory.

    • Common Usage:

    • cd /path/to/directory: Navigates to an absolute path.

    • cd directory_name: Navigates to a subdirectory relative to the current directory.

    • cd ..: Moves up one directory level.

    • cd: Navigates to the user's home directory.

    • cd -: Navigates to the previous working directory.

    • Example: cd ..

2. File Manipulation Commands
  • touch

    • Purpose: Creates an empty file or updates the timestamp of an existing file.

    • Example: touch newfile.txt

  • cp (Copy)

    • Purpose: Copies files or directories.

    • Common Options:

    • -r or -R: Recursive (required for copying directories).

    • -i: Interactive (prompts before overwriting).

    • Example: cp file.txt /path/to/destination/

    • Example (directory): cp -r directory/ /path/to/destination/

  • mv (Move)

    • Purpose: Moves or renames files or directories.

    • Usage: Source and destination arguments similar to cp.

    • Example: mv oldname.txt newname.txt (rename)

    • Example: mv file.txt /path/to/destination/ (move)

  • rm (Remove)

    • Purpose: Deletes files or directories.

    • Common Options:

    • -r or -R: Recursive (required for deleting non-empty directories).

    • -f: Force (deletes without prompting).

    • -i: Interactive (prompts before every deletion).

    • Example: rm oldfile.txt

    • Example (directory): rm -r olddirectory/

3. Directory Manipulation Commands
  • mkdir (Make Directory)

    • Purpose: Creates a new directory.

    • Common Options:

    • -p: Parents (creates parent directories as needed).

    • Example: mkdir new_folder

    • Example: mkdir -p project/src/main

  • rmdir (Remove Directory)

    • Purpose: Deletes an empty directory.

    • Note: rm -r is typically used for non-empty directories.

    • Example: rmdir empty_folder

4. Viewing File Content
  • cat (Concatenate)

    • Purpose: Displays the content of a file to standard output.

    • Example: cat document.txt

  • less

    • Purpose: Allows viewing file content one screen at a time (scrollable).

    • Usage: Press q to quit, arrow keys to scroll.

    • Example: less large_log_file.txt

  • head

    • Purpose: Displays the beginning lines of a file (default: 1010 lines).

    • Common Options:

    • -n COUNT: Displays the first COUNT lines.

    • Example: head -n 5 file.txt

  • tail

    • Purpose: Displays the ending lines of a file (default: 1010 lines).

    • Common Options:

    • -n COUNT: Displays the last COUNT lines.

    • -f: Follows (monitors) the file for new content (useful for logs).

    • Example: tail -n 5 file.txt

    • Example: tail -f /var/log/syslog

5. User and Permissions
  • chmod (Change Mode)

    • Purpose: Changes file permissions.

    • Permissions: Read (r=4r=4), Write (w=2w=2), Execute (x=1x=1)

    • Categories: User (uu), Group (gg), Others (oo), All (aa)

    • Symbolic Mode: chmod u+x filename (add execute for user)

    • Octal/Numeric Mode: Sum of permissions for each category (rwx=7,rw−=6,r−x=5rwx = 7, rw- = 6, r-x = 5 etc.)

    • Example: chmod 755 script.sh (user: rwx, group: r-x, others: r-x)

    • Example: chmod go-w file.txt (remove write for group and others)

  • chown (Change Owner)

    • Purpose: Changes the owner of a file or directory.

    • Example: chown user filename

    • Example (with group): chown user:group filename

  • sudo (Substitute User Do)

    • Purpose: Allows a permitted user to execute a command as the superuser or another user.

    • Example: sudo apt update (requires password for elevated privileges)

  • whoami

    • Purpose: Displays the current user's username.

    • Example: whoami

  • man (Manual)

    • Purpose: Displays the manual page for a command.

    • Usage: Press q to quit.

    • Example: man ls