In Linux, metacharacters are special characters that have a specific meaning and
are used in shell commands for various operations like pattern matching, process
control, command chaining, and more. Here are some common metacharacters along with
examples of how they are used:
1. Wildcard/ Metacharacters
--------------------------
*: Matches zero or more characters.
Example: ls *.txt
Lists all files ending with .txt.
?: Matches exactly one character.
Example: ls file?.txt
Lists files like file1.txt, file2.txt, but not file10.txt.
[]: Matches any one of the enclosed characters.
Example: ls file[123].txt
Lists files like file1.txt, file2.txt, file3.txt.
2. Redirection Metacharacters
------------------------------
>: Redirects standard output to a file.
Example: echo "Hello, World!" > hello.txt
Writes "Hello, World!" to hello.txt.
<: Redirects standard input from a file.
Example: sort < unsorted.txt
Sorts the contents of unsorted.txt.
>>: Appends standard output to a file.
Example: echo "Goodbye" >> hello.txt
Appends "Goodbye" to hello.txt.
3. Pipes and Command Chaining
------------------------------
|: Pipes the output of one command as input to another.
Example: ls -l | grep "txt"
Lists detailed information about .txt files.
; : Separates multiple commands to run sequentially.
Example: cd /tmp; ls
Changes directory to /tmp and then lists files.
&&: Runs the second command only if the first succeeds.
Example: mkdir newdir && cd newdir
Creates newdir and enters it if creation is successful.
||: Runs the second command only if the first fails.
Example: mkdir newdir || echo "Failed to create directory"
Attempts to create newdir and echoes an error if it fails.
4. Quotation Metacharacters
---------------------------
" (double quotes): Preserves literal value of enclosed characters except for $, `,
and \.
Example: echo "The price is $5"
Prints The price is $5.
' (single quotes): Preserves literal value of all enclosed characters.
Example: echo 'The price is $5'
Prints The price is $5 without interpreting $.
` (backticks): Executes the enclosed command and substitutes its output.
Example: echo `date`
Prints the current date and time.
5. Process Control Metacharacters
---------------------------------
$: Refers to variables.
Example: echo $HOME
Prints the current user's home directory.
6. Escape Character
---------------------
\: Escapes the special meaning of a metacharacter.
Example: echo \$HOME
Prints $HOME literally, not the value of the HOME variable.
7. Command Substitution
-----------------------
$(): Similar to backticks, it executes the enclosed command and substitutes its
output.
Example: echo "Today is $(date)"
Prints Today is followed by the current date.
The date command in Linux is used to display or set the system date and time. It
can also format dates and times in various ways.
Basic Usage
To display the current date and time:
date
Example Output:
Wed Sep 4 12:34:56 UTC 2024
Formatting Date and Time
You can format the output of the date command using format specifiers.
Display date in YYYY-MM-DD format:
date +"%Y-%m-%d"
Example Output:
2024-09-04
Display time in HH:MM:SS format:
date +"%H:%M:%S"
Example Output:
12:34:56
Display full date and time in custom format:
date +"%A, %B %d, %Y %H:%M:%S"
Example Output:
Wednesday, September 04, 2024 12:34:56
Useful Date Format Specifiers
%Y: Year (e.g., 2024)
%m: Month (01-12)
%d: Day of the month (01-31)
%I: 12 Hour
%H: Hour (00-23)
%M: Minute (00-59)
%S: Second (00-59)
%A: Full weekday name (e.g., Wednesday)
%B: Full month name (e.g., September)
File permissions
****************
changes the permissions for files and directories
3 types of users
# primary
# group
# others
3 permissions for all users we have:
# read (r)
# write (w)
# execute (x)
2 ways we can give permissions:
1. symbolic or text method
2. using mode number
notations
u:user
g:group
o:other
+:add permission
-: remove permission
r:read
w:write
x:execute
command used to change permissions for files and directories is
chmod : change mode
chmod u-w+x,g+x-w,o-wx <file name> {symbolic or text method)
this command will change the permissions of user,group and others
note + and- are used to add permission and remove permissions repectively.
chmod 777 new3.doc {using mode number} 777 is given to all user,group and others
full permissions
decimal binary permissions
r w x
0 0 0 0 NO
1 0 0 1 Execute
2 0 1 0 write
3 0 1 1 W & E
4 1 0 0 Read
5 1 0 1 R & E
6 1 1 0 R & W
7 1 1 1 R W & E
what pipe?
pipe are used to redirect a stream from one program to another program.
the output of one command redirect it to the input of another "|" vertical bar
command1 | command2 | command3 |...
ls -l | wc : it prints no.lines,no.of words and no. of bytes of all file present in
current directory
Miscellaneous Commands
----------------------
1. ls list all the files and directories.
2. pwd present working directory.
3. head displays the top 10 lines of a file by default
syntax:
head [options] filename
-n <N> → Show the first N lines (default: 10). N is the number
-c <N> → Show the first N bytes.
eg:
head file.txt
head -n 5 file.txt
head -c 20 file.txt
4. Tail Displays the last 10 lines of the file.
syntax:
tail [options] filename
-n <N> → Show the last N lines.
-c <N> → Show the last N bytes.
eg:
tail file.txt
tail -n 5 file.txt
5. Tac Displays the file in the reverse order.
syntax:
tac filename
6. More Similar to cat command. We can display large content.
View files one page at a time
syntax:
more filename
Navigation:
Space → Move forward one page.
Enter → Move forward one line.
q → Quit.
7. Less we can display small content.
View files interactively (like more but with scrolling).
Syntax:
less filename
Navigation:
Space / Page Down → Move forward one page.
b / Page Up → Move backward one page.
q → Quit.
8. Id Display the ID of the user (or) group.
Show user ID (UID), group ID (GID), and group memberships.
Syntax:
id [options] [username]
-u → Show only UID.
-g → Show only GID.
-G → Show all groups the user belongs to.
9. Ping check the network connectivity, status of network/server.
ping [options] hostname/IP
-c <N> → Send N packets and stop.
-i <N> → Set interval between pings.
-s <size> → Set packet size.
ping google.com # Send continuous pings
ping -c 5 google.com # Send only 5 packets
ping -s 1000 google.com # Send packets of 1000 bytes
10. Hostname Displays the owner name.
11. Ps used to list the currently running processes and their PIDs along with
some other information depends on different options.
ps [options]
-e → Show all processes.
-f → Full-format listing.
-u <user> → Show processes for a specific user.
-aux → Show all processes with detailed info.
Examples:
ps ----> Show processes of the current shell
ps -ef -------> Show all processes with full details
ps aux -----------> Show all processes with CPU/memory usage
ps -u user123 -----> Show processes for user 'user123'
12. Kill It is used for manually terminating the processes.
13. who lets you display the users currently logged in to your UNIX or Linux
operating system.
14. Whoami It displays the username of the current user when this command is
invoked.
15. Date displays current date and time
16. Cal display current year calendar
File compare Commands
---------------------
1. cmp used to compare the two files byte by byte and helps you to find out
whether the two files are identical or not.
2. diff allows you to compare two files line by line. It can also compare the
contents of directories.
3. comm compare two sorted files line by line and write to standard output;
the
lines that are common and the lines that are unique.
--------------------------------------------
1. wc used to find out number of lines, word count, byte and characters count
in
the files specified in the file arguments.
2. uniq command-line utility that reports or filters out the repeated lines
in a
file.