Common Commands with examples
Using these commands in various scenarios to become proficient in basic command-line tasks, file management, and system administration.
ls - List directory contents
The ls command is used to list files and directories in a specified location. It has many options to customize the output.
Examples:
- ls -l: Long listing format, showing permissions, owner, size, and modification date
- ls -a: Show all files, including hidden ones (those starting with a dot)
- ls -R: Recursively list subdirectories
- ls -lh: Use human-readable file sizes (e.g., 1K, 234M, 2G)
- ls /etc: List contents of the /etc directory
cd - Change directory
cd is used to navigate the file system. It can use absolute or relative paths.
Examples:
- cd /home/user: Change to the /home/user directory (absolute path)
- cd ..: Move up one directory level
- cd -: Return to the previous directory
- cd ~: Change to the user's home directory
- cd Documents/Projects: Change to a subdirectory (relative path)
pwd - Print working directory
pwd displays the full path of the current working directory.
Examples:
- pwd: Outputs something like /home/user/Documents
- pwd -P: Print the physical directory, without any symbolic links
mkdir - Create a new directory
mkdir is used to create one or more directories.
Examples:
- mkdir newdir: Create a directory named "newdir"
- mkdir -p parent/child/grandchild: Create nested directories
- mkdir dir1 dir2 dir3: Create multiple directories at once
- mkdir -m 755 secured_dir: Create a directory with specific permissions
touch - Create an empty file or update file timestamps
touch can create empty files or update the access and modification times of existing files.
Examples:
- touch newfile.txt: Create a new empty file or update timestamps if it exists
- touch -t 202502191200 file.txt: Set specific timestamp (YYYYMMDDhhmm)
- touch -r reference_file target_file: Use timestamps from reference_file
cp - Copy files and directories
cp is used to copy files and directories from one location to another.
Examples:
- cp file.txt /home/user/backup/: Copy a file to another directory
- cp -r dir1 dir2: Copy a directory and its contents recursively
- cp -i *.txt /destination/: Copy all .txt files, prompting before overwrite
- cp -p source dest: Preserve file attributes (mode, ownership, timestamps)
mv - Move or rename files and directories
mv can move files/directories or rename them.
Examples:
- mv file.txt newname.txt: Rename a file
- mv /home/user/doc.txt /tmp/: Move a file to another directory
- mv dir1 dir2: Rename a directory (if dir2 doesn't exist) or move dir1 into dir2
- mv -i *.jpg /home/user/Pictures/: Move all .jpg files, prompting before overwrite
rm - Remove files or directories
rm is used to delete files and directories. Use with caution!
Examples:
- rm file.txt: Delete a file
- rm -r directory/: Recursively remove a directory and its contents
- rm -f unwanted_file: Force removal without prompting
- rm -i *.log: Remove .log files with confirmation for each
rmdir - Remove empty directories
rmdir is used to remove empty directories.
Examples:
- rmdir empty_folder: Remove an empty directory
- rmdir -p parent/child/grandchild: Remove nested empty directories
shutdown - Shut down or restart the system
The shutdown command is used to safely bring down or restart a Linux system, allowing for scheduled shutdowns and custom messages to users.
Examples:
- shutdown now: Shut down the system immediately
- shutdown -r +10: Reboot the system in 10 minutes
- shutdown -h 23:30: Schedule a shutdown at 11:30 PM
- shutdown -c: Cancel a scheduled shutdown
- shutdown +15 "System maintenance in 15 minutes": Schedule a shutdown with a custom message
- shutdown -P now: Power off the system immediately
halt - Stop all CPU functions
The halt command stops all CPU functions and brings the system to a halt state. It's similar to shutdown but may not power off the system on all hardware.
Examples:
- halt: Halt the system
- halt -p: Halt the system and power off
- halt -f: Force halt without calling shutdown
- halt -w: Don't actually halt, just write a wtmp record
reboot - Restart the system
The reboot command restarts the system, similar to shutdown -r but more direct.
Examples:
- reboot: Reboot the system
- reboot -f: Force an immediate reboot without calling shutdown
- reboot -w: Don't actually reboot, just write a wtmp record
- reboot --no-wall: Don't send wall message before rebooting
poweroff - Shut down and power off the system
The poweroff command shuts down the system and turns off the power. It's equivalent to shutdown -P.
Examples:
- poweroff: Shut down and power off the system
- poweroff -f: Force power off without calling shutdown
- poweroff -w: Don't actually power off, just write a wtmp record
- poweroff --no-wall: Don't send wall message before powering off
set: Displays all shell variables and functions.
Example: set | grep PATH
- unset: Removes an environment variable15.
Example: unset GNUPLOT_DRIVER_DIR
- export: Sets an environment variable for the current shell and all its child processes36.
Example: export VAR="value"
- env: Displays all environment variables6.
Example: env
- echo: Prints the value of a specific environment variable3.
Example: echo $VAR
- Quotes are used when setting variables containing spaces or special characters6:
Example: export GREETING="Hello, World!"
To set a temporary environment variable:
- VAR="value"
- To make it persistent, add it to ~/.bashrc or /etc/environment47.
To unset a variable, use:
- unset VAR
- export VAR=
history - Command to view and manipulate command history
The history command allows users to view, search, and manipulate the list of commands previously executed in the shell.
Examples:
- history: Display the entire command history
- history 10: Show the last 10 commands
- history | grep ssh: Search for all ssh-related commands in history
- !42: Execute the command at line 42 in the history
- !-3: Execute the third-to-last command
- !!: Execute the last command again
.bash_history - File storing command history
.bash_history is a hidden file in the user's home directory that stores the command history across sessions.
Examples:
- cat ~/.bash_history: View the contents of the history file
- echo "export HISTSIZE=10000" >> ~/.bashrc: Increase history size
- echo "export HISTCONTROL=ignoredups" >> ~/.bashrc: Avoid duplicate entries
- echo "export HISTIGNORE='ls:bg:fg:history'" >> ~/.bashrc: Ignore certain commands
Additional history-related features:
- Ctrl+R: Reverse search through command history
- !string: Execute the most recent command starting with "string"
- history -c: Clear the current session history
- history -d 50: Delete line 50 from history
- export HISTTIMEFORMAT="%F %T ": Add timestamps to history entries
bash
Bourne Again Shell, the default command-line interpreter for most Linux distributions
Examples:
- bash script.sh: Execute a bash script
- bash -c "echo Hello, World": Run a bash command directly
- bash --version: Display bash version information
. (dot)
Represents the current directory in file paths
Examples:
- ls .: List contents of the current directory
- cp file.txt .: Copy file.txt to the current directory
- ./script.sh: Execute a script in the current directory
Relative path
Specifies the location of a file or directory relative to the current working directory
Examples:
- cd documents: Change to the documents subdirectory
- ../images/photo.jpg: Reference a file in a sibling directory
- ./config.ini: Reference a file in the current directory
Absolute path
Specifies the complete path to a file or directory from the root of the file system
Examples:
- /home/user/documents/report.pdf: Full path to a file
- /etc/ssh/sshd_config: Path to a system configuration file
- /var/log/syslog: Path to a system log file