the core GNU packages commonly found in the base system include:
coreutils: Provides essential commands for file and text manipulation, including
ls
,cp
,mv
,rm
,mkdir
,rmdir
,cat
,echo
,touch
,chown
,chgrp
, and many more.findutils: Includes the
find
command for searching and locating files and directories in a directory hierarchy.diffutils: Contains tools for comparing and finding differences between files, including the
diff
andcmp
commands.grep: The GNU version of the
grep
command for searching text files using regular expressions.sed: A stream editor for text transformations.
awk: A versatile text processing tool for manipulating data.
tar: A utility for creating and extracting archives.
gzip/gunzip: Provides file compression and decompression utilities.
bash: The GNU Bourne-Again Shell, a command-line interpreter that is commonly used as the default system shell.
file: A command for determining file types.
xargs: A utility for building and executing command lines from standard input.
diff: A program for comparing files and showing their differences.
patch: A utility for applying differences between files, often used for source code patching.
ncurses: The library that provides text-based user interfaces for terminal applications.
gawk: The GNU implementation of AWK, a powerful text processing language.
bison: A parser generator used for creating parsers and compilers.
flex: A lexical analyzer generator used for creating scanners for programming languages.
coreutils: A package that provides various fundamental commands for file and text manipulation.
These packages and utilities form the core set of tools that are included in most GNU/Linux distributions and are considered essential for basic system administration, scripting, and day-to-day tasks. The availability of these utilities ensures that the system can perform fundamental file and text operations, and they are often required for system bootstrapping and recovery.
coreutils
ls: List files and directories in a directory.
cd: Change the current directory.
pwd: Print the current working directory.
touch: Create an empty file or update a file's access and modification timestamps.
mkdir: Create directories.
rm: Remove files or directories.
cp: Copy files and directories.
mv: Move or rename files and directories.
cat: Concatenate and display file content.
more/less: View text files one page at a time.
head/tail: Display the beginning or end of a file.
grep: Search for patterns in text using regular expressions.
find: Search for files and directories in a directory hierarchy.
chmod: Change file permissions.
chown: Change file ownership.
chgrp: Change file group ownership.
ln: Create hard or symbolic links.
ps: Display information about processes.
kill: Terminate processes.
top/htop: Monitor system processes and resource usage.
df: Display disk space usage.
du: Estimate file and directory space usage.
tar: Create and extract archives.
gzip/gunzip: Compress and decompress files.
awk: Text processing tool for manipulating data.
sed: Stream editor for text transformations.
sort: Sort lines of text files.
cut: Remove sections from lines of files.
xargs: Build and execute command lines from standard input.
tee: Read from standard input and write to standard output and files.
date: Display or set the system date and time.
echo: Display text to the terminal.
basename: Remove directory and suffix from file names.
dirname: Remove non-directory suffix from file names.
rev: Reverse lines of files.
tr: Translate characters.
wc: Count lines, words, and characters in files.
cmp: Compare two files byte by byte.
comm: Compare two sorted files line by line.
diff: Compare and find differences between files.
ln: Create hard or symbolic links.
touch: Change file timestamps.
mktemp: Create a temporary file or directory.
awk
AWK is a versatile text processing tool that allows you to manipulate data in text files. Here's a quick AWK tutorial to get you started:
Basic Structure:
- AWK programs consist of a pattern-action pair. It processes each line in a file, matching lines that meet the specified pattern and performing the associated action.
Printing:
- The simplest AWK program is
awk '{print $0}' file.txt
, which prints every line offile.txt
.
- The simplest AWK program is
Field Separators:
- AWK splits lines into fields. By default, fields are separated by spaces or tabs, but you can specify a custom separator with
-F
orFS
.
- AWK splits lines into fields. By default, fields are separated by spaces or tabs, but you can specify a custom separator with
Printing Fields:
- To print specific fields, use
$1
,$2
, etc. For example,awk '{print $2, $1}' file.txt
swaps the first and second fields.
- To print specific fields, use
Patterns and Conditions:
- You can add conditions to filter lines. For example,
awk '$3 > 10' file.txt
prints lines where the third field is greater than 10.
- You can add conditions to filter lines. For example,
Built-in Variables:
- AWK provides several built-in variables like
NF
(number of fields) andNR
(record number). For example,awk 'NF > 3' file.txt
prints lines with more than 3 fields.
- AWK provides several built-in variables like
Regular Expressions:
- AWK supports regular expressions. Use
/pattern/
to match lines with a specific pattern. For example,awk '/error/' file.txt
prints lines containing "error."
- AWK supports regular expressions. Use
User-Defined Variables:
- You can create your own variables in AWK. For example,
awk '{total += $1} END {print total}' file.txt
calculates the sum of the first field.
- You can create your own variables in AWK. For example,
Control Structures:
- AWK has basic control structures like
if
,else
, andfor
. For instance, you can use anif
statement to conditionally perform actions.
- AWK has basic control structures like
Functions:
- AWK supports functions. You can use built-in functions like
split()
or create custom functions to perform complex operations.
- AWK supports functions. You can use built-in functions like
File Output:
- You can redirect the output to a file using
>
or>>
. For example,awk '/pattern/' file.txt > output.txt
saves lines with "pattern" to a new file.
- You can redirect the output to a file using
Pipelines:
- AWK can be used in pipelines with other Unix utilities like
grep
,sort
, andsed
to perform more complex text processing tasks.
- AWK can be used in pipelines with other Unix utilities like
One-Liners:
- AWK is often used for quick one-liners. For example,
ls -l | awk '{total += $5} END {print "Total:", total}'
calculates the total size of files in the current directory.
- AWK is often used for quick one-liners. For example,
AWK is a powerful tool for text processing and data extraction. You can create complex data processing scripts or use it for simple text manipulation tasks. The more you practice and explore its capabilities, the more proficient you'll become.
sed
Basic
sed
Command Structure:sed 's/pattern/replacement/' file
replaces occurrences ofpattern
withreplacement
in the specifiedfile
.
Printing Text:
- To print the contents of a file, use
sed -n 'p' file
.
- To print the contents of a file, use
Substitution:
- Replace text using the
s
command. For example,sed 's/old/new/' file
replaces the first occurrence ofold
withnew
on each line.
- Replace text using the
Global Substitution:
- Use the
g
flag to replace all occurrences in a line:sed 's/old/new/g' file
.
- Use the
In-Place Editing:
- To edit a file in-place, use the
-i
option:sed -i 's/old/new/g' file
.
- To edit a file in-place, use the
Multiple Operations:
- You can combine multiple operations by separating them with a semicolon:
sed 's/one/two/; s/three/four/' file
.
- You can combine multiple operations by separating them with a semicolon:
Print Specific Lines:
- To print specific lines, use line addresses. For example,
sed -n '1,5p' file
prints lines 1 to 5.
- To print specific lines, use line addresses. For example,
Delete Lines:
- To delete lines, use the
d
command. For instance,sed '/pattern/d' file
deletes lines containingpattern
.
- To delete lines, use the
Append Text:
- To append text after a line, use the
a
command:sed '/pattern/a text to append' file
.
- To append text after a line, use the
Insert Text:
- To insert text before a line, use the
i
command:sed '/pattern/i text to insert' file
.
- To insert text before a line, use the
Find and Replace:
- Replace text on specific lines. For example,
sed '3,7s/old/new/g' file
replacesold
withnew
on lines 3 to 7.
- Replace text on specific lines. For example,
Regular Expressions:
sed
supports regular expressions. You can usesed
with regex patterns to match complex text.
Backreferences:
- Use backreferences to refer to parts of the matched pattern. For example,
sed 's/\(first\).*/\1/' file
keeps only "first."
- Use backreferences to refer to parts of the matched pattern. For example,
Save to New File:
- To save the edited content to a new file, redirect the output:
sed 's/old/new/' input.txt > output.txt
.
- To save the edited content to a new file, redirect the output:
Case Insensitivity:
- Make your search and replace case-insensitive with the
i
flag:sed 's/pattern/replacement/i' file
.
- Make your search and replace case-insensitive with the
Multiple Input Files:
- You can process multiple files by specifying them as arguments:
sed 's/old/new/' file1 file2
.
- You can process multiple files by specifying them as arguments:
Remove Leading Whitespace:
- To remove leading whitespace, use
sed 's/^[ \t]*//' file
.
- To remove leading whitespace, use
Remove Trailing Whitespace:
- To remove trailing whitespace, use
sed 's/[ \t]*$//' file
.
- To remove trailing whitespace, use
sed
is a versatile tool for text processing and manipulation, and it's commonly used in scripts and command pipelines for various tasks. As you gain experience, you'll discover its many powerful features for text editing and transformation.
Basic Structure:
grep pattern file
searches for lines infile
that match the specifiedpattern
.
Case-Insensitive Search:
- Use the
-i
option to perform a case-insensitive search. For example,grep -i pattern file
matches "Pattern," "PATTERN," and "pattern."
- Use the
Whole Word Search:
- Use the
-w
option to search for whole words only. For example,grep -w word file
matches "word" but not "words" or "sword."
- Use the
Invert Match:
- Use the
-v
option to invert the match, showing lines that don't contain the pattern. For example,grep -v pattern file
shows lines without "pattern."
- Use the
Count Matches:
- Use the
-c
option to count the number of matches. For example,grep -c pattern file
counts how many times "pattern" appears.
- Use the
Regular Expressions:
grep
supports regular expressions for complex pattern matching. For example,grep '^a.*z$' file
matches lines that start with "a" and end with "z."
Print Line Numbers:
- Use the
-n
option to print line numbers along with matching lines. For example,grep -n pattern file
shows line numbers.
- Use the
Recursive Search:
- Search for a pattern in all files under a directory and its subdirectories using the
-r
option. For example,grep -r pattern directory
.
- Search for a pattern in all files under a directory and its subdirectories using the
Show Context:
- Use the
-A
,-B
, or-C
options to display context around the match. For example,grep -C 2 pattern file
shows two lines of context around each match.
- Use the
Filter Files by Type:
- Use the
--include
option to specify file types to search. For example,grep pattern --include='*.txt' directory
searches for "pattern" only in text files.
- Use the
Multiple Patterns:
- Search for lines that match multiple patterns using the
|
(pipe) character. For example,grep 'pattern1\|pattern2' file
matches lines with either "pattern1" or "pattern2."
- Search for lines that match multiple patterns using the
Quiet Mode:
- Use the
-q
option for quiet mode, which is useful for scripting. It doesn't display matching lines but returns an exit code indicating success or failure.
- Use the
Fixed Strings:
- Use the
-F
option to treat the pattern as a fixed string (no regular expressions). This is useful when searching for special characters.
- Use the
Print Matching Patterns Only:
- Use the
-o
option to print only the matching part of the line, rather than the whole line.
- Use the
"Grep" is a versatile tool for searching and pattern matching in text files. It's widely used in Unix-like systems and is a valuable tool for text processing and data extraction.
find
The find
command is a powerful tool for searching and locating files and directories within a Unix-like file system. It offers a wide range of options for fine-tuned searches.
Basic Syntax:
find <path> <options> <pattern>
: Searches for files and directories in the specified<path>
.
Search by Name:
- To find files with a specific name, use
find <path> -name "<filename>"
. For example,find /home/user -name "file.txt"
.
- To find files with a specific name, use
Search by Type:
- To find files or directories of a specific type, use
-type
. For example,find /path -type f
finds files, andfind /path -type d
finds directories.
- To find files or directories of a specific type, use
Search by Size:
- To find files by size, use
-size
. For example,find /path -size +1M
finds files larger than 1MB.
- To find files by size, use
Search by Modification Time:
- Use
-mtime
to find files modified in a specific number of days. For example,find /path -mtime -7
finds files modified within the last 7 days.
- Use
Search by Access Time:
- Use
-atime
to find files accessed in a specific number of days. For example,find /path -atime +30
finds files accessed more than 30 days ago.
- Use
Search by Owner:
- To find files owned by a specific user, use
-user
. For example,find /path -user username
.
- To find files owned by a specific user, use
Search by Group:
- To find files in a specific group, use
-group
. For example,find /path -group groupname
.
- To find files in a specific group, use
Search by Permissions:
- To find files with specific permissions, use
-perm
. For example,find /path -perm 644
finds files with read and write permissions for the owner.
- To find files with specific permissions, use
Search for Empty Files and Directories:
- To find empty files or directories, use
-empty
. For example,find /path -type d -empty
finds empty directories.
- To find empty files or directories, use
Search with Logical Operators:
- Combine search criteria with logical operators like
-and
,-or
, and-not
. For example,find /path -name "*.txt" -or -name "*.log"
finds files with either.txt
or.log
extensions.
- Combine search criteria with logical operators like
Execute Commands on Results:
- Use
-exec
to perform actions on the search results. For example,find /path -name "*.txt" -exec rm {} \;
deletes all.txt
files.
- Use
Limit the Depth of Search:
- Use
-maxdepth
to limit the depth of the search. For example,find /path -maxdepth 2
searches up to a depth of 2 directories.
- Use
Print Results:
- By default,
find
prints the results to the standard output. You can redirect the output to a file for further analysis.
- By default,
Search Multiple Directories:
- You can specify multiple paths to search in different directories.
Case-Insensitive Search:
- Use the
-iname
option for a case-insensitive search.
- Use the
Save Results to a File:
- Use the
>
or>>
operators to save the results to a file. For example,find /path -name "*.txt" > results.txt
.
- Use the
"find" is a versatile and powerful tool for locating files and directories on a Unix-like system. By mastering its various options and combining them, you can perform advanced and precise file searches.
xargs
is a command that allows you to process input and execute commands with that input, which can be particularly useful when working with lists of items or data from standard input.
Basic Syntax:
xargs [options] [command]
:xargs
reads items from standard input (usually separated by spaces or newlines) and passes them as arguments to the specifiedcommand
.
Reading from Standard Input:
- By default,
xargs
reads data from standard input (e.g., via a pipe). For example,echo "file1 file2 file3" | xargs command
passes "file1," "file2," and "file3" as arguments tocommand
.
- By default,
Passing Arguments:
- The
command
is the action to perform on each item received. For example,xargs -I {} mv {} /destination
moves each item to the specified destination.
- The
Specifying Delimiters:
- Use the
-d
or--delimiter
option to specify a custom delimiter for input items. For example,echo "item1:item2:item3" | xargs -d ':' command
splits the input on colons.
- Use the
Maximum Arguments:
- Use the
-n
or--max-args
option to limit the number of arguments passed to each command. For example,echo "file1 file2 file3" | xargs -n 2 command
processes two items at a time.
- Use the
Interactive Mode:
- The
-p
option prompts the user for confirmation before executing each command. For example,find /path -type f | xargs -p rm
asks for confirmation before removing each file.
- The
Verbose Mode:
- Use the
-t
or--verbose
option to print the command to be executed before running it. For example,echo "file1 file2" | xargs -t -I {} mv {} /destination
shows themv
command before execution.
- Use the
Substituting with Placeholder:
- The
-I
option allows you to specify a placeholder (e.g.,{}
) to represent the input item in the command. For example,echo "file1 file2" | xargs -I {} mv {} /destination
moves files to the destination.
- The
Parallel Execution:
xargs
can execute commands in parallel using the-P
option, followed by the maximum number of parallel processes. For example,echo "file1 file2" | xargs -P 2 -I {} command
runs twocommand
processes concurrently.
Using a File List:
- You can read input from a file by using the
-a
or--arg-file
option. For example,xargs -a input.txt command
processes items listed ininput.txt
.
- You can read input from a file by using the
Ignoring Empty Lines:
- Use the
-r
or--no-run-if-empty
option to prevent command execution if no input items are provided.
- Use the
Combining with Find:
find
can be used in combination withxargs
to execute commands on files found byfind
. For example,find /path -type f | xargs command
executescommand
on each file found.
xargs
is a versatile tool for processing lists of items or data from standard input and applying commands to them efficiently. It's commonly used in conjunction with other commands to perform batch operations and streamline text processing tasks.
credits
chatgpt
Comments
Post a Comment