How to check all specific file type files are copied into a directory? - bash

I've tried to compare specific file type (like .txt) and a specific directory by using the diff function but it doesn't turn out right. Can I get some help here?
backup=$(find . -name "*.text" -type f)
backup2=$(find /home/user/Desktop/backupfile -name "*.text" -type f)
diff -rq $backup $backup2

You can try the below, using a for loop comparing the files in both locations (if found) :
for f in ($find . -type f -name "*.text")
do
name=$(basename "$f")
if [ -f /home/user/Desktop/backupfile/"$name" ]
then
diff -rq "$f" /home/user/Desktop/backupfile/"$name"
else
echo match of "$f" not found under /home/user/Desktop/backupfile/
done

Related

Rename file if it is the only one with the extension in directory

This works however I would like to do it only if it is the only .jpg for the given directory, the one below will just rename them all to folder.jpg, overwriting the other files:
find . -type f -name '*.jpg' -execdir mv {} 'folder.jpg' \;
I guess find cannot filter by the number of matches, but you can always exec a shell which does more elaborate checks for you:
find . -type f -name '*.jpg' -execdir sh -c '[ $# = 1 ] && mv "$1" folder.jpg' sh {} +

Find those files with an underscore that have no matching file without underscore in shell?

I have got files like klein_parat-strong-aqua-stop.jpg. Most of them have a counterpart with all underscores replaced with a minus, klein-parat-strong-aqua-stop.jpg for example.
Some of them however do not have that counterpart. I want to find those files in a folder including its subfolders. How would I do that?
THanks!
Try this two commands
for FF in `find . -type f -name "*_*" `;do echo $FF;done > list
for II in `<list`;do I=`basename $II`;F2=`echo $I | tr '_' '-'`;FOUND=`find . -name "$F2"`;[ "$FOUND" == "" ] && { echo $II; };done
The basic idea would be find all files first and then test the existence of the counterpart, as mentioned in the comment:
for i in `find . -name '*_*'`
do
test -f "${i//_/-}" || echo "$i"
done
A one-liner:
for i in `find . -name '*_*'`; do test -f "${i//_/-}" || echo "$i"; done
test -f file: check whether file is a normal file or not.

BASH: If condition uses result from find command to determine which file will be written

I want to list all files in a nested directory, but in that directory has some files which having space in their name. So I wanna write down the paths of which files don't have space in their name and which have in 2 different files.
So far, I just know how to find those having space in their name by this command:
find /<my directory> -type f -name * *
I want something like:
find /<my directory> -type f
if [ name has space]
then > a.txt
else > b.txt
fi
Thank you in advance.
You can put a condition in a brief -exec. This is somewhat more complex than you would hope because -exec cannot directly contain shell builtins.
find "$path" -type f -exec sh -c 'for f; do
case $f in *\ *) dest=a;; *) dest=b;; esac;
echo "$f" >>$dest.txt
done' _ {} +
In other words, pass the found files to the following sh -c ... script. (The underscore is to populate $0 with something inside the subshell.)
If the directory tree isn't too deep, perhaps it would be a lot easier to just run find twice.
find "$path" -type f -name '* *' >a.txt
find "$path" -type f \! -name '* *' >b.txt
Use two separate commands:
find "$path" -type f -name '* *' > a.txt
find "$path" -type f -not -name '* *' > b.txt

Rename files with a suffix with name of its folder

Each folder has a .mkv or .avi file.
E.g:
Sommerferien201308/eins.avi
Sommerferien201309/eins.mkv
Herbst201401/film.avi
Herbst201402/krz.mkv
Renaming to:
Sommerferien201308/Sommerferien201308.avi
Sommerferien201309/Sommerferien201309.mkv
Herbst201401/Herbst201401.avi
Herbst201402/Herbst201402.mkv
How can I rename the filename to the name of its folder?
Of course every foldername is unique. And too much to do it manually for each file/folder.
I Would try with find.
find . -type d -name "" -exec cd "" && mv {} \;
But i dont know how to select the folder name and how to make the .avi or .mkv selection and how to store the selected folder name..
You can use this find command from base folder of your folders that contain *.avi and *.mkv files:
while IFS= read -rd '' f; do
(
IFS=/
arr=($f)
if [[ $f != *"/${arr[len-2]}"* ]]; then
len=${#arr[#]}
ext="${arr[len-1]##*.}"
cd "$(dirname "$f")" && echo mv "${arr[len-1]}" "${arr[len-2]}.$ext"
fi
)
done < <(find . \( -name '*.mkv' -o -name '*.avi' \) -print0)
When you're satisfied with the output remove echo before mv.

bash delete directories based on contents

Currently I have multiple directories
Directory1 Directory2 Directory3 Directory4
each of these directories contain files (the files are somewhat cryptic)
what i wish to do is scan files within the folders to see if certain files are present, if they are then leave that folder alone, if the certain files are not present then just delete the entire directory. here is what i mean:
im searching for the files that have the word .pass. in the filename.
Say Directory 4 has that file that im looking for
Direcotry4:
file1.temp.pass.exmpl
file1.temp.exmpl
file1.tmp
and the rest of the Directories do not have that specific file:
file.temp
file.exmp
file.tmp.other
so i would like to delete Directory1,2 and3 But only keep Directory 4...
So far i have come up with this code
(arr is a array of all the directory names)
for x in ${arr[#]}
do
find $x -type f ! -name "*pass*" -exec rd {} $x\;
done
another way i have thought of doing this is like this:
for x in ${arr[#]}
do
cd $x find . -type f ! -name "*Pass*" | xargs -i rd {} $x/
done
SO far these don't seem to work, and im scared that i might do something wrong and have all my files deleted.....(i have backed up)
is there any way that i can do this? remember i want Directory 4 to be unchanged, everything in it i want to keep
To see if your directory contains a pass file:
if [ "" = "$(find directory -iname '*pass*' -type f | head -n 1)" ]
then
echo notfound
else
echo found
fi
To do that in a loop:
for x in "${arr[#]}"
do
if [ "" = "$(find "$x" -iname '*pass*' -type f | head -n 1)" ]
then
rm -rf "$x"
fi
done
Try this:
# arr is a array of all the directory names
for x in ${arr[#]}
do
ret=$(find "$x" -type f -name "*pass*" -exec echo "0" \;)
# expect zero length $ret value to remove directory
if [ -z "$ret" ]; then
# remove dir
rm -rf "$x"
fi
done

Resources