I am writing a script that renames *.MP4 files on an inserted SD card and then rsyncs them.
The directory with the *.MP4 files does not always have the same name:
eg: it could be /DCIM/123_PANA/ or /DCIM/141_PANA/ etc
So I'm trying to write a script that will see what folders are in the /DCIM path, and rename all the *.MP4 files, (there is also a MISC folder in this path which I suspect is causing the issue)
I am using a couple of variables to rename the files also
What I have is:
for f in /media/pi/LUMIX/DCIM/*; do
if [ -d "$f" ]; then
echo $f
for file in $(find $f -name 'P*.MP4')
do
echo $file ">" $(dirname "${file}")/$(date +"%d")$cardname$(basename $file)
mv $file $(dirname "${file}")/$(date +"%d")$cardname$(basename $file)
done
fi
done
But what seems to happen is I end up with a single file with the prefix only (say 08_nb1_) in the _PANA folder, all the others have been deleted. Obviously this is not my desired result!
UPDATE:
$cardname is of the format _nb2_
When I do as asked replace mv with echo here is the output:
/media/pi/LUMIX/DCIM/141_PANA
mv /media/pi/LUMIX/DCIM/141_PANA/P1410192.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410192.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410193.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410193.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410194.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410194.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410195.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410195.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410196.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410196.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410197.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410197.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410198.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410198.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410199.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410199.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410200.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410200.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410201.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410201.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410202.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410202.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410203.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410203.MP4
mv /media/pi/LUMIX/DCIM/141_PANA/P1410204.MP4 /media/pi/LUMIX/DCIM/141_PANA/09_nb2_P1410204.MP4
/media/pi/LUMIX/DCIM/MISC
OK I fixed it by filtering the additional directory names and limiting to only the ones with *_PANA - which solved the issue. I also added a the rsync part and demounted the SD card (if required using Zenity)
A text file placed on the SD card identifies it as a unique card giving each file a unique name when rsyncing it to the backup folder. Renaming on the SD card means
that it can still be used an written to if not full, but we then know which files have been backed up.
Very useful in the field when filming with multiple cards, crews. All running on a Rpi4
for f in /media/pi/LUMIX/DCIM/*_PANA/; do
if [ -d "$f" ]; then
echo "$f"
for file in $(find $f -wholename '*_PANA/P*.MP4')
do
mv "$file" $(dirname "${file}")/$(date +"%d")"$cardname"$(basename "${file}")
done
rsync --stats -u --progress "$f"/*.MP4 /media/pi/VDRIVE/ | tee /home/pi/Documents/ytu/rsync.txt | zenity --icon-name="dialog-warning" \
--width=300 --progress --pulsate --auto-close --auto-kill \
--title="Copying $sdn"
zenity --question --text="Unmount Card?"
if [ $? = 0 ]; then
umount /media/pi/LUMIX
else
exit
fi
fi
done
Related
I have a directory with several folders inside. Inside the folders I have a worksheet with the same name in all folders. I need to run a script that randomly changes the name of the worksheets so that I can throw them all in the same folder. For example: worksheet1
worksheet2
worksheet3.
today they are all called spreadsheet.csv
I have a sketch in linux.
need help please.
NN=0;
for arq in $(ls -1 *.csv);do
let NN++;
rename -n 's/'${arq}'/spreadsheet'${NN}'.csv/' ${arq};
done
(this search all files csv, but dont is recursive. Work only in one directory)
Use globstar:
n=0
shopt -s globstar nullglob || exit
for f in **/*.csv; do
until dst=spreadsheet$(( ++n )).csv; [[ ! -e ${dst} ]]; do
continue
done
mv -i -- "${f}" "${dst}"
done
Sample files:
$ find . -name "*.csv"
./spreadsheet.csv
./sub1/spreadsheet.csv
./sub1/sub2/spreadsheet.csv
One idea:
n=0
while read -r oldname
do
((++n))
newname="${oldname##*/}"
newname="${newname//.csv/-$n.csv}"
echo mv "$oldname" "$newname"
done < <(find . -name spreadsheet.csv)
This generates:
mv ./spreadsheet.csv spreadsheet-1.csv
mv ./sub1/spreadsheet.csv spreadsheet-2.csv
mv ./sub1/sub2/spreadsheet.csv spreadsheet-3.csv
Once OP is satisifed with the output, remove the echo and run the script again.
After removing the echo and running again:
$ find . -name "*.csv"
./spreadsheet-1.csv
./spreadsheet-2.csv
./spreadsheet-3.csv
I have these files:
100-1.jpg
100-2.jpg
200-1.jpg
200-2.jpg
I want these to be transferred to specific folder based on filename
100/100-1.jpg
100/100-2.jpg
200/200-1.jpg
200/200-2.jpg
How do I do this?
What I have tried so far
cd ~/images
for f in *.jpg
do
mv -v "$f" ~/images/${f}/${f%}.jpg
done
how do I know I cut the string before the dash e.g 200-1 to 200 and store in a variable?
so I can do it like this
cd ~/images
for f in *.jpg
name="$f without the .jpg"
do
mv -v "$f" ~/images/${f}/${f%}.jpg
done
#!/bin/bash
cd ~/images
for f in *.jpg
do
mkdir -p ${f%-*}
echo ${f%-*}
mv "$f" ~/images/${f%-*}/${f%}
done
if [ -f *.mp4 ]; then
for file in *.mp4; do
ffmpeg2theora -v 8 --nometadata "$file"
rm -f "$file"
done
fi
if [ -f *.avi ]; then
for file in *.avi; do
ffmpeg2theora -v 8 --nometadata "$file"
rm -f "$file"
done
fi
I can't figure out why if [ -f *.mp4 ]; then is not detecting mp4 files, i use this method it to find .avi, .epub, .chm and some other extensions and it works perfectly. I wonder me if it is because of the 4. i also tried by doing if [ -f *."mp4" ]; then but it didn't work.
I find weird that for file in *.mp4; do detects .mp4 files and if [ -f *.mp4 ] not!
Use the bash option nullglob to allow globs to expand to zero elements. This way, the loop will simply not run if there are no matching files:
shopt -s nullglob
for file in *.mp4; do
ffmpeg2theora -v 8 --nometadata "$file" && rm -f "$file" || \
echo Problem transcoding "$file"
done
As for [ -f *.mp4 ], it only works correctly if there is exactly one matching file, because then it expands to [ -f myfile.mp4 ].
If there are no matching files, it may still work (without nullglob) because it ends up checking for a file with an asterisk in the name, which hopefully doesn't exist.
If there are multiple files, it fails, because [ -f file1.mp4 file2.mp4 file3.mp4 ] is not valid test syntax.
if [ -f *.mp4 ]; then
There is no expansion inside the test. The test is looking for a file named *.mp4 which doesn't exist. What you should do it:
for i in *.mp4; do
<do something>
done
Here expansion will provide all files with .mp4 extensions in the present working directory. You can also you find /path/to/dir -type f -name "*.mp4" to get your filenames as well. (note: this expects .mp4 files to be present in the directory. If not, i=*.mp4. You are safer doing:
find /path -type f -name "*.mp4" | while read i; do
<do whatever>
done
I have a huge directory structure of movie files. For analysis of that structure I want to copy the entire directory structure, i.e. folders and files however I don't want to copy all the movie files while I want to keep there file names. Ideally I get zero-byte files with the original movie file name.
I tried to and then rsync to my remote machine which didn't fetch the link files.
Any ideas how to do that w/o writing scripts?
You can use find:
find src/ -type d -exec mkdir -p dest/{} \; \
-o -type f -exec touch dest/{} \;
Find directory (-d) under (src/) and create (mkdir -p) them under dest/ or (-o) find files (-f) and touch them under dest/.
This will result in:
dest/src/<file-structre>
You can user mv creatively to resolve this issue.
Other (partial) solution can be achieved with rsync:
rsync -a --filter="-! */" sorce_dir/ target_dir/
The trick here is the --filter=RULE option that excludes (-) everything that is not (!) a directory (*/)
On ubuntu you can try:
cp -r --attributes-only <source_dir> <target_dir>
It doesn't copy file data.
From manpage of cp
--attributes-only
don't copy the file data, just the attributes
Note: I'm not sure this option available for other distributions, if anybody can confirm please update the answer.
I needed an alternative to this to sync only the file structure:
rsync --recursive --times --delete --omit-dir-times --itemize-changes "$src_path/" "$dst_path"
This is how I realized it:
# sync source to destination
while IFS= read -r -d '' src_file; do
dst_file="$dst_path${src_file/$src_path/}"
# new files
if [[ ! -e "$dst_file" ]]; then
if [[ -d "$src_file" ]]; then
mkdir -p "$dst_file"
elif [[ -f $src_file ]]; then
touch -r "$src_file" "$dst_file"
else
echo "Error: $src_file is not a dir or file"
fi
echo -n "+ "
ls -ld "$src_file"
# modification time changed (files only)
elif [[ -f $dst_file ]] && [[ $(date -r "$src_file") != $(date -r "$dst_file") ]]; then
touch -r "$src_file" "$dst_file"
echo -n "+ "
ls -ld "$src_file"
fi
done < <(find "$src_path" -print0)
# delete files in destination if they disappeared in source
while IFS= read -r -d '' dst_file; do
src_file="$src_path${dst_file/$dst_path/}"
# file disappeard on source
if [[ ! -e "$src_file" ]]; then
delinfo=$(ls -ld "$dst_file")
if [[ -d "$dst_file" ]] && rmdir "$dst_file" 2>/dev/null; then
echo -n "- $delinfo"
elif [[ -f $dst_file ]] && rm "$dst_file"; then
echo -n "- $delinfo"
fi
fi
done < <(find "$dst_path" -print0)
As you can see I use echo and ls to display changes.
ls > listOfMovie.txt; You will have the list of your films in a .txt file
.For multiple directories see the man page.
I have the following directory structure:
+-archive
+-a
+-data.txt
+-b
+-data.txt
+-incoming
+-a
+-data.txt
+-c
+-data.txt
How do I do the equivalent of mv incoming/* archive/ but have the contents of the files in incoming appended to those in archive rather than overwrite them?
# move to incoming/ so that we don't
# need to strip a path prefix
cd incoming
# create directories that are missing in archive
for d in `find . -type d`; do
if [ ! -d "../archive/$d" ]; then
mkdir -p "../archive/$d"
fi
done
# concatenate all files to already existing
# ones (or automatically create them)
for f in `find . -type f`; do
cat "$f" >> "../archive/$f"
done
This should find any file in incoming and concatenate it to an existing file in archive.
The important part is to be inside incoming, because else we'd had to strip the path prefix (which is possible, but in the above case unnecessary). In the above case, a value of $f typically looks like ./a/data.txt, and hence the redirection goes to ../archive/./a/data.txt.
run it on the current directory.
find ./incoming -type f | while read -r FILE
do
dest=${FILE/incoming/archive}
cat "$FILE" >> "$dest"
done
the one in incoming/c would not be appended though
Here's a version with proper quoting:
#!/bin/sh
if [ -z "$1" ]; then
# acting as parent script
find incoming -type f -exec "$0" {} \;
else
# acting as child script
for in_file; do
if [ -f "$in_file" ]; then
destfile="${in_file/incoming/archive}"
test -d "$(dirname "$destfile")" || mkdir -p "$_"
cat "$in_file" >> "$destfile" &&
rm -f "$in_file"
fi
done
fi