Basic Commands
- ls: Lists the contents of a directory.
- cd: Changes the current directory.
- cp: Copies files or directories.
- mv: Moves or renames files or directories.
- rm: Removes files or directories.
- chmod: Changes file permissions.
- chown: Changes file ownership.
- ps: Displays information about active processes.
- grep: Searches for patterns within files.
- find: Searches for files and directories.
File System
- Hierarchy: The Linux file system is organized in a hierarchical structure, starting from the root directory (/). Key directories include,
- /home (user directories)
- /etc (configuration files)
- /var (variable data)
- /usr (user programs).
- Permissions: Files and directories have permissions that determine who can read, write, or execute them. Permissions are represented by a combination of below.
- letters (r, w, x): read, write, Execute
- numbers e.g.755: 7(4+2+1) means Read(4), Write(2) and Execute(1) access.
- Ownership: Each file and directory has an owner and a group associated with it. Ownership can be changed using the chown command.
Text Editors
- vim: A powerful text editor with a steep learning curve but great for advanced editing tasks.
- nano: A simpler, user-friendly text editor suitable for beginners.
Basic Usage of ps
- ps: Running ps without any options will display a snapshot of the current processes running in the terminal session.
Common Options
- ps -e: Displays information about all processes.
- ps -f: Provides a full-format listing, including additional details like the parent process ID (PPID) and the command that started the process.
- ps -u [username]: Shows processes for a specific user.
- ps -aux: Displays all processes in a user-oriented format, including those not associated with a terminal.
Output Columns
- PID: Process ID, a unique identifier for each process.
- TTY: Terminal type associated with the process.
- TIME: Total CPU time used by the process.
- CMD: Command that started the process.
- USER: User who owns the process.
- %CPU: Percentage of CPU usage.
- %MEM: Percentage of memory usage.
Examples
- ps -ef: Displays a detailed list of all processes with full-format listing.
- ps -u root: Shows all processes owned by the root user.
- ps -aux | grep sshd: Filters the process list to show only processes related to sshd.
Advanced Usage
- ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem: Customizes the output to show specific columns and sorts the processes by memory usage in descending order.
- ps -C [command]: Displays processes based on the command name.
To kill a process using its Process ID (PID), you can use the kill command in Linux. Here are the steps:
Steps to Kill a Process
- Find the PID: First, you need to find the PID of the process you want to kill. You can use the ps command or top command to list processes and their PIDs.
- ps -aux | grep [process_name]
- Kill the Process: Once you have the PID, you can use the kill command followed by the PID to terminate the process.
- kill [PID]
- Force Kill (if necessary): If the process does not terminate with the kill command, you can forcefully kill it using the -9 signal.
- kill -9 [PID]
Example
Let's say you want to kill a process with the PID 1234:
kill 1234
If the process does not terminate, you can force kill it:
kill -9 1234
Important Signals
- SIGTERM (15): The default signal sent by kill. It requests the process to terminate gracefully.
- SIGKILL (9): Forces the process to terminate immediately. Use this as a last resort.
Using pkill and killall
- pkill: Kills processes by name.
- pkill [process_name]
- killall: Kills all processes with the specified name.
- killall [process_name]
Basic Usage of grep
- Syntax:
- grep [options] pattern [file...]
- Example: To search for the word "example" in a file named file.txt:
- grep "example" file.txt
Common Options
- -i: Ignore case distinctions.
grep -i "example" file.txt
- -r: Recursively search directories.
grep -r "example" /path/to/directory
- -v: Invert the match, showing lines that do not match the pattern.
grep -v "example" file.txt
- -n: Show line numbers with output lines.
grep -n "example" file.txt
- -l: Show only the names of files with matching lines.
grep -l "example" *.txt
Regular Expressions
grep supports regular expressions, allowing for more complex pattern matching:
- .: Matches any single character.
grep "ex.mple" file.txt
- ^: Matches the start of a line.
grep "^example" file.txt
- $: Matches the end of a line.
grep "example$" file.txt
- *: Matches zero or more of the preceding elements.
grep "ex.*ple" file.txt
Examples
- Search for a word in multiple files:
grep "example" file1.txt file2.txt
- Search for a pattern in all .log files in a directory:
grep "error" *.log
- Search for a pattern and display the count of matching lines:
grep -c "example" file.txt
Combining with Other Commands
grep can be combined with other commands using pipes:
- Search for a pattern in the output of another command:
- ps -aux | grep 'sshd'
Basic Search:
- find /path/to/directory -name "filename"
- This command searches for files with the specified name in the given directory and its subdirectories.
- Search by File Type:
- find /path/to/directory -type f -name "*.txt"
- This command searches for all text files (*.txt) in the specified directory.
- Search by Size:
- find /path/to/directory -size +100M
- This command finds files larger than 100 MB.
- Search by Modification Time:
- find /path/to/directory -mtime -7
- This command finds files modified within the last 7 days.
- Execute a Command on Found Files:
- find /path/to/directory -name "*.log" -exec rm {} \;
This command finds all log files and deletes them.
The -exec option in the find command allows you to execute a specified command on each file or directory that matches the search criteria. Here’s a breakdown of how it works:
Syntax
find /path/to/directory -name "pattern" -exec command {} \;
- /path/to/directory: The directory where the search begins.
- -name "pattern": The search criteria (e.g., file name pattern).
- -exec command {}: The command to execute on each found item. {} is a placeholder for the current file or directory.
- \;: Indicates the end of the command.
Example
Let’s say you want to find all .txt files in a directory and then print their contents using the cat command:
find /path/to/directory -name "*.txt" -exec cat {} \;
This command will search for all .txt files and display their contents.
Deleting Files
Another common use is to delete files:
find /path/to/directory -name "*.log" -exec rm {} \;
This command finds all .log files and deletes them.
Using + Instead of \;
You can also use + instead of \; to execute the command on multiple files at once, which can be more efficient:
find /path/to/directory -name "*.log" -exec rm {} +
The -exec option in the find command is versatile and can be used with a variety of commands. Here are some common ones:
- Remove Files:
find /path/to/directory -name "*.log" -exec rm {} \;
Deletes all .log files.
- Move Files:
find /path/to/directory -name "*.txt" -exec mv {} /new/directory/ \;
Moves all .txt files to a new directory.
- Change Permissions:
find /path/to/directory -type f -exec chmod 644 {} \;
Changes the permissions of all files to 644.
- Print File Details:
find /path/to/directory -name "*.sh" -exec ls -l {} \;
Lists detailed information about all .sh files.
- Compress Files:
find /path/to/directory -name "*.log" -exec gzip {} \;
Compresses all .log files using gzip.
- Count Lines in Files:
count the number of lines in each .txt file.
