grep
is a command under linux which is user for searching text patterns in files.
grep [options] pattern [file...]
options
: Optional flags that modify the behaviour of thegrep
command.pattern
: The regular expression or string pattern to search for e.g. "help"file...
: Optional arguments specifying the files to search in. If not provided,grep
reads input from standard input.
Option | Description | Example |
---|---|---|
-c |
Count the number of matching lines | grep -c "pattern" file.txt |
-i |
Perform case-insensitive matching | grep -i "pattern" file.txt |
-v |
Invert the match (select non-matching lines) | grep -v "pattern" file.txt |
-n |
Show line numbers along with matching lines | grep -n "pattern" file.txt |
-l |
List only the names of matching files | grep -l "pattern" file1.txt file2.txt |
-r |
Recursively search directories and subdirectories | grep -r "pattern" directory/ |
-E |
Use extended regular expressions | grep -E "pattern" file.txt |
-F |
Treat the pattern as a fixed string | grep -F "pattern" file.txt |
-w |
Match whole words only | grep -w "pattern" file.txt |
-m <num> |
Stop reading a file after matching lines | grep -m 5 "pattern" file.txt |
-q |
Suppress output; useful for script usage | grep -q "pattern" file.txt |
--color |
Highlight the matching pattern in output | grep --color "pattern" file.txt |
cp
is a command under linux which is used to copy files and directories.
options
: Optional flags that modify the behaviour of thecp
command.source
: Specifies the source file or directory of which thecp
command takes contents from.destination
Specifies the destination file or directory in which thecp
command put content in.
Option | Description | Example |
---|---|---|
-r , -R |
Copy directories recursively | cp -r sourcedir/ destinationdir/ |
-p |
Preserve the original file attributes | cp -p file.txt backup/ |
-f |
Force overwrite existing files without prompt | cp -f newfile.txt existingfile.txt |
-i |
Prompt before overwriting existing files | cp -i file.txt destination/ |
-v |
Verbose output (display copied file names) | cp -v file1.txt file2.txt directory/ |
alias
is a command under linux which is used to define aliases for commands.
Certainly! Here's a cheat sheet for creating and managing aliases in Linux using the alias
command, represented in Markdown table format:
The syntax for creating aliases using the alias
command in Linux is as follows:
alias <name>=<value>
<name>
: The name you want to assign to the alias. It can be any valid name without spaces.<value>
: The command or set of commands you want the alias to represent. It can be any valid command or a combination of commands.
Here are a few examples to illustrate the alias syntax:
-
Create an alias named
ll
that executesls -l
:alias ll="ls -l"
-
Create an alias named
gs
that executesgit status
:alias gs="git status"
-
Create an alias named
cpv
that executescp -v
(verbose copy):alias cpv="cp -v"
-
Create an alias named
d
that executesdocker
:alias d="docker"
Command | Description | Example |
---|---|---|
alias |
Lists all defined aliases. | alias |
alias <name>=<value> |
Creates an alias with the specified name and value. | alias ll="ls -l" |
unalias <name> |
Removes the alias with the specified name. | unalias ll |
alias <name> |
Displays the definition of the alias with the given name. | alias ll |
alias <name>= |
Clears the definition of the alias with the given name. | alias ll= |
Aliases are useful for creating custom shortcuts or abbreviations for frequently used commands. By defining aliases, you can save time and simplify your command-line workflow.
Remember that aliases are typically defined in shell configuration files like ~/.bashrc
or ~/.bash_profile
. After modifying the configuration file, you need to reload it or open a new terminal session for the changes to take effect.
The cat
command concatenates (reads) the contents of a file and prints it to the standard output.
cat [OPTION]... [FILE]...
Here's a breakdown of the syntax elements:
cat
: The command itself.[OPTION]...
: Optional flags that modify the behavior of thecat
command. Flags are prefixed with a dash (-
).[FILE]...
: Optional file names or paths. You can specify one or more files to display their contents or concatenate them together.
Here are a few examples illustrating the usage of the cat
command:
-
Display the contents of a single file:
cat file.txt
-
Concatenate multiple files and display their contents:
cat file1.txt file2.txt
-
Display the contents of a file with line numbers:
cat -n file.txt
-
Concatenate multiple files and save the result to a new file:
cat file1.txt file2.txt > merged.txt
-
Append the contents of a file to an existing file:
cat file2.txt >> file1.txt
Command | Description | Example |
---|---|---|
cat |
Display the contents of a file | cat file.txt |
cat file1 file2 |
Concatenate multiple files and display their contents | cat file1.txt file2.txt |
cat > file |
Create a new file and enter text (Ctrl+D to save) | cat > newfile.txt |
cat >> file |
Append text to an existing file (Ctrl+D to save) | cat >> existingfile.txt |
cat -n |
Display line numbers with the file contents | cat -n file.txt |
cat -b |
Display line numbers with non-empty lines | cat -b file.txt |
cat -E |
Display $ at the end of each line |
cat -E file.txt |
cat -T |
Display tabs as ^I |
cat -T file.txt |
cat -s |
Squeeze multiple blank lines into one | cat -s file.txt |
cat -v |
Display non-printable characters in a visible format | cat -v file.txt |
The rm
command in Linux is used to remove/delete files and directories. The basic syntax of the rm
command is:
rm [OPTION]... FILE...
Here's a breakdown of the syntax elements:
rm
: The command itself.[OPTION]...
: Optional flags that modify the behavior of therm
command. Flags are prefixed with a dash (-
).FILE...
: One or more file names or paths specifying the files or directories to be removed.
Here are a few examples illustrating the usage of the rm
command:
-
Remove a single file:
rm file.txt
-
Remove multiple files:
rm file1.txt file2.txt
-
Remove a directory and its contents recursively:
rm -r directory/
-
Forcefully remove a file without prompt:
rm -f file.txt
-
Prompt for confirmation before removing a file:
rm -i file.txt
-
Remove a directory and its contents forcefully and recursively:
rm -rf directory/
Command | Description | Example |
---|---|---|
rm <file> |
Delete a file | rm file.txt |
rm -r <directory> |
Delete a directory and its contents recursively | rm -r directory/ |
rm -f <file> |
Forcefully delete a file without prompt | rm -f file.txt |
rm -i <file> |
Prompt for confirmation before deleting a file | rm -i file.txt |
rm -rf <directory> |
Forcefully delete a directory and its contents recursively | rm -rf directory/ |
rm -d <directory> |
Delete an empty directory | rm -d emptydir/ |
rm -v <file> |
Display verbose output (show deleted files) | rm -v file.txt |
rm --help |
Display help information about the rm command |
rm --help |
These are some common options and commands for the rm
command. Please note that the -f
(force) and -r
(recursive) options should be used with caution as they can permanently delete files and directories without further confirmation.
Remember to exercise caution when using the rm
command as deleted files cannot be easily recovered. Always double-check your commands and ensure you're deleting the intended files or directories.
Additionally, the behavior of the rm
command can vary slightly depending on the operating system and version. It's recommended to refer to the command's manual (man rm
) for more detailed information and options specific to your environment.
The mkdir
command in Linux is used to create directories (folders). The basic syntax of the mkdir
command is:
mkdir [OPTION]... DIRECTORY...
Here's a breakdown of the syntax elements:
mkdir
: The command itself.[OPTION]...
: Optional flags that modify the behavior of themkdir
command. Flags are prefixed with a dash (-
).DIRECTORY...
: One or more directory names or paths specifying the directories to be created.
Here are a few examples illustrating the usage of the mkdir
command:
-
Create a single directory:
mkdir directory_name
-
Create multiple directories at once:
mkdir dir1 dir2 dir3
-
Create a directory with specific permissions (mode):
mkdir -m 755 directory_name
-
Create nested directories (with parent directories if they don't exist):
mkdir -p path/to/directory
-
Create directories with verbose output:
mkdir -v dir1 dir2 dir3
Option | Description | Example |
---|---|---|
-m, --mode=MODE |
Set the file mode (permissions) of the created directory to MODE (in octal). |
mkdir -m 755 directory_name |
-p, --parents |
Create any necessary parent directories. If they already exist, no error is reported. | mkdir -p path/to/directory |
-v, --verbose |
Print a message for each created directory. | mkdir -v dir1 dir2 dir3 |
-Z, --context[=CTX] |
Set the SELinux security context of each created directory to CTX . If CTX is not specified, the default context is used. |
mkdir -Z httpd_sys_content_t /var/www |
The touch
command in Linux is used to create new files or update the timestamps (access time and modification time) of existing files. The basic syntax of the touch
command is:
touch [OPTION]... FILE...
Here's a breakdown of the syntax elements:
touch
: The command itself.[OPTION]...
: Optional flags that modify the behavior of thetouch
command. Flags are prefixed with a dash (-
).FILE...
: One or more file names or paths specifying the files to be created or updated.
Here are a few examples illustrating the usage of the touch
command:
-
Create a new file:
touch file.txt
-
Create multiple files at once:
touch file1.txt file2.txt file3.txt
-
Update the timestamps of an existing file:
touch existing_file.txt
-
Set specific timestamps for a file:
touch -t 202201011200 file.txt
-
Update the timestamps of a file using another file as reference:
touch -r reference_file.txt target_file.txt
Here's a Markdown table representing the options for the touch
command:
Option | Description | Example |
---|---|---|
-a |
Change only the access time | touch -a file.txt |
-c |
Do not create a new file if it doesn't exist | touch -c file.txt |
-d |
Set the access and modification time to the specified value | touch -d "2022-01-01" file.txt |
-m |
Change only the modification time | touch -m file.txt |
-r <file> |
Use the timestamp of <file> for the new file |
touch -r original.txt new.txt |
-t <time> |
Set the access and modification time using <time> |
touch -t 202201011200 file.txt |
-h |
Change the timestamps of symbolic links instead of the target | touch -h link.txt |
--help |
Display help information about the touch command |
touch --help |
These options provide various ways to modify the timestamps (access time and modification time) of files using the touch
command. You can use these options to set specific timestamps, change individual timestamps, or reference timestamps from other files.
The echo
command in Linux is used to display text or variables on the terminal. The basic syntax of the echo
command is:
echo [OPTION]... [STRING]...
Here's a breakdown of the syntax elements:
echo
: The command itself.[OPTION]...
: Optional flags that modify the behavior of theecho
command. Flags are prefixed with a dash (-
).[STRING]...
: Optional strings or variables to be displayed on the terminal. These can be enclosed in quotes for multiple words or containing special characters.
Here are a few examples illustrating the usage of the echo
command:
-
Display a simple string:
echo "Hello, World!"
-
Display the value of a variable:
name="John" echo "My name is $name"
-
Display multiple strings:
echo "Hello," "World!"
-
Display strings without a trailing newline:
echo -n "This is a " echo "sentence."
-
Interpret and display escape sequences:
echo -e "This is\ttabbed text"
Option | Description | Example |
---|---|---|
-n |
Suppresses the trailing newline character | echo -n "Hello, World!" |
-e |
Enables the interpretation of escape sequences | echo -e "Line 1\nLine 2" |
-E |
Disables the interpretation of escape sequences (default) | echo -E "Line 1\nLine 2" |
-s |
Suppresses the echo command itself from being displayed | echo -s "This will not be displayed" |
--help |
Displays help information about the echo command |
echo --help |
--version |
Displays the version information of the echo command |
echo --version |
Under linux the wc
command is used to count the number of lines, wordsm and characters in a file or the output of a command. wc
stands for word count
.
wc [options] [file]
options
Optional flags that modify the behaviour of thewc
commandfile
Optional argument specifying the file to be analyzed. If not provided,wc
reads input from standard input.
The wc
command provides three types of counts:
- Line count
-l
option: It displays the number of lines in the given file or input. - Word count
-w
option: It shows the number of words in the given file or input. A word is defined as a contiguous sequence of characters separated by whitespace. - Character count
-c
option: It gives the number of characters in the given file or input. This count includes spaces, tabs, and newlines.
A few examples:
-
Count the numbers of lines, words, and characters in a file:
wc filename.txt
-
Count only the number of lines in a file:
wc -l filename.txt
-
Count the number of words in the output of a command (piped out):
cat filename.txt | wc -w
-
Count the number of characters in a file:
wc -c filename.txt
Option | Description | Example |
---|---|---|
-l |
Count the number of lines | wc -l file.txt |
-w |
Count the number of words | wc -w file.txt |
-c |
Count the number of characters | wc -c file.txt |
-m |
Count the number of characters (including multibyte characters) | wc -m file.txt |
-L |
Find the length of the longest line | wc -L file.txt |
--help |
Display help information about the wc command |
wc --help |
--version |
Display the version information of the wc command |
wc --version |
These options allow you to customize the output of the wc
command and obtain specific information about the input file or stream. You can count lines, words, characters (bytes), or multibyte characters, as well as find the length of the longest line.
It's important to note that the wc
command can accept multiple files as arguments, and it will provide separate counts for each file as well as a cumulative count for all files combined.
The awk
command in Linux is a versatile text-processing tool used to extract and manipulate data from text files. The basic syntax of the awk
command is:
awk 'pattern { action }' [file]
Here's a breakdown of the syntax elements:
awk
: The command itself.'pattern { action }'
: The pattern-action pairs enclosed in single quotes. The pattern determines which lines to process, and the action specifies the action to perform on those lines.[file]
: Optional file name or path specifying the input file. If not provided,awk
reads from standard input (e.g., piped input).
Here are a few examples illustrating the usage of the awk
command:
-
Print all lines in a file:
awk '{ print }' file.txt
-
Print the first column of each line:
awk '{ print $1 }' file.txt
-
Print lines where the second column is equal to a specific value:
awk '$2 == "value" { print }' file.txt
-
Calculate and print the sum of numbers in a column:
awk '{ sum += $1 } END { print sum }' file.txt
-
Print lines with a specific pattern and perform custom actions:
awk '/pattern/ { print "Found:", $0 }' file.txt
Option | Description | Example |
---|---|---|
-F <value> |
Specifies the input field separator | awk -F ',' '{print $1}' file.csv |
-v <var=value> |
Sets the value of a variable | awk -v num=10 '{print $1 + num}' file.txt |
-f <file> |
Specifies a file containing the awk script | awk -f script.awk file.txt |
BEGIN{} |
Executes actions before processing the input | awk 'BEGIN{print "Start"} {print $0}' file.txt |
END{} |
Executes actions after processing the input | awk '{sum += $1} END{print "Total:", sum}' file.txt |
NR |
Represents the current record number | awk '{print NR, $0}' file.txt |
NF |
Represents the number of fields in the current record | awk '{print NF, $0}' file.txt |
/pattern/ |
Matches lines that contain the specified pattern | awk '/keyword/ {print $0}' file.txt |
{print} |
Prints the entire record or selected fields | awk '{print $1, $3}' file.txt |
if(){} else{} |
Conditional statement to perform actions based on a condition | awk '{if ($1 > 10) print "High"; else print "Low"}' file.txt |
length() |
Returns the length of a string or field | awk '{if (length($1) > 5) print $0}' file.txt |
Wildcard | Description | Example |
---|---|---|
* |
Matches any sequence of characters (including none) | ls *.txt - matches all files ending with .txt |
? |
Matches any single character | ls file?.txt - matches file1.txt , file2.txt , etc. |
[ ] |
Matches any single character within the specified range | ls file[1-3].txt - matches file1.txt , file2.txt , file3.txt |
[! ] |
Matches any single character not within the specified range | ls file[!1-3].txt - matches any file except file1.txt , file2.txt , file3.txt |
{ } |
Expands a sequence of comma-separated values | cp file{1,2,3}.txt destination/ - copies file1.txt , file2.txt , file3.txt to the destination/ directory |
Here are some examples to illustrate the usage of wildcards:
-
List all files with the
.txt
extension:ls *.txt
-
Remove all files starting with
backup
and ending with.bak
:rm backup*.bak
-
Copy all files with a single character name and
.txt
extension to a directory:cp ?*.txt destination/
-
Find files with names that match a specific pattern:
find . -name "prefix*suffix"
-
Move files with names containing a range of numbers:
mv file[1-5].txt destination/
Aliases are shortcuts to commonly used commands. To define an alias, use the alias
command.
Command | Description | Example |
---|---|---|
alias |
Displays a list of currently defined aliases | alias |
alias name=value |
Creates an alias with the specified name and value | alias ll='ls -al' |
unalias name |
Removes the alias with the specified name | unalias ll |
alias name |
Displays the definition of the alias with the specified name | alias ll |
alias name=new_value |
Modifies the value of an existing alias | alias ll='ls -lh' |
alias -p or alias --p |
Displays a list of all defined aliases in a format that can be reused in a shell script | alias -p |
Here are some examples of commonly used aliases:
-
Shorten a frequently used command:
alias ll='ls -al'
-
Create an alias with command options:
alias grep='grep --color=auto'
-
Create an alias to navigate to a specific directory:
alias mydocs='cd ~/Documents'
-
Define an alias with multiple commands:
alias update='sudo apt update && sudo apt upgrade'
-
Combine an alias with command arguments:
alias duh='du -sh'
Aliases can significantly enhance your productivity by allowing you to create custom shortcuts or modify the behavior of existing commands. However, please note that aliases are specific to the current shell session and are not permanent. If you want to make an alias permanent, you can add it to your shell configuration file, such as .bashrc
or .zshrc
.
© Rayan Lee Bopp