0% found this document useful (0 votes)
5 views11 pages

OS Practical-1

The document outlines basic Linux commands such as pwd, cd, ls, cat, and others, along with their usage and examples. It also includes two practical shell scripts: one for validating a date in the format dd-mm-yyyy and another for checking if an entered string is a palindrome. Each command and script is accompanied by its corresponding code and expected output.

Uploaded by

mrinank1318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

OS Practical-1

The document outlines basic Linux commands such as pwd, cd, ls, cat, and others, along with their usage and examples. It also includes two practical shell scripts: one for validating a date in the format dd-mm-yyyy and another for checking if an entered string is a palindrome. Each command and script is accompanied by its corresponding code and expected output.

Uploaded by

mrinank1318
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PRACTICAL-1

 Study Basic Command of Linux :-


1. pwd :- Prints current working directory.
Path=$ pwd
/c/users/piet

2. cd :- change current working directory.


Path=$ cd Desktop

2.1. cd .. :-
Path=$ cd ..

Enrollment no: 2303031460069 Page no: 18


3. ls :- List contents of directory.
Path= $ ls

3.1. ls-a :-list all the contents hidden in the directory.


Path= $ ls -a

Enrollment no: 2303031460069 Page no: 18


4. ls –r :-prints sub directories.
Path = $ ls -r

4.1. ls –al :- Display all details of directories.

Enrollment no: 2303031460069 Page no: 18


5. cat :- Display file contents in the terminal.
Path=$ cat lsc.txt

6. Head :- Prints First 10 lines in the file.


Path= $ head lsc.txt

Enrollment no: 2303031460069 Page no: 18


7. Tail :-Prints Last 10 lines in the
file. Path= $ tail lsc.txt

8. touch:- creates a new file.


Path= $ touch lsc

9. mv :- Move file from one location to another.


Path=$ mv parul.txt lsc

Enrollment no: 2303031460069 Page no: 18


10. mkdir:-create a new Directory.

Path=$ mkdir lsc

10. rmdir:-delete a directory.

Path= $ rmdir lscc

11. cp :- Copy contents from one to another.

Path=$ cp -r parul university.

Enrollment no: 2303031460069 Page no: 18


12. ECHO:-

Path=$ echo lsc

13. whoami :- Displays the user

Path=$ whoami

14. wc :- counts the number of words in the directory

(Word count)
Path= $ wc ls.txt

Enrollment no: 2303031460069 Page no: 18


Practical – 4

Aim: Write a shell script to validate the entered date. (eg. Date format is:
dd-mm-yyyy).
Code:
read -p "Enter a date (dd-mm-yyyy): " date_input
IFS='-' read -r day month year <<< "$date_input"
if [[ -n $day && -n $month && -n $year && $day =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $year
=~ ^[0-9]+$ ]]; then
if date -d "$year-$month-$day" >/dev/null 2>&1; then
echo "Valid date."
else
echo "Invalid date."
fi
else
echo "Invalid format. Please enter the date in dd-mm-yyyy format."
Fi

Enrollment no: 2303031460069 Page no: 18


Practical – 5

Aim: Write a shell script to check entered string is palindrome or not.


Code:
read -p "Enter a string: " input_str
input_str=$(echo "$input_str" | tr 'A-Z' 'a-z')
reversed_str=""
for (( i=${#input_str}-1; i>=0; i-- )); do
reversed_str+="${input_str:$i:1}"
done
if [[ "$input_str" == "$reversed_str" ]]; then
echo "The string is a palindrome."
else
echo "The string is not a palindrome."
Fi

Enrollment no: 2303031460069 Page no: 18


Practical – 4
Aim: Write a shell script to validate the entered date. (eg. Date format is:
dd-mm-yyyy).
Code:
read -p "Enter a date (dd-mm-yyyy): " date_input
IFS='-' read -r day month year <<< "$date_input"
if [[ -n $day && -n $month && -n $year && $day =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $year
=~ ^[0-9]+$ ]]; then
if date -d "$year-$month-$day" >/dev/null 2>&1; then
echo "Valid date."
else
echo "Invalid date."
fi
else
Enrollment no: 2303031460069 Page no: 18
echo "Invalid format. Please enter the date in dd-mm-yyyy format."
Fi

Practical – 5
Aim: Write a shell script to check entered string is palindrome or not.
Code:
read -p "Enter a string: " input_str
input_str=$(echo "$input_str" | tr 'A-Z' 'a-z')
reversed_str=""
for (( i=${#input_str}-1; i>=0; i-- )); do
reversed_str+="${input_str:$i:1}"
done
if [[ "$input_str" == "$reversed_str" ]]; then
echo "The string is a palindrome."
else
echo "The string is not a palindrome."
Fi

Enrollment no: 2303031460069 Page no: 18

You might also like