Shell Script to Perform Operations on a File (original) (raw)

Last Updated : 23 Jul, 2025

Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file.

For our convenience, we create a file named 'geeks.txt' and another .sh file (or simply run on the command line) to execute different functions or operations on that file. Operations may be reading the contents of the file or testing the file type. These are being discussed below with proper examples:

File Reading Functionalities

File reading is an interesting task in a programmer's life. Shell scripting offers some functionalities for reading the file, reversing the contents, counting words, lines, etc.

Script:

#!/bin/bash read -p "Enter file name : " filename while read line do echo $line done < $filename

Script:

#! /bin/bash

echo Enter the filename read file c=cat $file | wc -c w=cat $file | wc -w l=grep -c "." $file echo Number of characters in fileisfile is fileisc echo Number of words in fileisfile is fileisw echo Number of lines in fileisfile is fileisl

Script:

$ nl geeks.txt | sort -nr | cut -f 2-

Script:

cat geeks.txt | xargs printf "%s\n" | sort | uniq -c | sort -nr | awk '{print 2,2,2,1}'

File Test Operators

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

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

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -d $file_name ] then echo "$file_name is a directory" else echo "$file_name is not a directory" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -e $file_name ] then echo "$file_name exist" else echo "$file_name not exist" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -f $file_name ] then echo "$file_name is file" else echo "$file_name is not file" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -r $file_name ] then echo "$file_name is readable" else echo "$file_name is not readable" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -s $file_name ] then echo "$file_name has size>0" else echo "$file_name has size= 0" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -w $file_name ] then echo "$file_name is writable" else echo "$file_name is not writable" fi

Output:

Script:

#! /bin/bash echo -e "Enter the name of the file : \c" read file_name

if [ -x $file_name ] then echo "$file_name is executable" else echo "$file_name is not executable" fi

Output:

Rename & delete file: To rename the file, we use the 'mv' command which changes the name of the file and 'rm' to delete the file.

As we can see below the command line, breakingBad file (after renaming ) and deleting it with 'rm' command, it is no longer there.