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" ) ]]
Related
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
#!/bin/bash
dir=$1
cd $dir
list=$(find . -maxdepth 1 -mindepth 1 -type d -printf '%f,')
IFS=',' read -a array <<< "${list}"
for i in "${array[#]}"
do
longPermission=$(getfacl $i |stat -c %A $i)
longOwner=$(getfacl $i |grep owner)
ownerPermission=${longPermission:1:3}
owner=${longOwner:9}
if (($ownerPermission=="rwx"))
then
permission='FULL_PERMISSION'
fi
if (($ownerPermission=="rw-"))
then
permission='READ/WRITE_PERMISSION'
fi
#if (($ownerPermission=="r--"))
#then
# permission='READ_ONLY_PERMISSION'
#fi
echo ’BUERO\ $owner -user -group -type windows permission $permission’
done
Bash treats the "-" in rw- and rw-- as arithmetic operators but what I wanto to achieve is processing it as string because I want to work with output I get from getfacl $i |grep owner which gives me back a string I wanna echo in
echo ’BUERO\ $owner -user -group -type windows permission $permission’
thanks in advance!
Use if [[ $ownerPermission == "rwx" ]] and if [[ $ownerPermission == "rw-" ]]
The (( expr )) is used for arithemtic expressions, which you don't need here.
I have created a script to zip and move log files from one directory to another directory to free space. This is the script:
#!/bin/bash
logsDirectory="/test//logs/"
email=""
backupDirectory="/test/backup"
pid="/data/test/scripts/backup.pid"
usage=$(df | grep /data/logs | awk '{ print $2 }')
space=450000000
getBackup ()
{
if [[ ! -e $pid ]] then
if [[ $usage -le $space ]]
then
touch $pid
find $backupDirectory -mtime +15 -type f -delete;
for i in $(find $logsDirectory -type f -not -path "*/irws/*")
do
/sbin/fuser $i > /dev/null 2>&1
if [ $? -ne 0 ]
then
gzip $i
mv -v $i.gz $backupDirectory
else
continue
fi
done
[[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
rm -f $pid
fi
fi
}
getBackup
I am getting this error:
gzip: /data/logs/log01.log.gz already has .gz suffix -- unchanged
mv: cannot stat `/data/logs/log01.log.gz': No such file or directory
I got the error every time I ran the script in my DEV and PROD (CentOS servers) environments. To analyse it, I ran the same script in a VM (Ubuntu) in my laptop, and I don't get the error there.
My questions:
How can I prevent this error?
What I have done wrong in the script?
Your script contains a number of common clumsy or inefficient antipatterns. Here is a refactoring. The only real change is skipping any *.gz files.
#!/bin/bash
logsDirectory="/test//logs/"
email=""
backupDirectory="/test/backup"
pid="/data/test/scripts/backup.pid"
# Avoid useless use of grep -- awk knows how to match a regex
# Better still run df /data/logs
usage=$(df /data/logs/ | awk '{ print $2 }')
space=450000000
getBackup ()
{
# Quote variables
if [[ ! -e "$pid" ]]; then
if [[ "$usage" -le "$space" ]]; then
touch "$pid"
find "$backupDirectory" -mtime +15 -type f -delete;
# Exclude *.gz files
# This is still not robust against file names with spaces or wildcards in their names
for i in $(find "$logsDirectory" -type f -not -path "*/irws/*" -not -name '*.gz')
do
# Avoid useless use of $?
if /sbin/fuser "$i" > /dev/null 2>&1
then
gzip "$i"
mv -v "$i.gz" "$backupDirectory"
# no need for do-nothing else
fi
done
[[ ! -z "$email" ]] &&
echo "Backup is ready" | mas"Backup" "$email"
rm -f "$pid"
fi
fi
}
getBackup
With a slightly more intrusive refactoring, the proper fix to the find loop would perhaps look something like
find "$logsDirectory" -type f \
-not -path "*/irws/*" -not -name '*.gz' \
-exec sh -c '
for i; do
if /sbin/fuser "$i" > /dev/null 2>&1
then
gzip "$i"
mv -v "$i.gz" "$backupDirectory"
fi
done' _ {} +
where the secret sauce is to have find ... -exec + pass in the arguments to the sh -c script in a way which does not involve exposing the arguments to the current shell at all.
What I have done wrong in the script?
Your script tries to zip every file but the gzip command is rejecting files already zipped
How can I prevent this error?
Have the script check whether the file is zipped or not and only gzip if it corresponds (1). Alternatively, you could force re-compression even if it is already compressed (2).
Going with option number 1):
getBackup ()
{
if [[ ! -e $pid ]] then
if [[ $usage -le $space ]]
then
touch $pid
find $backupDirectory -mtime +15 -type f -delete;
for i in $(find $logsDirectory -type f -not -path "*/irws/*")
do
/sbin/fuser $i > /dev/null 2>&1
if [ $? -ne 0 ]
then
if [[ $i =~ \.gz$ ]]
# File is already zipped
mv -v $i $backupDirectory
else
gzip $i
mv -v $i.gz $backupDirectory
fi
else
continue
fi
done
[[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
rm -f $pid
fi
fi
}
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'.
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)