Find files with certain date strings - linux

I have a number of files with dates in their names:
file_19990101.txt
file_19990102.txt
...
file_20031231.txt
I want to generate an ASCII file with all files up to the date 20010320, and then a second file with files from 20010321 to 20031231. How can this be accomplished using bash commands?
My current solution is to do a bunch of find commands:
find . -name "file_1999*" -print > index.txt
find . -name "file_2000*" -print >> index.txt
find . -name "file_200101*" -print >> index.txt
find . -name "file_200102*" -print >> index.txt
find . -name "file_2001030*" -print >> index.txt
find . -name "file_2001031*" -print >> index.txt
find . -name "file_20010320*" -print >> index.txt
etc. But there must be an easier way to accomplish this task!

This might work:
for f in file_*
do
ymd=${f#file_}
ymd=${ymd%.txt}
[[ "${ymd}" < "20010321" ]] && echo "${f}" >>index1 || echo "${f}" >>index2
done
Loop over the glob, strip the prefix and suffix, compare with the cutoff and append to one file or the other.
Edit: I missed the second condition.
#!/bin/bash
for f in file_*
do
ymd=${f#file_}
ymd=${ymd%.txt}
if [[ "${ymd}" < "20010321" ]]
then
echo "${f}" >>index1
elif [[ "${ymd}" > "20031231" ]]
then
:
else
echo "${f}" >>index2
fi
done

Related

bash delete script don't work

i have a bash script that should all files that are not .avi, .mp4 and .mkv.
This is what i tried:
#!/bin/bash
FILES=$(find /home/mattia/test/ -type f -name '*.*')
for f in $FILES
do
ext=${f#*.}
echo $ext
if [[ "$ext" != "mp4" || "$ext" != "mkv" || "$ext" != "avi" ]]; then
rm -f $f
echo deleted
fi
done
But this script deletes all files.
Aside from changing || to &&, the script is fragile,
for example it won't work if any of the files contains spaces.
In general it's not safe to store the output of find in a variable for looping, so this practice should be avoided.
In any case, you don't need a loop, find can do this all by itself, and safer:
find /home/mattia/test/ -type f ! \( -name '*.mp4' -o -name '*.mkv' -o -name '*.avi' \) -print -delete
Note that instead of !, you could use the more intuitive -not,
but keep in mind that it is not POSIX compliant.
It is logical that
[[ "$ext" != "mp4" || "$ext" != "mkv" || "$ext" != "avi" ]]
is always true. For example: if the file name is movie.avi, "$ext" != "mp4" will be true and therefore the complete if will always be true.
As always, there are many ways to solve this. Janos did it in find; an alternative would be:
find /find /home/mattia/test/ -type f -name '*.*'home/mattia/test/ -type f -name '*.*' |
egrep -v '.mp4$|.mkv$|.avi$' |
xargs rm
Or, in your loop:
#!/bin/bash
FILES=$(find /home/mattia/test/ -type f -name '*.*')
for f in $FILES
do
ext=${f#*.}
echo $ext
case "$ext" in
(mp4) echo "Keeping $f"
;;
(mkv) echo "Not deleting $f"
;;
(avi) echo "Holding on to $f"
;;
(*) rm -f "$f"
echo "deleted $f"
;;
esac
done
(which should work as long as your file names don't have spaces)
Or sorting-out the if-condition
[[ ! ( "$ext" = "mp4" || "$ext" = "mkv" || "$ext" = "avi" ) ]]

Linux filename check if exist and delete

I have some problem in Shell scripting.
So I have to write a script that find every file in a directory with this string: "gom". So i found all of them. After I need to cut it off, and compare that the remaining filename is exist. If exist i need to remove the file that contains the string.
Example: there are 5 files: algomb, gomba, alb, algomba, alba.
I need to find the filenames with "gom". algomb, gomba, algomba.
After it i need to cut the "gom". And a remaining filenames is exist I need to remove the file with "gom" string.
So after the cutting "gom" there will be 5 files: alb, ba, alb, alba, alba
So there are two files that is extist: alb, alba....I need to remove the following files: algomb, albomba.
After it the will be 3 files: gomba, alb, alba.
Sorry for my bad english.
I can find, I can remove, but I cant compare the filenames.
Here's my code:
#!/bin/bash
sz="gom"
talal=`find . -type f -name "*$sz*" -exec basename {} \;`
ossz=`find . -type f -exec basename {} \;`
c=`echo ${talal%%:*}${talal##*:}`
for c in ossz; do
if [ ! -d ]; then
echo "This is a directory"
else
if [ -f ];
then
find .-type f -name "*$sz*" -exec basename {} \;
else
echo ${talal%%:*}${talal##*:}
fi
fi
done
So this is works. This echo ${talal%%:*}${talal##*:} is give back the filename without "gom". But I cant compare these values with find . -type f -exec basename {} \; results.
Sorry for my bad english.
Can sombody help me?
Best regards, Richard
I would do it this way, without find.
shopt -s globstar
for gom_file in **/*gom*; do
# Skip non-regular files
[[ -f $gom_file ]] || continue
# Delete the file if the gom-free file exists
[[ -f ${gom_file/$sz//} ]] && rm "$gom_file"
done
Using find is slightly less efficient, since you need to fork a new shell for each file:
find . -type f -name "*gom*" -exec bash -c 'echo rm -f "${1/gom/}"' {} \;
Run this to test that it outputs the rm commands you want to execute, then run it again with echo removed.
I think you want to use those bash features:
array (declare -a)
hashtable (declare -A)
regex ( ${a/b/c} ${a//b/c} )
Here is an example as a general idea:
#!/bin/bash
sz="gom"
declare -a ta
declare -A ossz
i=0
while read -r -d $'\0' ff
do ta[$i]=$ff
i=$((i+1))
done < <(find . -type f -name "*$sz*" -print0)
while read -r -d $'\0' ff
do ossz[${ff##*/}]=1
done < <(find . -type f -print0)
for ff in "${ta[#]}"
do subff=${ff/$sz/}
subff=${subff##*/}
if [ _${ossz[$subff]} = _1 ]
then echo "$ff"
fi
done

Recursive unrar and deletion in directory and all subdirectories

I'm trying to work on a script that will crawl my Plex media folder, find any header ".r00" files, extract them in their own directory, and trash the archive zips after it's done. I have two options I've been playing around with. Combined they do what I want, but I would like to have it all in one nice little script.
Option 1:
This script opens the "LinRAR" GUI, makes me navigate to a specific directory, finds and extracts any .r00 file in that directory, and successfully deleted all archive zips.
while true; do
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
if [[ ! -d $dir ]]; then
echo "$dir: Wrong Directory" >&2
else
( cd "$dir" && for f in *.r00; do [[ -f $f ]] || continue; rar e "$f" && rm "${f%00}"[0-9][0-9]; done )
fi
else
echo "$bold Selection cancelled $bold_off" >&2
exit 1
fi
zenity --title="What else...?" --question --text="More work to be done?" || break
done
Option 2:
This script cd's to my Plex folder, recursively finds any .r00 files, extracts to my /home/user folder, and does not remove the archive zips.
(cd '/home/user/Plex');
while [ "`find . -type f -name '*.r00' | wc -l`" -gt 0 ];
do find -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;;
done
I would like to have something that takes the first working script, and applies the recursive find to all folders inside of /Plex instead of only letting me navigate to one folder at a time through the "LinRAR" GUI.
No need to use cd. find takes a starting directory.
It's that dot (.) you passed to it.
Also added another (more sane) alternative for the find & loop:
#!/bin/bash
while true; do
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
if [[ ! -d $dir ]]; then
echo "$dir: Wrong Directory" >&2
else
# Alternative 1 - a little more comfortable
files="$(find "${dir}" -type f -name '*.r00')"
for file in ${files}; do
rar e "${file}" && rm "${file}"
done
# Alternative 2 - based on your original code
while [ "`find "${dir}" -type f -name '*.r00' | wc -l`" -gt 0 ]; do
find "${dir}" -type f -name "*.r00" -exec rar e -- '{}' \; -exec rm -- '{}' \;;
done
fi
else
echo "$bold Selection cancelled $bold_off" >&2
exit 1
fi
zenity --title="What else...?" --question --text="More work to be done?" || break
done
According to the comments, I ran a small example of this code and it works perfectly fine:
#!/bin/bash
if dir=$(zenity --title="LinRAR by dExIT" --file-selection --directory); then
if [[ ! -d $dir ]]; then
echo "$dir: Wrong directory" >&2
else
find $dir -type f
fi
else
echo "cancelled"
fi
A directory is successfully picked and all its files are printed. If I chose to cancel in zenity, then it prints 'cancelled'.

Bash Script to process data containing input string

I am trying to create a script that will find all the files in a folder that contain, for example, the string 'J34567' and process them. Right now I can process all the files in the folder with my code, however, my script will not just process the contained string it will process all the files in the folder. In other words once I run the script even with the string name ./bashexample 'J37264' it will still process all the files even without that string name. Here is my code below:
#!/bin/bash
directory=$(cd `dirname .` && pwd)
tag=$1
echo find: $tag on $directory
find $directory . -type f -exec grep -sl "$tag" {} \;
for files in $directory/*$tag*
do
for i in *.std
do
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < $i > $i.sum
done
for j in *.txt
do
egrep "device|Device|\(F\)" $i > $i.fail
done
echo $files
done
Kevin, you could try the following:
#!/bin/bash
directory='/home'
tag=$1
for files in $directory/*$tag*
do
if [ -f $files ]
then
#do your stuff
echo $files
fi
done
where directory is your directory name (you could pass it as a command-line argument too) and tag is the search term you are looking for in a filename.
Following script will give you the list of files that contain (inside the file, not in file name) the given pattern.
#!/bin/bash
directory=`pwd`
tag=$1
for file in $(find "$directory" -type f -exec grep -l "$tag" {} \;); do
echo $file
# use $file for further operations
done
What is the relevance of .std, .txt, .sum and .fail files to the files containing given pattern?
Its assumed there are no special characters, spaces, etc. in file names.
If that is the case following should help working around those.
How can I escape white space in a bash loop list?
Capturing output of find . -print0 into a bash array
There are multiple issues in your script.
Following is not required to set the operating directory to current directory.
directory=$(cd `dirname .` && pwd)
find is executed twice for the current directory due to $directory and ..
find $directory . -type f -exec grep -sl "$tag" {} \;
Also, result/output of above find is not used in for loop.
For loop is run for files in the $directory (sub directories not considered) with their file name having the given pattern.
for files in $directory/*$tag*
Following for loop will run for all .txt files in current directory, but will result in only one output file due to use of $i from previous loop.
for j in *.txt
do
egrep "device|Device|\(F\)" $i > $i.fail
done
This is my temporary solution. Please check if it follows your intention.
#!/bin/bash
directory=$(cd `dirname .` && pwd) ## Should this be just directory=$PWD ?
tag=$1
echo "find: $tag on $directory"
find "$directory" . -type f -exec grep -sl "$tag" {} \; ## Shouldn't you add -maxdepth 1 ? Are the files listed here the one that should be processed in the loop below instead?
for file in "$directory"/*"$tag"*; do
if [[ $file == *.std ]]; then
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
fi
if [[ $file == *.txt ]]; then
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
fi
echo "$file"
done
Update 1
#!/bin/bash
directory=$PWD ## Change this to another directory if needed.
tag=$1
echo "find: $tag on $directory"
while IFS= read -rd $'\0' file; do
echo "$file"
case "$file" in
*.std)
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
;;
*.txt)
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
;;
*)
echo "Unexpected match: $file"
;;
esac
done < <(exec find "$directory" -maxdepth 1 -type f -name "*${tag}*" \( -name '*.std' -or -name '*.txt' \) -print0) ## Change or remove the maxdepth option as wanted.
Update 2
#!/bin/bash
directory=$PWD
tag=$1
echo "find: $tag on $directory"
while IFS= read -rd $'\0' file; do
echo "$file"
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
done < <(exec find "$directory" . -maxdepth 1 -type f -name "*${tag}*" -name '*.std' -print0)
while IFS= read -rd $'\0' file; do
echo "$file"
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
done < <(exec find "$directory" -maxdepth 1 -type f -name "*${tag}*" -name '*.txt' -print0)

How to use output from find for if-condition?

I would like that if this command outputs anything
find /var/www/cgi-bin -name touch -cmin 10
then should "ok" be echoed.
Have tried
if [ $(find /var/www/cgi-bin -name touch -cmin 10) ]; then echo "ok";fi
but it never echoes anything.
Put double quotes around $(..):
if [ "$(find /var/www/cgi-bin -name touch -cmin 10)" ]; then echo "ok"; fi
This will interpret the output of find as a single word.
This should work for you
if [ -n "$(find ./var/www/cgi-bin -name touch -cmin 10)" ];then echo ok;fi
Even better you can do it like this:
find /var/www/cgi-bin -name touch -cmin 10 -exec echo "ok" \;
HTH
if find ... | grep . > /dev/null; then
echo found something
fi
If you need the output:
if h=$(find ... | grep . ); then
echo found $h
fi

Resources