0% found this document useful (0 votes)
8 views

Practical No12 - Rohit

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

Practical No12 - Rohit

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

Practical No :12

Name:Rohit Sudhir Nikam


Roll No:30
File Check Operation
echo -e "Enter the name of the file:"
read file_name

# Check if the file exists


if [ -e "$file_name" ]; then
echo "$file_name exists."

# Check if it is a regular file


if [ -f "$file_name" ]; then
echo "$file_name is a regular file."
else
echo "$file_name is not a regular file."
fi

# Check if it is a directory
if [ -d "$file_name" ]; then
echo "$file_name is a directory."
else
echo "$file_name is not a directory."
fi

# Check if it is a block special file


if [ -b "$file_name" ]; then
echo "$file_name is a block special file."
else
echo "$file_name is not a block special file."
fi

# Check if it is a character special file


if [ -c "$file_name" ]; then
echo "$file_name is a character special file."
else
echo "$file_name is not a character special file."
fi

# Check if the file has a size greater than zero


if [ -s "$file_name" ]; then
echo "$file_name has a size greater than zero."
else
echo "$file_name is empty."
fi

else
echo "$file_name does not exist."
fi

Ex 3-A
file=abc.txt
if [ ! -f "$forloop.sh" ]; then
echo "$forloop.sh"
fi

Ex 3-B
file="abc.txt"

# Check if the file exists and is a regular file


test -f "$file" || echo "$file does not exist or is not a regular file."

Ex 3-C
# Define the file name
file="abc.txt"

# Check if the file exists and is a regular file


[ -f "$file" ] || echo "$file does not exist or is not a regular file."

Ex12_1
read -p "Enter the source file name: " source_file
read -p "Enter the destination file name: " destination_file

# Copy the source file to the destination


cp "$source_file" "$destination_file"

# Check if the copy was successful


if [ $? -eq 0 ]; then
echo "Successfully copied $source_file to $destination_file."
else
echo "Failed to copy $source_file."
fi
Ex12_2
# List all directories in the home directory
echo "Directories in your home directory:"
ls -d ~/[^.]*/*/ 2>/dev/null
Ex12_3

# List all files in the home directory


echo "Files in your home directory:"
ls -p ~ | grep -v / 2>/dev/null
Ex12_4

# Prompt user for a file or directory name


read -p "Enter a file or directory name: " path

if [ -f "$path" ]; then
echo "$path is a regular file."
echo "Choose an action:"
echo "1) Copy"
echo "2) Remove"
echo "3) Rename"
read -p "Enter your choice (1-3): " choice

case $choice in
1)
read -p "Enter the destination file name: " dest_file
cp "$path" "$dest_file"
echo "Copied $path to $dest_file."
;;
2)
rm "$path"
echo "Removed $path."
;;
3)
read -p "Enter the new name for the file: " new_name
mv "$path" "$new_name"
echo "Renamed $path to $new_name."
;;
*)
echo "Invalid choice."
;;
esac
elif [ -d "$path" ]; then
echo "$path is a directory."
else
echo "$path does not exist."
fi

You might also like