I have a bash script that I want to list all my files older than x hours. But I have some errors. What would be the correct syntax?!
find: missing argument to-exec'
./script.sh: line 9: [[0: command not found [root#localhost home]#
prev_count=0
path=/home/alex/
find $path -type f -mmin +2 -exec ls -ltrh\; > /home/test.txt
count=$(cat /home/test.txt | wc -l)
if ["$prev_count" -lt "$count"]
then
echo "This are the files that need to be deleted" >> $MESSAGE
echo "+----------------------------------------+" >> $MESSAGE
echo "" >> $MESSAGE
fi```
Related
I am writing a script to check the disk usage and list the files larger than 1MB in the path input by the user.
#!/bin/bash
DISK_USAGE=$(df -h /dev/xvda1 | awk '{gsub("%","");print $5}' | tail -1)
THRESHOLD=80
if [ $DISK_USAGE -ge $THRESHOLD ]
then
read -p "Enter the path for log files: " PATH
echo "Below is the list of large log files which are taking space:"
echo $("find $PATH -type f -size +1M")
fi
Below is the output of this script:
Enter the path for log files: /var/log
Below is the list of large files which are taking space:
./sample-script.sh: line 17: find /var/log -type f -size +1M: No such file or directory
For some reason find command is not able to pick any path that I enter as an input.
What could be a possible reason and the solution?
echo $("find $PATH -type f -size +1M")
This attempts to run a command called: find $PATH -type f -size +1M (with the $PATH expanded). As there is no such command you get the error.
Your quotes should be outside the $(...)
echo "$(find $PATH -type f -size +1M)"
Better still, drop the echo as it is not required:
find $PATH -type f -size +1M
As #alexcs notes, you shouldn't use PATH as a variable name because it is already used by the system (in fact you shouldn't use any all-caps variable name) and you should quote its use as the user may have provided something containing whitespace:
read -p "Enter the path for log files: " LogPath
# ...
find "$LogPath" -type f -size +1M
Consider:
echo $("echo '1..4..7'; echo 'a b c'")
"echo '1..4..7'; echo 'a b c'"
echo $(echo '1..4..7'; echo 'a b c')
echo "$(echo '1..4..7'; echo 'a b c')"
echo '1..4..7'; echo 'a b c'
I need to write script in loop which will count the number of files and directories and indicates which grater and by how much. Like etc: there are 10 more files than directories.
I was trying something like that but it just show files and directories and I don't have idea how to indicates which is greater etc. Thanks for any help
shopt -s dotglob
count=0
for dir in *; do
test -d "$dir" || continue
test . = "$dir" && continue
test .. = "$dir" && continue
((count++))
done
echo $count
for -f in *; do
"$fname"
done
Here is a recursive dir walk I used for something a while back. Added counting of dirs and files:
#!/bin/sh
# recursive directory walk
loop() {
for i in *
do
if [ -d "$i" ]
then
dir=$((dir+1))
cd "$i"
loop
else
file=$((file+1))
fi
done
cd ..
}
loop
echo dirs: $dir, files: $file
Paste it to a script.sh and run with:
$ sh script.sh
dirs: 1, files: 11
You can use the find command to make things simplier.
The following command will list all the files in the given path:
find "path" -mindepth 1 -maxdepth 1 -type f
And also using the -type d you will get the directories.
Piping find into the wc -l will give you the number instead of the actual file and directory names, so:
root="${1:-.}"
files=$( find "$root" -mindepth 1 -maxdepth 1 -type f | wc -l)
dirs=$( find "$root" -mindepth 1 -maxdepth 1 -type d | wc -l)
if [ $files -gt $dirs ]; then
echo "there are $((files - dirs)) more files"
elif [ $files -lt $dirs ]; then
echo "there are $((dirs - files)) more dirs"
else
echo "there are the same"
fi
Use could use find to get the number of files/folders in a directory. Use wc -l to count the number of found paths, which you could use to calculate/show the result;
#!/bin/bash
# Path to search
search="/Users/me/Desktop"
# Get number of files
no_files=$(find "$search" -type f | wc -l )
# Number of folders
no_folders=$(find "$search" -type d | wc -l )
echo "Files: ${no_files}"
echo "Folders: ${no_folders}"
# Caculate dif
diff=$((no_files - $no_folders))
# Check if there are more folders or files
if [ "$diff" -gt 0 ]; then
echo "There are $diff more files then folders!"
else
diff=$((diff * -1 ) # Invert negative number to positive (-10 -> 10)
echo "There are $diff more folders then files!"
fi;
Files: 13
Folders: 2
There are 11 more files then folders!
I am trying to grep a particular pattern from a group of files stored in a directory thru shell script. However script scans through the files but it is not fetching me the result. I have 5 files with the pattern as finishing with status COMPLETE
Here is the code,could you please help on the issue
#!/bin/bash
dt=$(date --d='7 day ago' +'%Y%m%d')
countT=$("find /home/arun/file_status -type f -name "product_feed_*stock*_${dt}*.out" -exec grep "finishing with status COMPLETE" {} \;" | wc -l)
echo $countT
if [ $countT -eq 5 ]
then
echo "Hello"
else
echo "Hai"
fi
The below is the error:
find /home/arun/file_status -type f -name product_feed_stock_20170504*.out -exec grep finishing: No such file or directory 0 Hai
Need to remove quotes in find command.
#!/bin/bash
dt=$(date --d='7 day ago' +'%Y%m%d')
countT=$(find /home/arun/file_status -type f -name "product_feed_stock_${dt}*.out" -exec grep "finishing with status COMPLETE" {} \;| wc -l)
echo $countT
if [ $countT -eq 5 ]
then
echo "Hello"
else
echo "Hai"
fi
#!/bin/bash
#sh j
find . -name "*first*" && echo "file found" || echo "file not found"
read -p "Run command $foo? [yn]" answer
case "$answer" in
y*) find . -type f -exec rename 's/(.*)\/(.*)first(.*)/$1\/beginning_$2changed$3/' {} + ;;
n*) echo "not renamed" ;;
esac
fi
I want the script to loop through folder and subfolders and find files that contain certain string and then have an option to rename the file or let it be(That is the y/n option) after selection the script should continue finding.
Also i have a problem that says "syntax error unexpected token 'fi' "
Try this:
#bin/bash
handle_file(){
local file=$0
local pattern=some_pattern
if [[ $(grep -c ${pattern} ${file}) -gt 0 ]];
then
......................................
do anything you want with your ${file}
......................................
fi
}
export -f handle_file
find . -type f -exec bash -c 'handle_file "$#"' {} \;
handle_file is a function that will be invoked as handle_function <filename>, so the <filename> is available as $0 inside the function.
I want to use a find command in order to obtain files older than 8640 min and send the result in a email body. I used this script that makes use of a file - ATTACH_FILE - containing the results of the find command:
#!/bin/sh
ATTACH_FILE="/pub/email_attach.txt"
WORK_DIR="/pub/"
rm -f $ATTACH_FILE
find $WORK_DIR -maxdepth 1 -name '*x.rsd' -type f -daystart -mmin +8640 -exec echo {} >> $ATTACH_FILE \;
if [ ! -z $ATTACH_FILE ]; then
FILESIZE=$(stat -c%s "$ATTACH_FILE" 2>> getLatestErr.log)
echo $ATTACH_FILE "size $FILESIZE bytes"
if [ $FILESIZE -gt 0 ]; then
cat $ATTACH_FILE | mail -s "Test "$TODAY mmm#server.com
fi
fi
How can I get the same result by putting a message in the body of the email without using the auxiliary file ATTACH_FILE ?
You can use the -e option to mail. That tells it not to do anything if the input is empty.
find $WORK_DIR -maxdepth 1 -name '*x.rsd' -type f -daystart -mmin +8640 -print | mail -e -s "Test "$TODAY mmm#server.com
To expand on my comment above:
Assign to an array variable and use printf to separate the found items with a newline character:
#!/bin/bash
WORK_DIR="/pub/"
FILE_LIST=($(find $WORK_DIR -maxdepth 1 \
-name '*x.rsd' -type f \
-daystart -mmin +8640 ))
if [ -n "${FILE_LIST[0]}" ]; then
printf '%s\n' "${FILE_LIST[#]}" | mail -s "Test "$TODAY mmm#server.com
fi
I exchanged /bin/sh with /bin/bash, as the question is tagged with [bash].