Explain Different GPU ( clear ,cal , date, wc, who ) in linux with syntax and example
1. clear
Purpose: The clear command is used to clear the terminal screen. It removes all
previous commands and their output, providing a clean slate for the user.
Syntax:
$ clear
After executing clear, the terminal screen will be completely blank, showing only the
command prompt at the top.
2. cal
The cal command displays a calendar of the current month. It can also be used to
display a specific month, year, or a range of years.
Syntax:
cal [options] [[month] year]
Examples:
● To display the calendar for the current month:
$ cal
This will show the calendar for the current month with the current day highlighted.
● To display the calendar for a specific month and year (e.g., October 2025):
$ cal 10 2025
● To display the calendar for the entire year (e.g., 2025):
$ cal 2025
● To display the calendars for the previous, current, and next months:
$ cal -3
3. date
Purpose: The date command is used to display the system's current date and time. It
can also be used to set the system date and time (requires superuser privileges) or to
format the output in various ways.
Syntax:
date [options] [+format]
Examples:
● To display the current date and time:
$ date
The output will be similar to: Wed Sep 17 15:12:11 IST 2025
● To format the date and time: The date command uses format specifiers to
customize the output.
○ Display date in YYYY-MM-DD format:
$ date +%Y-%m-%d
Output: 2025-09-17
○ Display time in HH:MM:SS format:
$ date +%H:%M:%S
Output: 15:12:11
○ Combine date and time with a custom string:
$ date +"Today is %A, %B %d, %Y at %r"
Output: Today is Wednesday, September 17, 2025 at 03:12:11 PM
4. wc
Purpose: The wc (word count) command is used to count the number of lines, words,
and characters (or bytes) in a specified file.
Syntax:
wc [options] [file]
Examples: Let's assume we have a file named document.txt with the following content:
This is the first line.
This is the second line.
And the third.
● To count lines, words, and characters in a file:
$ wc document.txt
Output: 3 11 63 document.txt (3 lines, 11 words, 63 characters)
● To count only the number of lines (-l):
$ wc -l document.txt
Output: 3 document.txt
● To count only the number of words (-w):
$ wc -w document.txt
Output: 11 document.txt
● To count only the number of characters (-c):
$ wc -c document.txt
Output: 63 document.txt
5. who
Purpose: The who command displays information about users currently logged in on
the system. It is a simple way to see who is using the system.
Syntax:
who [options]
Examples:
● To display who is logged in:
$ who
Output will be similar to:
user1 pts/0 2025-09-17 15:12 (192.168.1.10)
user2 pts/1 2025-09-17 15:15 (192.168.1.12)
The columns represent:
○ Username: The login name of the user.
○ Terminal: The terminal line (e.g., pts/0).
○ Login Time: The time the user logged in.
○ Origin: The host from which the user connected.
● To display who is logged in with column headers (-H):
$ who -H
Output:
NAME LINE TIME COMMENT
user1 pts/0 2025-09-17 15:12 (192.168.1.10)
user2 pts/1 2025-09-17 15:15 (192.168.1.12)
● To display your own login information (whoami): A related command,
whoami, is a common shortcut to display the effective username of the current
user.
$ whoami
Output: user1 (or your current username)
file handling- ls ,cat ,cp, mv , rm commands
1. ls
Purpose: The ls command is used to list files and directories in the current working
directory. Syntax:
ls [options] [file/directory]
Examples:
To list files and directories in the current directory:
$ ls
● This will display a simple list of files and directories, like: documents photos
script.sh notes.txt
To list files in a long format (-l):
$ ls -l
This provides detailed information, including file permissions, owner, group, size, and
last modification date.
total 8
drwxr-xr-x 2 user group 4096 Sep 17 10:00 documents
-rw-r--r-- 1 user group 1024 Sep 17 10:05 notes.txt
● To list all files, including hidden files (-a):
$ ls -a
● This shows all files, including those that begin with a dot (.), which are hidden.
To combine options (-la):
$ ls -la
● This command combines the long listing format with the option to show all files,
including hidden ones.
2. cat
Purpose: The cat (concatenate) command is used to display the content of one or
more files to the standard output. It is also used to create files and combine them.
Syntax:
cat [options] [file(s)]
Examples: Let's assume we have a file named message.txt with the content: "Hello,
this is a test message."
To display the content of a single file:
$ cat message.txt
● Output: Hello, this is a test message.
To display the content of multiple files:
$ cat file1.txt file2.txt
● This will display the content of file1.txt immediately followed by the content
of file2.txt.
To create a new file:
$ cat > newfile.txt
● After running this command, you can type your content directly into the terminal.
Press Ctrl + D to save and exit.
3. cp
Purpose: The cp (copy) command is used to copy files and directories.
Syntax:
cp [options] source destination
Examples:
To copy a file to a different name in the same directory:
$ cp file.txt file_copy.txt
● This creates a new file named file_copy.txt that is an exact copy of
file.txt.
To copy a file to a different directory:
$ cp file.txt /home/user/documents/
● This copies file.txt from the current directory into the
/home/user/documents/ directory.
To copy a directory recursively (-r):
$ cp -r photos /home/user/backup/
● This command copies the entire photos directory, including all its files and
subdirectories, to the /home/user/backup/ directory.
4. mv
Purpose: The mv (move) command is used to move or rename files and directories.
Syntax:
mv [options] source destination
Examples:
To rename a file in the same directory:
Bash
$ mv oldname.txt newname.txt
● The file oldname.txt is renamed to newname.txt.
To move a file to a different directory:
$ mv report.doc /home/user/archives/
● The report.doc file is moved from the current directory to the
/home/user/archives/ directory. It is no longer in the original location.
5. rm
Purpose: The rm (remove) command is used to delete files and directories. Use this
command with caution, as deleted files are not moved to a trash or recycle bin and are
often difficult to recover.
Syntax:
rm [options] [file/directory]
Examples:
To delete a single file:
$ rm unwanted_file.txt
● This command permanently deletes unwanted_file.txt.
To delete multiple files:
$ rm file1.txt file2.txt file3.txt
To delete a directory and all its contents recursively (-r):
$ rm -r xyz/
● This command deletes the xyz directory along with everything inside it.
To force a deletion without prompting (-f):
$ rm -f file.txt
● The -f option forces the removal and overrides any file permissions that might
prevent deletion.
LISTING FILE NAMES, USING META CHARACTERS ( * , ? ,[ ] )
Or
TYPES OF METACHARACTERS AND OPERATORS IN LINUX
To use shell more efficiently, the bash shell uses certain special characters, called as
metacharacters and operators.
· Metacharacters helps to match one or more files without typing each filename
completely.
· Operators let you direct information from one command or file to another command or
file.
Using file-matching metacharacters
To save you some keystrokes and to refer easily a group of files, the bash shell lets to
use metacharacters. Anytime we need open, delete, list a file or directory, we can use
metacharacters to match the files we want. Here are some useful metacharacters for
matching filenames:
* — This matches any number of characters.
? — This matches any one character.
[...] — This matches any one of the characters between the brackets, which can
include a dash-separated range of letters or numbers.
To try out some of these file-matching metacharacters, go to an empty directory
and create some files. Here’s an example of how to create some empty files using
touch command. (Although the touch command is more commonly used to set assign
the current date and time to an existing file than to create new ones):
Create Some Empty Files
$ touch apple banana grape grapefruit watermelon
The next few commands show how to use shell metacharacters to match filenames.
And these metacharacters can be used as arguments to the ls command. Using the
metacharacters, we can match the filenames just created with the touch command.
Type the following commands and see if you get the same responses:
$ ls a* ------ matches any file that begins with an a. (apple)
apple
$ ls g* ------ matches any file that begins with an g. (grape grapefruit)
grape grapefruit
$ ls g*t ------ files beginning with g and ending in t are matched ( grapefruit ).
grapefruit
$ ls *e* ------ any file that contains an e in the name is matched ( apple , grape ,
grapefruit , watermelon ).
apple grape grapefruit watermelon
$ ls *n* -------- any file that contains an n is matched( banana ,watermelon
).
banana watermelon
Here are a few examples of pattern matching with the question mark
metacharacters
$ ls ????e ---- Matches any five-character file that ends in e ( apple , grape ).
apple grape
$ ls g???e* ---- matches any file that begins with g and has e as its fifth character (
grape , grapefruit ).
grape grapefruit
Here are a few examples of using brackets to do pattern matching:
$ ls [abw]* ----- any file beginning with a , b , or w is matched.
apple banana watermelon
$ ls [agw]*[ne] ---- any file that begins with a , g , or w and also ends with either
n or e is matched.
apple grape watermelon
You can also include ranges within brackets. For example:
$ ls [a-g]* -- any filenames beginning with a letter from a through g is matched.
apple banana grape grapefruit
Using file-redirection metacharacters
Commands receive data from standard input and send it to standard output. Standard
input is normally user input from the keyboard, and standard output is normally
displayed on the screen. Using pipes ( | ), you can provide standard output of one
command to another command as the standard input. With files, you can use less than (
< ) and greater than ( > ) signs to direct data to and from files.
Here are the file redirection characters:
• < — Direct the contents of a file as input to the command. (because many commands
take a file name as an option, the < key is not usually needed).
• > — Direct the output of a command to a file, overwriting any existing file.
• >> — Direct the output of a command to a file, adding the output to the end of the
existing file.
Here are some examples of command lines where information is directed to and from
files:
$ mail root < ~/.bashrc ---- the contents of the .bashrc file in the home directory are
sent in a mail message to the computer’s root user.
$ man chmod | col -b > /tmp/chmod
command line formats the chmod man page (using the man command), removes extra
back spaces ( col -b), and sends the output to the file /tmp/chmod (erasing the previous
/tmp/chmod file, if it exists).
$ echo "Finished project on $(date)" >>project.txt
Following text being added to the user’s project.txt file:
Finished project on Wed Jun 25 13:46:49 PST 2008.
You could also pipe the output of the previous command to another command.
For example, the following command line would send the line just shown in a mail
message to the user boss@example.com:
$ echo "Finished project on $(date)"|mail -s 'Done' boss@example.com
Explain Concept of directory , home directory , directory handling commands- cd
, mkdir, rmdir,pwd in linux
Concept of Directories
In Linux, a directory is a folder used to organize and store files and other
directories. The file system is structured in a hierarchical tree-like fashion, starting from
the root directory (/).
Home Directory
The Home Directory (/home/<username>) is a special directory for a specific user. It is
the default directory where a user is placed after logging into the system. It is designed
to store all of the user's personal files, documents, and user-specific configuration
settings. The home directory is a safe place for a user to create, modify, and delete files
without affecting the rest of the system.
Directory Handling Commands
These are essential commands for navigating and managing directories in the Linux
command line.
1. pwd
Purpose: The pwd (print working directory) command displays the full path of the
current directory you are in. It helps you orient yourself within the file system.
Syntax:
pwd
Example:
● To find your current location:
$ pwd
/home/user/documents
● This output shows that you are currently inside the documents directory, which is
inside the user directory, which is inside the home directory.
2. cd
Purpose: The cd (change directory) command is used to change your current location
(working directory).
Syntax:
cd [directory_path]
Examples:
● To change to the home directory:
$ cd
● Simply typing cd without any arguments takes you back to your home directory
(/home/user).
● To change to a specific directory:
$ cd /home/user/documents
● This changes your working directory to the documents directory using an
absolute path.
● To change to a subdirectory:
$ cd projects
● If you are in /home/user, this command will change your location to
/home/user/projects. This uses a relative path.
● To go back to the parent directory:
$ cd ..
● This command moves you up one level in the directory tree. If you were in
/home/user/documents, you will be moved to /home/user.
3. mkdir
Purpose: The mkdir (make directory) command is used to create a new directory.
Syntax:
mkdir [options] directory_name
Examples:
● To create a new directory:
$ mkdir new_folder
● This creates a new directory named new_folder in your current location.
● To create multiple directories at once:
$ mkdir dir1 dir2 dir3
●
● This creates dir1, dir2, and dir3 in the current directory.
● To create a nested directory structure (-p):
$ mkdir -p project/src/main
● The -p (parents) option creates the parent directories (project and src) if they do
not already exist, allowing you to create the full path in one command.
4. rmdir
Purpose: The rmdir (remove directory) command is used to delete an empty directory.
Syntax:
rmdir [options] directory_name
Examples:
● To remove an empty directory:
$ rmdir old_folder
● This will delete the directory named old_folder, but only if it contains no files or
subdirectories.
● To remove multiple empty directories:
$ rmdir empty_dir1 empty_dir2
Note: If a directory is not empty, rmdir will fail. To delete a non-empty directory and all its
contents, you should use the rm command with the -r (recursive) option: rm -r
directory_name.