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

examplesShellscript

The document provides a series of Bash scripts for various file and system operations, including counting files, checking disk usage, retrieving system information, renaming files, modifying file permissions, checking internet connectivity, and searching for files in a directory. Each script is accompanied by a brief explanation of its functionality. Additionally, it includes usage examples for the file search script.

Uploaded by

mawoha3262
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

examplesShellscript

The document provides a series of Bash scripts for various file and system operations, including counting files, checking disk usage, retrieving system information, renaming files, modifying file permissions, checking internet connectivity, and searching for files in a directory. Each script is accompanied by a brief explanation of its functionality. Additionally, it includes usage examples for the file search script.

Uploaded by

mawoha3262
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

File count

#!/bin/bash
file_count=$(ls | wc -l)
echo "Number of files: $file_count"

file_count=$(ls | wc -l):

 ls lists the contents of the current directory.

 wc -l counts the number of lines in the output from ls, effectively


counting the items (files and directories).

 The result is stored in the variable file_count.

Disk usage

#!/bin/bash
df -h

df: Reports file system disk space usage.


-h: Displays the output in a human-readable format (e.g., GB, MB).

System Info

#!/bin/bash
uname -a

Prints system information, including the kernel version

File Rename

#!/bin/bash
old_name="old.txt"
new_name="new.txt"
mv "$old_name" "$new_name"

Renames a file from “old.txt” to “new.txt.”

File Permissions

#!/bin/bash
file="file.txt"
ls -llh
chmod +x "$file"
ls -llh

Check Internet Connection

#!/bin/bash
ping -c 5 google.com

Checks internet connectivity by pinging Google.

File search in current directory

#!/bin/bash

# Check if a search pattern is provided


if [ -z "$1" ]; then
echo "Usage: $0 <filename-pattern>"
exit 1
fi
# Search for the file in the current directory and
subdirectories
find . -type f -name "$1"

find: Command to search for files and directories.

. : Start the search from the current directory.

-type f: Only look for regular files (ignores directories).

-name "$1": Match filenames based on the pattern provided as


the first argument ($1).

How to run

Option1:List all the file,

./filesearch.sh "*.*"

Option2: only txt file

./filesearch.sh "*.txt"

You might also like