Заделал пару функций.
Пока летел в самолете, наваял для bashrc парочку функций. Выложил все в github. https://github.com/mrAibo/bash/tree/main
В общем-то ничего особенного, за исключением пары функций из-за которых и выкладываю. Может кто-то оценит. Документацию составил ИИ. Тестируйте, если есть идеи, что добавить/улучшить - пишите. Будет время рассмотрю.
rcp (Remote Copy) Function
The rcp
function is a versatile tool for copying files and directories, both locally and to/from remote systems. It supports both rsync and scp, offering various options for compression, moving files, and more.
Usage
rcp [OPTIONS] source destination
Key Features
- Supports both local and remote file transfers
- Option to use rsync (default) or scp
- File compression during transfer
- Move files instead of copying
- Bandwidth limiting
- Exclude patterns for selective file transfer
- Verbose output option
Options
-z
,--compress
: Compress data during transfer-m
,--move
: Move files (delete source after successful transfer)-u
,--user USERNAME
: Specify remote username-p
,--port PORT
: Specify SSH port-i
,--identity FILE
: Use specific SSH identity file-l
,--limit RATE
: Set bandwidth limit (KB/s, rsync only)-e
,--exclude FILE
: Specify exclude file or pattern-s
,--use-scp
: Force SCP instead of rsync-v
,--verbose
: Enable verbose output-h
,--help
: Show help message
Examples
-
Basic copy:
rcp /path/to/source user@remote:/path/to/destination
-
Compressed transfer with verbose output:
rcp --compress --verbose /path/to/source user@remote:/path/to/destination
-
Move files using a specific SSH key and port:
rcp -m -p 2222 -i ~/.ssh/my_key /path/to/source user@remote:/path/to/destination
-
Use SCP with exclude patterns:
rcp --use-scp --exclude '*.log' /path/to/source user@remote:/path/to/destination
-
Limit bandwidth and use compression:
rcp -z -l 1000 -e '**/*.tmp' /path/to/source user@remote:/path/to/destination
This function provides a flexible and powerful way to handle file transfers in various scenarios, combining the strengths of rsync and scp with additional features for improved usability.
ex (Extract and Compress) Function
The ex
function is a versatile tool for extracting and compressing various types of archive files. It supports multiple formats and offers options for both extraction and compression operations.
Usage
ex [OPTIONS] file1 [file2 ...]
Key Features
- Supports multiple archive formats (e.g., tar.gz, zip, 7z)
- Can extract and compress files
- Allows specifying a target directory for operations
- Option to delete original files after successful operation
- Supports excluding files/directories during compression
- Verbose mode for detailed operation information
Options
-x
,--extract
: Extract mode (default)-c
,--compress FORMAT
: Compress mode (specify format: tar.gz, tar.bz2, zip, 7z)-v
,--verbose
: Enable verbose output-t
,--target DIR
: Specify target directory for extraction or compression-d
,--delete
: Delete original file(s) after operation-e
,--exclude PATTERN
: Exclude files/directories matching the pattern or specified in a file-h
,--help
: Show help message
Supported Formats
Extraction
- .tar.bz2, .tar.gz, .bz2, .rar, .gz, .tar, .tbz2, .tgz, .zip, .Z, .7z, .xz, .exe, .tar.xz, .tar.zst
Compression
- tar.gz, tar.bz2, zip, 7z
Examples
-
Extract an archive:
ex archive.tar.gz
-
Compress files to a zip archive with verbose output:
ex --compress zip --verbose --target /tmp file1 file2
-
Extract files to a specific directory and delete the original archive:
ex --extract --delete --target /path/to/extract archive.zip
-
Compress directories to a tar.gz archive, excluding certain files:
ex --compress tar.gz --exclude '*.log' --target /tmp dir1 dir2
-
Compress using patterns from an exclude file:
ex -c zip --exclude exclude.txt dir1 dir2
The ex
function simplifies the process of working with archives by providing a unified interface for both extraction and compression tasks. It supports a wide range of formats and offers flexibility through various options, making it a powerful tool for file archiving and extraction operations.
duu (Enhanced Disk Usage) Function
The duu
function is an enhanced version of the standard du
(disk usage) command. It provides a colorful, easy-to-read summary of disk usage for directories and files.
Usage
duu [directory]
If no directory is specified, it will analyze the current directory.
Key Features
- Color-coded output for easy reading
- Sorts results by size in descending order
- Displays sizes in human-readable format (K, M, G, T)
- Shows percentage and graphical representation of disk usage
- Summarizes total disk usage at the end
Output Format
The function displays the following information:
- Size: The amount of disk space used
- Unit: The unit of measurement (K, M, G, T)
- Usage: A graphical bar representing the percentage of usage
- Path: The file or directory path
Color Coding
- Green: Kilobytes (K)
- Yellow: Megabytes (M)
- Cyan: Gigabytes (G)
- Magenta: Terabytes (T)
- Blue: Default color for other units
Example Output
Disk Usage Summary
Size Unit Usage Path
---------- ------ --------- --------------------
1.23 G █████░░░░ /home/user/Documents
856.45 M ████░░░░░ /home/user/Downloads
234.56 M ██░░░░░░░ /home/user/Pictures
45.67 M █░░░░░░░░ /home/user/.config
----------------------------------------
Total: 2.34 G
Benefits
- Visual Representation: The bar chart provides an immediate visual cue about the relative sizes of directories.
- Color-Coding: Different colors for different size units make it easy to quickly identify large consumers of disk space.
- Sorted Output: Results are sorted by size, placing the largest items at the top.
- Human-Readable: Sizes are converted to the most appropriate unit (K, M, G, T) for easy understanding.
- Total Summary: Provides a total at the end, giving an overview of the analyzed directory’s total size.
Use Cases
- Quickly identify large files or directories consuming disk space
- Analyze disk usage patterns in a visually appealing manner
- Get a summarized view of disk usage in a specific directory
The duu
function enhances the standard disk usage analysis by providing a more user-friendly and informative output, making it easier to understand and manage disk space usage.