Showing posts with label find. Show all posts
Showing posts with label find. Show all posts

Wednesday, December 2, 2009

Bash Script To Find and Delete All Files Except Some When the filesystem reaches a specific size

#!/bin/sh

# This script checks if the /siebel filesystem reaches the warning limit of free space
# And then delete the old logs except those of last 12 hours

warninglimit=1572864    # This number is in KB, it's equal to 1.5 GB,
                        # KB is used to avoid floating point numbers

filesystem="/siebel"   # The filesystem to be monitored

size=`df -k $filesystem|grep $filesystem|awk '{ print $3; }'`   # Extract size of /siebel Filesystem in KB

if [ $size -le $warninglimit ]  # If fs size is less than warning lim.
    then
              find /siebel/siebsrvr/enterprises/ESPRD/$HOSTNAME/log -cmin +720 \( ! -name "ESPRD*" \) | xargs rm  # Delete log files that are more than 12 hours old from now (except those starting with "ESPRD")
fi

Credit goes to Osama Magdy for the above script.

"Find All EXCEPT" using bash script

find . \( ! -name "hobba*" \)

The above command invokes the "find" command asking it to search in the current directory (".") and return all files except those starting with the string "hobba". Note: Take care of the spaces as one more or less space character can ruin the whole script.

The above script is taken from a bash script written by Osama Magdy that deletes all files in a certain directory except one.

Thursday, August 27, 2009

Searching for a string inside multiple files and output the name of the files containing that string

find . -name "*" | grep -i ecomm | xargs ls -las | grep "Aug 27" | awk '{print $10}' | xargs grep -i string_to_search_for | grep -i "\.log" | awk '{print $1}'

where:

"ecomm" is part of the name of the file in which we should search for the string
"Aug 27" is the date the files were last modified
"$10" is the last column resulting from the "ls -las" command, this is the column containing the filename (the string is not searched for yet)
"string_to_search_for" the name is obvious :)
"\.log" all the files end with ".log"
"$1" is the filename containing the string

There are still two problems left (I don't have time right now to solve them):
  1. All the filenames end with the ":" character, it needs to be omitted
  2. Filenames are redundant, i.e. each file is repeated many times
Update: Problem number 2 solved using the "sort" and "uniq" commands which sort the results and then omit the repeated results, here is an example:

find . -name "*" | grep -i ecomm | xargs ls -las | grep "Aug 31" | awk '{print $10}' | xargs grep -i mbassiouny | grep -i "\.log" | awk '{print $1}' | sort | uniq

Monday, August 17, 2009

find files and copy them to a certain folder in bash (draft)

find . -name "*" | grep -i scomm | xargs ls -ltr | grep "Aug 13 10" | awk '{print $9}'

to use in the cp command (get the column number 9 which contains the file's full path

cp `find . -name "*" | grep -i scomm | xargs ls -ltr | grep "Aug 13 10" | awk '{print $9}'` /sfs/aamr/scomm/sbprd02

If the search_string contains any special characters (e.g. dot, slash, double quotations ...etc, then preceed it by a '\'

Sunday, August 16, 2009

How to search for a string inside multiple text files

find starting_folder -name "*" | grep -i file_name | xargs grep -i search_string

for example:

"find . -name "*" | grep -i hobba | xargs grep -i tito"

searches for the string "tito" inside all files whose name contains the word "hobba" inside the current folder.

Note that the "-i" parameter for the "grep" command makes the search case insensitive.

How to search for a file using the "find" command in bash (not case sensitive)

find start_directory -name "*" | grep -i search_string

e.g. "find . -name "*" | grep -i hobba" searches within the current directory and can return a file with name "aamr_HoBba.log" for example.