For Linux users who are familiar with commands like cp, mv, and ls, here are some commands I found useful but took some time to master:
cat
Most often, cat is used to display file contents, such as cat {your file}. However, it can also be used to concatenate two files and create a new file.
- To concatenate two files:
cat {file1} {file2} > {merged file}
- To create a new file and write to it:
cat > {your file}- It will accept your input and write it to the file.
- Some automation scripts use this command to create files, like:
1 2 3cat > testFile << EOF {your content} EOF
grep
|
|
- -i: Ignore case.
- -R: Recursively search in subdirectories.
- -v: Invert match, output lines that don’t contain the keyword.
cut
This command is useful for quickly extracting specific content from files with a fixed format, such as /etc/passwd. In this file, each line is separated by colons (":").
|
|
Using cut on this file:
|
|
- -d specifies the delimiter, and we use “:” to indicate that the delimiter is a colon.
- -f1 indicates that we want to extract the first field after splitting, which is the username.
The output will be:
|
|
awk
When the separator is more complex or variable, you can use awk. For the example mentioned above, you can use awk to achieve the same result:
|
|
- -F specifies the field separator. {print $1} specifies to print the first field after splitting.
sed
sed is used for text substitution. It operates on streams and doesn’t overwrite the original file. For example:
|
|
Redirection
By default, the output of Linux commands is displayed on the screen, but you can redirect the output to a file. Here are some key points:
>will overwrite the target, while>>appends.1is stdout, and2is stderr.- You can use
&to redirect all output.
|
|
Pipe
Use the pipe | to pass the output of one command as input to another.
|
|