Page 2 of 2

Re: Problem copying a folder/subfolders to another drive

Posted: Sat Jan 04, 2025 9:03 am
by Governor
fredx181 wrote: Fri Jan 03, 2025 4:34 pm
d-pupp wrote: Fri Jan 03, 2025 4:14 pm

@Governor I agree it is fun leaning and writing simple scripts. I started with setup scripts. After a new frugal install I would just run a script or two and most of my personalization was done.

BTW there is a second method for testing if a command worked or not. The exit code. Most command have an exit code of 0 if they worked ok. Check the man page to verify. It is stored in the variable $? and is overwrote by the next command that runs. You can check it with echo $?

But in case of find (edit: this case with the pipes) it seems to return always value 0
Check this and it still echoes "Script completed" (but also gives "find: '/BLAHBLA': No such file or directory", as it cannot find the directory)

Code: Select all

DIR=/BLAHBLA
find "$DIR" | sort | uniq -i --all-repeated=separate && echo "Script completed"

Code: Select all

find: '/BLAHBLA': No such file or directory
Script completed

EDIT: but without the pipes to sort uniq etc... it gives error code 1

Code: Select all

DIR=/BLAHBLA
find "$DIR" && echo "Script completed"
find: ‘/BLAHBLA’: No such file or directory
echo $?
1

EDIT2: So need to use PIPESTATUS :

Code: Select all

DIR=/BLAHBLA
find "$DIR" | sort | uniq -i --all-repeated=separate
[ $PIPESTATUS -eq 0 ] && echo "Script completed" || echo "There was an error" 

output:

Code: Select all

find: '/BLAHBLA': No such file or directory
There was an error

But that may only give exit status 1 if the path provided to find doesn't exist (as in this case), not sure though.

I imagine it is the same in Puppy as in msdos; the more pipes in a script, the slower the script is executed, because the OS has to create a temporary file for each pipe.
In an ideal world, exit codes might be checked for each command before assuming it is ok to send the output through a pipe. I had to do that a few of times in msdos, although it. was normally sufficient to rely on the last exit code... Is it one exit code at a time in Puppy, or can there be more than one simultaneously?


Re: Problem copying a folder/subfolders to another drive

Posted: Sat Jan 04, 2025 9:39 am
by Governor
some1 wrote: Sat Jan 04, 2025 2:28 am
Governor wrote: Fri Jan 03, 2025 7:11 pm

.mp4
.mp4
Script completed

I could not see where the two .mp4 are coming from at the end. How do I locate the cause of that?

You probably have a newline in a downloaded filename.
Try this:

Code: Select all

DIR=/mnt/nvme0n1p5/Downloads                      #i.e. your upper-path
find "$DIR" -name *.mp4|grep -n -B2 -E '^.mp4'
exit

Alternatively list all mp4s for eyeballing:

Code: Select all

DIR=/mnt/nvme0n1p5/Downloads                      #i.e. your upper-path
find "$DIR" -name *.mp4
exit

the culprits will probably show up with a questionmark .i.e ? where the newline (cruft ) is
If there is an embedded newline in the path/filename -the line breaks going through the pipe -
and sort will see several distinct lines consisting of the parts.
----------
MochiMoppels/Freds code will probably work when we speak about {a-z] and [A-Z] but...

Success! I found the two culprit .mp4 filenames in two different directories. Both had what looks like the [Enter] key embedded in the filename. I could not tell if it was a carriage return, a linefeed or both. Would it definitely be a linefeed?
I either copied the filename from the address bar in my browser, or from the webpage the .mp4 was embedded on. I could see in rox, the filename took up two lines and the linefeed came just before the dot so the .mp4 was alone on the second line.. I certainly know what to guard against now. Thanks!

I got this now:

Code: Select all

DIR=/mnt/nvme0n1p5/Downloads                      #i.e. your upper-path
echo "Finds .mp4 filenames in "$DIR" containing more than one line"
find "$DIR" -name *.mp4|grep -n -B2 -E '^.mp4'
echo "Script completed"
exit

Re: Problem copying a folder/subfolders to another drive

Posted: Sat Jan 04, 2025 10:09 am
by Governor
MochiMoppel wrote: Sat Jan 04, 2025 1:54 am
Governor wrote: Fri Jan 03, 2025 7:11 pm
Trapster wrote: Fri Jan 03, 2025 3:52 pm

Remember to quote your echo's

You could also send the results to a file. then have the file open.

Code: Select all

find "$DIR" | sort | uniq -i --all-repeated=separate > /root/find_results.txt
geany /root/find_results.txt

This was elegant!

Maybe even more elegant, because you wouldn't need to create (and later delete) a temporary file:

Code: Select all

find "$DIR" | sort | uniq -i --all-repeated=separate | leafpad

The unelegant part of both solutions is that you will stare at a blank Geany/Leafpad document when no duplicates were found. It needs more code to prevent this.

Yes, I agree. results should only be sent to the program if duplicates are found.

I could not see where the two .mp4 are coming from at the end. How do I locate the cause of that?

I suspect that you have 2 files with linefeeds in their names:
mullvadbrowser
.mp4

and
MullvadBrowser
.mp4

This would cause the output you posted.
You could use the find command to hunt them down:

Code: Select all

find /mnt/nvme0n1p5/Downloads/ -iname 'mullvadbrowser*mp4'

If there are indeed linefeeds in the names they may be represented by a questionmark in output.
Please note that even without duplicates of such funny names you would not be able to copy such files to a Windows filesystem!

I was able to locate the two files with linefeeds in the name in two different folders, thanks to @some1 .

Code: Select all

DIR=/mnt/nvme0n1p5/Downloads                      #i.e. your upper-path
echo "Finds .mp4 filenames in "$DIR" containing more than one line"
find "$DIR" -name *.mp4|grep -n -B2 -E '^.mp4'
echo "Script completed"
exit

viewtopic.php?p=139366#p139366


Re: Problem copying a folder/subfolders to another drive

Posted: Sat Jan 04, 2025 12:58 pm
by Governor
fredx181 wrote: Thu Jan 02, 2025 4:35 pm

@Governor

It works in the terminal from any location! But not from rox, where set run action is grayed out.

Not run action, you need to right-click on *some* directory (as it's for scanning a directory), e.g. /usr (as I did , see pic) then "Customise Menu" , further see .gif picture below:
(I named the script "fndups", probably different from yours)
Screenshot(2).gif

New script: (runs now in urxvt terminal (otherwise no output will show when running from rox) and gives info when no dups found)

Code: Select all

#!/bin/bash

export DIR="$1"

fnddups () {
if [ ! -d "$DIR" ]; then
echo "Please provide a directory as first argument"
exit
fi

find "$DIR" | sort | uniq -i --all-repeated=separate | tee /tmp/fnd_dups
[ -z $(cat /tmp/fnd_dups) ] && echo "No duplicates found"  # if /tmp/fnd_dups is empty
rm -f /tmp/fnd_dups       # comment this out if you want to see the output in that text file
bash
}; export -f fnddups

urxvt -T "Scan for Duplicate names" -si -sb -fg white -bg SkyBlue4 -geometry 80x20 -e bash -c fnddups

EDIT: you can also comment out rm -f /tmp/fnd_dups so becomes # rm -f /tmp/fnd_dups (disabled then) for to see the output in that text file.

Ok, I now have this, but there is something wrong.

Code: Select all

#!/bin/bash

export DIR="$1"

fnddups () {
if [ ! -d "$DIR" ]; then
echo "Please provide a directory as first argument"
exit
fi
echo "Please wait...  duplicates found will show on screen."
find "$DIR" | sort | uniq -i --all-repeated=separate | tee /tmp/fnd_dups.txt
[ -z $(cat /tmp/fnd_dups.txt) ] && echo "No duplicates found in "$DIR"" && echo "" # if /tmp/fnd_dups.txt is empty
#rm -f /tmp/fnd_dups.txt       # comment this out if you want to see the output in that text file
bash
}; export -f fnddups

urxvt -T "Scan for Duplicate names" -si -sb -fg white -bg SkyBlue4 -geometry 160x90 -e bash -c fnddups
unset DIR
echo ""
Script error finding dup filenames.jpg
Script error finding dup filenames.jpg (40.37 KiB) Viewed 259 times

In this new console, I cannot copy/paste anything, yet I can run other commands at the prompt. Odd, isn't it?
About the /root/my-applications/bin folder: It is a good idea, but it does not seem completely "safe" to put scripts there. If I write a script and place it there, won't it disappear if the OS crashes or I forget to save when exiting? Or, if the OS gets messed up, and I have no backup that includes the script.
Additionally, if I create files there in bookworm, won't they be unavailable in fossapup?
I would rather designate a script folder on my internal nvme drive that is not directly included in bookworm OS, so I can access it from fossapup.
How would I do that?
Is it normal to clear variables and delete temp files when no longer needed?
In the code above, unset DIR does not seem to clear the variable?
Is there a way to tell the difference between variables are reserved for the OS and temporary variables set in scripts?
It looks to me like the variables set by a script are limited to the current terminal session.
Suppose you want a global temp variable that can be referenced by other scripts in other consoles?
Thanks!


Re: Problem copying a folder/subfolders to another drive

Posted: Sat Jan 04, 2025 1:27 pm
by geo_c
Governor wrote: Sat Jan 04, 2025 12:58 pm

I would rather designate a script folder on my internal nvme drive that is not directly included in bookworm OS, so I can access it from fossapup.
How would I do that?

Create your folder full of scripts on the drive where your various Os's are located. Then create a symlink to that folder in each of your OS's. You can do it in a terminal with a command:

Code: Select all

ln -s /mnt/home/govscripts /root

Now you can access that folder from /root/govscripts. If you change something from there, it will be changed for all the other OS's.

I actually have my /root/my-applications/bin folder symlinked from /mnt/home/geoscripts. But the way I do it, it shows up as /root/my-applications/bin by using this command instead:

Code: Select all

ln -s /mnt/home/geoscripts /root/my-applications/bin

In that command, it's necessary to move all the scripts from /root/my-applications/bin FIRST, then delete the empty ../bin folder, so that the ln command can create a symlink with that name.

You can also use xfe for creating symlinks, it's really good for creating symbolic links with a right click. Rox can also do it.

addendum: I think you'll find a majority of experienced linux/pup users here have more than just an OS installed, it's more like a directory of several OS's sharing a large amount of linked data/scripts/applications. My system is so integrated across OS's that I created a library of rsync scripts to keep this duplicated directory structure in sync across multiple devices at different locations. The symlink capability is one of the huge benefits of linux/unix. It's how @mikewalsh gets portable apps running from outside the OS,