> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/linux-commands/basic-commands.md).

# Basic Commands

## **`whoami`** :- Show the Current Logged-In User

**Examples:**

* Display the username of the currently logged-in user:

  ```bash
  whoami
  ```

## `pwd` :- Print Working Directory

The `pwd` (Print Working Directory) command displays the full path of the current directory you are in.

* Show the full path of the current directory:

  ```bash
  pwd
  ```
* Show the actual physical path, resolving any symbolic links:

  ```bash
  pwd -P
  ```

***

## `ls` :- List files and directories

The `ls` command is used to display the files and directories within a directory.

**Basic Usage:**

* List all files and directories in the current directory:

  ```bash
  ls
  ```

**Listing with Details:**

* Show all files, including hidden files:

  ```bash
  ls -a
  ```
* Display files in a long format (permissions, owner, size, modification date):

  ```bash
  ls -l
  ```
* Combine both options to show all files with detailed information:

  ```bash
  ls -la
  ```

**Human-Readable Format:**

* Show file sizes in KB, MB, or GB instead of bytes:

  ```bash
  ls -lh
  ```

**Sorting Files:**

* Sort by modification time (newest first):

  ```bash
  ls -lt
  ```
* Sort by file size (largest first):

  ```bash
  ls -lS
  ```

**Recursive Listing:**

* List all files and subdirectories recursively:

  ```bash
  ls -R
  ```

***

## `cd` :- Change Directory

The `cd` command is used to navigate between directories in Linux.

**Basic Usage:**

* Move into a specific directory:

  ```bash
  cd /home/user/Documents
  ```
* Go to your home directory:

  ```bash
  cd ~
  ```
* Move up one directory (go back):

  ```bash
  cd ..
  ```
* Move up multiple levels (e.g., go back two directories):

  ```bash
  cd ../../
  ```

**Navigating with Absolute & Relative Paths:**

* **Absolute Path:** (Full path from the root `/`)

  ```bash
  cd /var/log/
  ```
* **Relative Path:** (Path from the current directory)

  ```bash
  cd logs/
  ```

**Using Special Characters for Navigation:**

* Go to the previous directory (toggle between two directories):

  ```bash
  cd -
  ```
* Navigate to the root directory:

  ```bash
  cd /
  ```

**Handling Directory Names with Spaces:**

* Use quotes:

  ```bash
  cd "My Documents"
  ```
* Use backslash `\` to escape spaces:

  ```bash
  cd My\ Documents/
  ```

***

## `mkdir` :- Make Directories

The `mkdir` command is used to create new directories (folders).

**Basic Usage:**

* Create a single directory:

  ```bash
  mkdir new_folder
  ```
* Create multiple directories at once:

  ```bash
  mkdir folder1 folder2 folder3
  ```

**Creating Nested Directories:**

* Create directories inside another directory using `-p`:

  ```bash
  mkdir -p parent_folder/child_folder/grandchild_folder
  ```

  *The `-p` flag ensures that parent directories are created if they don’t exist.*

***

## `rm` :- Remove Files and Directories

The `rm` command is used to delete files or directories.

**Removing Files:**

* Remove a single file:

  ```bash
  rm file.txt
  ```
* Remove multiple files:

  ```bash
  rm file1.txt file2.txt
  ```

**Removing Directories:**

* Remove an empty directory:

  ```bash
  rm -d empty_folder
  ```
* Remove a non-empty directory and its contents recursively:

  ```bash
  rm -r folder_name
  ```
* Force delete a directory without confirmation:

  ```bash
  rm -rf folder_name
  ```

***

## `rmdir` :- Remove Empty Directories

The `rmdir` command deletes empty directories only.

**Basic Usage:**

* Remove an empty directory:

  ```bash
  rmdir empty_folder
  ```
* Remove multiple empty directories at once:

  ```bash
  rmdir dir1 dir2 dir3
  ```

**Using `rmdir` with Parent Directories:**

* Remove a directory and its parent (if empty):

  ```bash
  rmdir -p parent_folder/child_folder
  ```

  *This removes `child_folder` first, then `parent_folder` if it is also empty.*

***

## `touch` :- Create Empty Files and Update Timestamps

The `touch` command is used to create new empty files and update timestamps of existing files.

**Basic Usage:**

* Create a new empty file:

  ```bash
  touch newfile.txt
  ```
* Create multiple files at once:

  ```bash
  touch file1.txt file2.txt file3.txt
  ```
* Update the timestamp of an existing file (without modifying content):

  ```bash
  touch existing_file.txt
  ```
* Create a file with spaces in its name:

  ```bash
  touch "my new file.txt"
  ```

***

## `cat` :- View, Create, and Concatenate Files

The `cat` command is used to display file content, create files, and join multiple files together.

**Viewing File Content:**

* Display the contents of a file:

  ```bash
  cat file.txt
  ```
* View multiple files together:

  ```bash
  cat file1.txt file2.txt
  ```

**Creating and Writing to a File:**

* Create a file and start writing:

  ```bash
  cat > newfile.txt
  ```

  *Type content and press `Ctrl + D` to save.*

**Appending Content to a File:**

* Add text to an existing file:

  ```bash
  cat >> existingfile.txt
  ```

  *Type content and press `Ctrl + D` to save.*

***

## `cp` :- Copy Files and Directories

The `cp` command is used to copy files and directories.

**Copying Files:**

* Copy a file to another location:

  ```bash
  cp file.txt /destination_folder/
  ```
* Copy and rename the file:

  ```bash
  cp file.txt newfile.txt
  ```

**Copying Multiple Files:**

* Copy multiple files to a directory:

  ```bash
  cp file1.txt file2.txt /destination_folder/
  ```

**Copying Directories:**

* Copy a directory and its contents recursively:

  ```bash
  cp -r source_directory destination_directory
  ```

***

## `mv` :- Move and Rename Files

The `mv` command is used to move or rename files and directories.

**Moving Files:**

* Move a file to another location:

  ```bash
  mv file.txt /destination_folder/
  ```
* Move multiple files to a directory:

  ```bash
  mv file1.txt file2.txt /destination_folder/
  ```

**Renaming Files and Directories:**

* Rename a file:

  ```bash
  mv oldfile.txt newfile.txt
  ```
* Rename a directory:

  ```bash
  mv old_directory new_directory
  ```

***

## `ln` :- Create Hard and Symbolic Links

The `ln` command is used to create links between files.

**Creating Hard Links:**

* Create a hard link:

  ```bash
  ln source_file hard_link_file
  ```

  *Hard links share the same inode, meaning changes affect both files.*

**Creating Symbolic (Soft) Links:**

* Create a symbolic (soft) link:

  ```bash
  ln -s source_file soft_link_file
  ```

  *Soft links point to the original file but have a different inode.*

***

## **Redirection in Linux**

#### `>` :- Redirect Output to a File (Overwrite)

* Save the output of a command to a file (overwrite if exists):

  ```bash
  ls > filelist.txt
  ```
* Create a new file and write text:

  ```bash
  echo "Hello, Linux!" > hello.txt
  ```

  *This will replace the content of `hello.txt`.*

***

#### `>>` :- Append Output to a File

* Append text to an existing file (without overwriting):

  ```bash
  echo "Appending text" >> hello.txt
  ```
* Append command output to a log file:

  ```bash
  date >> system.log
  ```

***

#### `2>` :- Redirect Error Messages

* Save error messages to a file:

  ```bash
  ls nonexistent_folder 2> error.log
  ```
* Discard errors (send to `/dev/null`):

  ```bash
  ls nonexistent_folder 2> /dev/null
  ```

***

#### `<` :- Use a File as Input

* Read input from a file instead of keyboard:

  ```bash
  wc -l < file.txt
  ```

  *This counts the number of lines in `file.txt`.*

***

#### `|` :- Pipe Output to Another Command

* Send the output of one command as input to another:

  ```bash
  ls -l | grep ".txt"
  ```

  *This lists only `.txt` files in the current directory.*
* Count the number of files:

  ```bash
  ls | wc -l
  ```

***

## **`uname`** :- Show System Information

**Examples:**

* Display system name (Linux, Unix, etc.):

  ```bash
  uname
  ```
* Show all system details:

  ```bash
  uname -a
  ```
* Display the machine hardware name:

  ```bash
  uname -m
  ```
* Show only the operating system:

  ```bash
  uname -o
  ```

***

## **`hostname`** :- Show or Set System Hostname

**Examples:**

* Display the system's hostname:

  ```bash
  hostname
  ```
* Show the system's fully qualified domain name (FQDN):

  ```bash
  hostname -f
  ```
* Set a new hostname (requires root privileges):

  ```bash
  sudo hostname new-hostname
  ```

***

## **`id`** :- Show User and Group ID Information

**Examples:**

* Display the current user’s UID and GID:

  ```bash
  id
  ```
* Check the user ID for a specific user:

  ```bash
  id username
  ```
* Show only the user ID:

  ```bash
  id -u
  ```
* Show only the group ID:

  ```bash
  id -g
  ```

***

## **`echo`** :- Print Text or Variables

**Examples:**

* Print a simple message:

  ```bash
  echo "Hello, World!"
  ```
* Print environment variables:

  ```bash
  echo $HOME
  ```
* Print text with newline escape characters:

  ```bash
  echo -e "Hello\nLinux!"
  ```
* Save output to a file:

  ```bash
  echo "Linux is great!" > output.txt
  ```

***

## **`man`** :- Show the Manual for a Command

**Examples:**

* Display the manual for `ls`:

  ```bash
  man ls
  ```
* Search for a keyword in manual pages:

  ```bash
  man -k memory
  ```

***

## **`info`** :- Show Detailed Documentation

**Examples:**

* Get more details about `ls`:

  ```bash
  info ls
  ```

***

## **`help`** :- Show Built-in Help for a Command

**Examples:**

* Get help for `echo`:

  ```bash
  help echo
  ```
* List all built-in commands:

  ```bash
  help
  ```

## **`date`** :- Show the Current Date and Time

**Examples:**

* Display the current date and time:

  ```bash
  date
  ```
* Show date in a specific format:

  ```bash
  date +"%Y-%m-%d %H:%M:%S"
  ```
* Show yesterday’s date:

  ```bash
  date -d "yesterday"
  ```

***

## **`time`** :- Measure Execution Time of a Command

**Examples:**

* Measure the time taken to list files:

  ```bash
  time ls
  ```
* Measure execution time of a script:

  ```bash
  time ./script.sh
  ```

***

## **`cal`** :- Display a Calendar

**Examples:**

* Show the current month’s calendar:

  ```bash
  cal
  ```
* Show the calendar for a specific year:

  ```bash
  cal 2025
  ```
* Display a calendar with weeks starting on Monday:

  ```bash
  cal -m
  ```

## **`lscpu`** :- Show CPU Information

**Examples:**

* Display CPU details:

  ```bash
  lscpu
  ```
* Check the number of CPU cores:

  ```bash
  lscpu | grep "CPU(s)"
  ```

***

## **`lsusb`** :- Show Connected USB Devices

**Examples:**

* List all USB devices:

  ```bash
  lsusb
  ```
* Get detailed USB device information:

  ```bash
  lsusb -v
  ```

***

## **`df`** :- Show Disk Usage

**Examples:**

* Display disk usage in human-readable format:

  ```bash
  df -h
  ```
* Show disk usage for a specific mount point:

  ```bash
  df -h /home
  ```
* Show inode usage instead of disk space:

  ```bash
  df -i
  ```

***

## **`shutdown`** :- Power Off the System

**Examples:**

* Shut down the system immediately:

  ```bash
  sudo shutdown now
  ```
* Shut down after 10 minutes:

  ```bash
  sudo shutdown +10
  ```
* Schedule shutdown at a specific time:

  ```bash
  sudo shutdown 22:30
  ```
* Cancel a scheduled shutdown:

  ```bash
  sudo shutdown -c
  ```

***

## **`reboot`** :- Restart the System

**Examples:**

* Reboot immediately:

  ```bash
  sudo reboot
  ```
* Schedule a reboot in 10 minutes:

  ```bash
  sudo shutdown -r +1
  ```
