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'.
Related
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 have to rename a complete folder tree ('target') recursively so that it has
the same files and folders name as on the file server ('server').
Example. On the 'target':
./target
./target/FILE
./target/dir2
./target/dir2/File2
./target/DIR1
./target/DIR1/file1
On the 'server':
./target
./target/file
./target/DIR2
./target/DIR2/File2
./target/dir1
./target/dir1/File1
I'm quite sure that if the filenames are the same (comparing in lower case)
the files are the same (maybe I can add a checksum comparing).
Final result should be that 'target' has the same filenames as the 'server'.
I've tried with bash (should be the best solution)... but bash hate me! Any clue? TY!
first, create 2 files, source.txt and target.txt containing the directories as printed in your question
You can achieve this for instance like this:
(cd path/to/the/server;find . -type d) > target.txt
(cd path/to/the/source;find . -type d) > source.txt
Then run this shell:
while read target_dir
do
fixed_name=$(grep -i ^$target_dir\$ source.txt)
if [ ! -z "$fixed_name" ] ; then
(cd path/to/the/server;mv $target_dir $fixed_name)
fi
done < target.txt
Basically, for each line of target.txt file, it looks the correct-case counterpart in the source.txt file. If found, then issues the rename command, located in the path/to/the/server directory.
Thanks Jean: u guide me to the solution! It's a little bit more complex:
#!/bin/bash
# 160912
# match filenames char-cases on target path as in server path
# usage : $0 targetDir serverDir
TARGET_PATH=$1
MAKE_EQUAL()
{
while read server_name
do
new_target_name=$(grep -i ^$server_name\$ targetList.txt)
if [ ! -z "$new_target_name" ] && [ "$server_name" != "$new_target_name" ] ; then
echo " $new_target_name --> $server_name"
(cd $TARGET_PATH;mv $new_target_name $server_name)
fi
done < serverList.txt
}
if [ ! -d "$1" -o ! -d "$2" ] ; then
echo "Paths not found!"
exit 0
fi
# Change directories changing depth level until is the last directory level
DIRLEVEL=1
LASTLEVEL=0
(cd $2;find . -maxdepth $DIRLEVEL -type d) > serverList.txt
while [ "$LASTLEVEL" == "0" ]
do
echo "Match directory level $DIRLEVEL:"
(cd $1;find . -maxdepth $DIRLEVEL -type d) > targetList.txt
MAKE_EQUAL
DIRLEVEL=`expr $DIRLEVEL + 1`
(cd $2;find . -maxdepth $DIRLEVEL -type d) > nextServerList.txt
diff nextServerList.txt serverList.txt > /dev/null
if [ $? -eq 0 ] ; then
LASTLEVEL=1
else
mv nextServerList.txt serverList.txt
fi
done
echo "Match files:"
(cd $1;find . -type f) > targetList.txt
(cd $2;find . -type f) > serverList.txt
MAKE_EQUAL
echo "Done!"
#!/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 move all my files older than 1000 days, which are distributed over various subfolders, from /home/user/documents into /home/user/archive. The command I tried was
find /home/user/documents -type f -mtime +1000 -exec rsync -a --progress --remove-source-files {} /home/user/archive \;
The problem is, that (understandably) all files end up being moved into the single folder /home/user/archive. However, what I want is to re-construct the file tree below /home/user/documents inside /home/user/archive. I figure this should be possible by simply replacing a string with another somehow, but how? What is the command that serves this purpose?
Thank you!
I would take this route instead of rsync:
Change directories so we can deal with relative path names instead of absolute ones:
cd /home/user/documents
Run your find command and feed the output to cpio, requesting it to make hard-links (-l) to the files, creating the leading directories (-d) and preserve attributes (-m). The -print0 and -0 options use nulls as record terminators to correctly handle file names with whitespace in them. The -l option to cpio uses links instead of actually copying the files, so very little additional space is used (just what is needed for the new directories).
find . -type f -mtime +1000 -print0 | cpio -dumpl0 /home/user/archives
Re-run your find command and feed the output to xargs rm to remove the originals:
find . -type f -mtime +1000 -print0 | xargs -0 rm
Here's a script too.
#!/bin/bash
[ -n "$BASH_VERSION" ] && [[ BASH_VERSINFO -ge 4 ]] || {
echo "You need Bash version 4.0 to run this script."
exit 1
}
# SOURCE=/home/user/documents/
# DEST=/home/user/archive/
SOURCE=$1
DEST=$2
declare -i DAYSOLD=10
declare -a DIRS=()
declare -A DIRS_HASH=()
declare -a FILES=()
declare -i E=0
# Check directories.
[[ -n $SOURCE && -d $SOURCE && -n $DEST && -d $DEST ]] || {
echo "Source or destination directory may be invalid."
exit 1
}
# Format source and dest variables properly:
SOURCE=${SOURCE%/}
DEST=${DEST%/}
SOURCE_LENGTH=${#SOURCE}
# Copy directories first.
echo "Creating directories."
while read -r FILE; do
DIR=${FILE%/*}
if [[ -z ${DIRS_HASH[$DIR]} ]]; then
PARTIAL=${DIR:SOURCE_LENGTH}
if [[ -n $PARTIAL ]]; then
TARGET=${DEST}${PARTIAL}
echo "'$TARGET'"
mkdir -p "$TARGET" || (( E += $? ))
chmod --reference="$DIR" "$TARGET" || (( E += $? ))
chown --reference="$DIR" "$TARGET" || (( E += $? ))
touch --reference="$DIR" "$TARGET" || (( E += $? ))
DIRS+=("$DIR")
fi
DIRS_HASH[$DIR]=.
fi
done < <(exec find "$SOURCE" -mindepth 1 -type f -mtime +"$DAYSOLD")
# Copy files.
echo "Copying files."
while read -r FILE; do
PARTIAL=${FILE:SOURCE_LENGTH}
cp -av "$FILE" "${DEST}${PARTIAL}" || (( E += $? ))
FILES+=("$FILE")
done < <(exec find "$SOURCE" -mindepth 1 -type f -mtime +"$DAYSOLD")
# Remove old files.
if [[ E -eq 0 ]]; then
echo "Removing old files."
rm -fr "${DIRS[#]}" "${FILES[#]}"
else
echo "An error occurred during copy. Not removing old files."
exit 1
fi
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)