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 FunctionalitiesFile 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 $file is $c echo Number of words in $file is $w echo Number of lines in $file is $l
Script:
$ nl geeks.txt | sort -nr | cut -f 2-
Script:
File Test Operatorscat geeks.txt | xargs printf "%s\n" | sort | uniq -c | sort -nr | awk '{print $2,$1}'
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4