# Basic Commands
touch
: Creates a new empty file.- Example:
touch newfile.txt
creates a new empty file named newfile.txt.
- Example:
mkdir
: Creates a new directory.- Example:
mkdir newdir
creates a new directory named newdir.
- Example:
mv
: Moves or renames files and directories.- Example:
mv oldfile.txt newfile.txt
renames oldfile.txt to newfile.txt.
- Example:
cp
: Copies files and directories.- Example:
cp file1.txt file2.txt
copies file1.txt to file2.txt.
- Example:
rm
: Removes files.- Example:
rm file.txt
deletes file.txt.
- Example:
rmdir
: Removes empty directories.- Example:
rmdir emptydir
deletes an empty directory named emptydir.
- Example:
# Pattern Matching and Globbing
- Globbing with
*
: Matches any number of characters.- Example:
rm *.txt
removes all files ending with .txt.
- Example:
# Advanced Globbing with Wildcards
[0-9]
: Matches any single digit.- Example:
ls file[0-9].txt
lists files like file1.txt, file2.txt, etc.
- Example:
[a-z]
: Matches any single lowercase letter.[A-Z]
: Matches any single uppercase letter.?
: Matches any single character.**
: Used for recursive globbing in newer versions of Bash.
# Viewing File Content
cat
: Displays the entire content of a file.- Example:
cat file.txt
displays the contents of file.txt.
- Example:
head
: Displays the first few lines of a file.- Example:
head -n 5 file.txt
shows the first 5 lines of file.txt.
- Example:
tail
: Displays the last few lines of a file.- Example:
tail -n 5 file.txt
shows the last 5 lines of file.txt.
- Example:
# Counting Words and Disk Usage
wc
: Counts lines, words, and characters in a file.- Example:
wc file.txt
displays the line, word, and character count of file.txt.
- Example:
du
: Shows the disk usage of files and directories.- Example:
du -sh *
shows the size of all files and directories in the current directory in human-readable format.
- Example:
# Writing and Appending to Text Files
Redirection Operator
>
: Writes output to a file, overwriting it.- Example:
echo "Hello" > file.txt
writes “Hello” to file.txt, overwriting its contents.
- Example:
Appending Operator
>>
: Appends output to a file.- Example:
echo "World" >> file.txt
appends “World” to file.txt.
- Example:
# The Standard Streams
stdin
(Standard Input): Usually the keyboard.stdout
(Standard Output): The screen or a file.stderr
(Standard Error): Error messages output stream.
#
Dual Output with tee
tee
: Reads from stdin and writes to stdout and files.- Example:
echo "Hello" | tee file.txt
writes “Hello” to both file.txt and the screen.
- Example:
#
Pipe: |
- Used to pass the output of one command as input to another.
- Example:
cat file.txt | grep "Hello"
passes the contents of file.txt to grep to search for “Hello”.
- Example:
# Sorting and Removing Duplicates
sort
: Sorts lines in text files.- Example:
sort file.txt
sorts the lines in file.txt.
- Example:
uniq
: Removes duplicate lines from sorted data.- Example:
sort file.txt | uniq
sorts file.txt and removes duplicate lines.
- Example:
#
Searching for Patterns with grep
grep
: Searches for patterns in files.- Example:
grep "Hello" file.txt
searches for the word “Hello” in file.txt.
- Example:
# Character Replacement and Reversal
tr
: Translates or deletes characters.- Example:
echo "hello" | tr 'a-z' 'A-Z'
translates lowercase to uppercase, outputting “HELLO”.
- Example:
rev
: Reverses lines character-wise.- Example:
echo "hello" | rev
outputs “olleh”.
- Example:
#
The cut
Program
cut
: Removes sections from each line of files.- Example:
cut -d':' -f1 file.txt
cuts out the first field from each line in file.txt, with fields delimited by ‘:’.
- Example:
#
Text Substitution with sed
sed
: A stream editor for filtering and transforming text.- Example:
sed 's/old/new/g' file.txt
replaces all occurrences of “old” with “new” in file.txt.
- Example: