File Operations: Copy, Move, and Delete
In Linux systems, the fundamental commands for file manipulation are cp (copy), mv (move), and rm (remove). ### Copy Command (cp)
The cp command follows this syntax: ```
cp [-adfilprsu] source_file destination_file
cp [options] source1 source2 source3 ... directory
Key parameters include: - `-a`: Archive mode, preserves all directory attirbutes
- `-d`: Copies symbolic links as links rather than the files they point to
- `-f`: Force mode, overwrites existing files without prompting
- `-i`: Interactive mode, prompts before overwriting existing files
- `-l`: Creates hard links instead of copying files
- `-p`: Preserves file attributes and timestamps
- `-r`: Recursive copy, used for directories
- `-s`: Creates symbolic links (shortcuts)
- `-u`: Updates only if the destination file is older than the source
Example: To copy `/data/source.txt` to `/backup/destination.txt`: ```
cp /data/source.txt /backup/destination.txt
Move Command (mv)
The mv command syntax is: ```
mv [-fiv] source destination
Parameters: - `-f`: Force mode, moves files without prompting
- `-i`: Interactive mode, prompts before overwriting existing files
- `-u`: Updates only if the destination file is older than the source
Example: To move `/home/user/docs/report.pdf` to `/shared/reports/`: ```
mv /home/user/docs/report.pdf /shared/reports/
Delete Command (rm)
The rm command syntax: ```
rm [-fir] files_or_directories
Parameters: - `-f`: Force mode, deletes without confirmation
- `-i`: Interactive mode, prompts before each deletion
- `-r`: Recursive mode, used for deleting directories and their contents
Example: To remove `/tmp/old_files` directory: ```
rm -rf /tmp/old_files
Firewall Configuration
Linux systems use firewalld as the default firewall management tool. Key commands include: ### Firewalld Service Management
# Check firewall status
systemctl status firewalld
# Install firewalld if not present
yum install firewalld
# Start the firewall service
systemctl start firewalld.service
# Stop the firewall service
systemctl stop firewalld.service
# Restart the firewall service
systemctl restart firewalld.service
# Disable firewall at boot
systemctl disable firewalld.service
# Enable firewall at boot
systemctl enable firewalld.service
# Check if service is enabled at boot
systemctl is-enabled firewalld.service
Firewall Rule Management
# Check firewall version
firewall-cmd --version
# Display firewall status
firewall-cmd --state
# List all open ports
firewall-cmd --zone=public --list-ports
# Add a permanent port rule (example: port 80/tcp)
firewall-cmd --zone=public --add-port=80/tcp --permanent
# Remove a port rule
firewall-cmd --zone=public --remove-port=80/tcp --permanent
# Reload firewall rules
firewall-cmd --reload
# View active zones
firewall-cmd --get-active-zones
# Check interface zone assignment
firewall-cmd --get-zone-of-interface=eth0
# Enable panic mode (blocks all traffic)
firewall-cmd --panic-on
# Disable panic mode
firewall-cmd --panic-off
# Check panic mode status
firewall-cmd --query-panic
File Transfer Operations
For file transfer between Linux and Windows systems, install the lrzsz package: ```
yum install -y lrzsz
Key commands: - `rz`: Upload files from Windows to Linux
- `sz filename`: Download files from Linux to Windows
Archive and Compression
-----------------------
The `tar` command is used for creating and extracting archives. Common parameters include: - `-c`: Create a new archive
- `-x`: Extract files from an archive
- `-t`: List archive contents
- `-z`: Compress/decompress with gzip
- `-j`: Compress/decompress with bzip2
- `-v`: Verbose mode (show files being processed)
- `-f`: Specify archive filename
### Creating Archives
Create a simple tar archive
tar -cvf /backup/etc.tar /etc
Create a gzipped tar archive
tar -zcvf /backup/etc.tar.gz /etc
Create a bzip2 compressed tar archive
tar -jcvf /backup/etc.tar.bz2 /etc
Create a compress compressed tar archive
tar -Zcvf /backup/etc.tar.Z /etc
### Extracting Archives
Extract a tar archive
tar -xvf archive.tar
Extract a gzipped tar archive
tar -xzvf archive.tar.gz
Extract a bzip2 compressed tar archive
tar -xjvf archive.tar.bz2
Extract a compress compressed tar archive
tar -xZvf archive.tar.Z
Extract RAR archives
unrar e archive.rar
Extract ZIP archives
unzip archive.zip
### Viewing Archive Contents
List contents of a tar archive
tar -tvf archive.tar
List contents of a gzipped tar archive
tar -ztvf archive.tar.gz
List contents of a bzip2 compressed tar archive
tar -jtvf archive.tar.bz2
List contents of a compress compressed tar archive
tar -Ztvf archive.tar.Z
File Linking Operations
-----------------------
Linux supports two types of file links: symbolic (soft) links and hard links. ### Symbolic Links
Symbolic links are created with the `-s` option. They act as pointers to the original file: ```
ln -s /usr/bin/python3 /usr/bin/python
Symbolic links: - Take minimal disk space
- Can link across different filesystems
- Can link to directories
- Can become "dangling" if the originla file is deleted
Hard Links
Hard links are created without the -s option: ```
ln /usr/bin/python3 /usr/bin/python_hard
Hard links: - Take the same amount of space as the original file
- Cannot link across different filesystems
- Cannot link to directories
- Directly reference the inode of the original file
Both types of links maintain synchronization with the original file - changes to either the link or the original file are reflected in all linked instances. File Searching
--------------
The `find` command is used to search for files based on various criteria: ```
find /path/to/search -criteria [value]
Common search criteria: - -type f: Search for files
-type d: Search for directories-name pattern: Search by name (case-sensitive)-iname pattern: Search by name (case-insensitive)-size +100M: Search for files larger than 100MB-size -100M: Search for files smaller than 100MB-perm 755: Search for files with specific permissions
Example searches: ```
Find all .conf files in the system
find / -type f -name "*.conf"
Find all directories larger than 100MB
find / -type d -size +100M
Find files with specific permissions
find /tmp -perm 755
Find files and display details
find /var -size +100M -ls
Process Management
------------------
### Process Listing with ps
The `ps` command displays currently running processes: ```
# List all processes
ps -ef
# Filter processes (example: find Java processes)
ps -ef | grep java
# Find top 10 processes by memory usage
ps -auxf | sort -nr -k 4 | head -10
# Find top 10 processes by CPU usage
ps -auxf | sort -nr -k 3 | head -10
Java Process Management with jps
The jps command lists Java processes: ```
List Java PIDs and main class names
jps
List Java PIDs with full class names
jps -l
List Java PIDs with main classes and arguments
jps -lm
List Java PIDs with JVM arguments
jps -v
List Java processes with full details (similar to ps -ef | grep java)
jps -lvm
Log File Analysis
-----------------
Linux provides several tools for examining log files: ```
# Search for specific content in logs
grep 'error' /var/log/syslog
# View the last 100 lines of a log file and follow new entries
tail -f /var/log/application.log
# View the last 100 lines of a log file
tail -100 /var/log/application.log
# Search in compressed log archives
zgrep 'error' /var/log/syslog.1.gz
# Count occurrences of specific patterns
grep -c 'error' /var/log/syslog
Configuration File Editing
The vim text editor is commonly used for configuration files: ```
Open a configuration file
vim /etc/profile
Insert mode: Press 'i' to start inserting text
Exit insert mode: Press 'Esc'
Save and quit: Press ':', type 'wq', then press Enter
Quit without saving: Press ':', type 'q!', then press Enter
Apply changes to a profile file
source /etc/profile