📜 ⬆️ ⬇️

We arrange disks by script less than kilobyte

While the flu virus is killing our ranks (I’ve already failed), the country is celebrating (did anyone have a table?) Weird holidays, and Russia is going to attack Poland (aaaa), it's time to arrange our collection of discs %).
So, take in your hands all the power of cat, grep, sed and bash and write a small but very useful script .


Introductory word



You probably have many, many blanks. I use them here to merge all the good that accumulates on the disc: from my favorite Simpsons and listened podcasts to the simply disgusting rap "Reper Syava" just because I feel sorry for the traffic I spent and I want The money had a material embodiment in the form of filled sectors on the dvd-disk.
But it can be very difficult to find something among hundreds of disks, so “catalogers” come to the rescue. For a long time I looked at the specimens of the existing individuals, but in the end I understood - NOT THAT! I need a simple tool: one command to add a disk, another to search. I don’t need a gui, because it is quite possible that such a situation will happen when the hand isn’t at hand (heh), but you need to find the disk: I don’t want to turn on the beech, insert the USB flash drive with the base into the router, go to ssh and find the right disk I give it to my friend, and if the flash is already shared, it's generally excellent.
')
The prelude is finished, now a bit of code .
I will bring here the entire script I typed - it is simple to the point of absurdity, and after that I will explain the important points of its work.

#!/bin/bash
#special for habra-users from [rk] (rapkasta on habrahabr.ru)
dbDir="/home/rap-kasta/Documents/cdvdm-article/db/"

if [ $# -lt 1 ]; then
echo " ..."
exit 1
fi

case "$1" in
"add" )
echo " "
if [ $# -ne 3 ]; then
echo " "
fi

find $2 > "${dbDir}${3}"

;;

"find" )
echo " "
if [ $# -ne 2 ]; then
echo " "
fi
grep -H -i -P "${2}" ${dbDir}*|sed "s|${dbDir}||"
;;

"help" )
echo ". :"
echo "1) : add __ _"
echo "2) (): find _"
;;
* )
echo " $1"
;;
esac


So, go through the points.

How will the database of disks look like



Everything is very simple: the base is a separate directory, which is indicated at the beginning of the script, the files inside the directory are disks whose names correspond to the names of the disks, and the contents of the files is the file tree of the disk.
Thus, we have the ability to delete and “edit” disks with pens, although if you really want to, you can add delete and edit functions directly to the script: make rm or query your favorite text editor as easily as adding files.

Adding Files



We will use the great and mighty program find , which, if you do not specify anything superfluous, will gladly give us all the files inside the directory:

find ___ > __


As we already found out, the file name in the disk database is the name of the disk. Following sound logic, the disk name should not contain anything illegal (slash and - due to subjective circumstances - a straight line ("|")) and should be unique to the disk . If you have a lot of disc titled “German Classic”, then you can collect all these discs in a pile and add a serial number to the name of each number: “German Classic # 67”.

After adding a disk, it folds into a pile / is thrown far, far away, and after a couple of months, it suddenly becomes necessary. The truth is already forgotten in which "in it."

File search



Suppose you, like me, appreciate the work of Groening and suddenly wanted to revise the entire 18th season of his Smeshariki. To do this, we need mplayer, but first we’ll find where we threw these very round monsters, and the find command will help us with this:

find -H -i -P _ _


In order to use regular expressions, we add -P to the find request, and -H will print out the name of the file in which the required substring was found, -i will make the request case-insensitive.
There is a minus: in the output, find will show the full path to the found files, because we need to remove the constantly repeating path to the base , in which the favorite text editor SED will help us:

echo "-"|sed 's// /'


As you can see, we will not use any separate keys and modifiers - sed searches line by line, which is what we need.

Oh yeah, smeshariki ...
[rap-kasta@acerAspire cdvdm-article]$ ./cdvdm.sh find 'Simpsons(.+)18(x|e)(.+)\.(avi|srt)'

Archive #10:/media/cdrom/Simpsons/The Simpsons [18x07] Ice Cream Of Margie With The Light Blue.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x08] The Haw-Hawed Couple.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x09] Kill Gil - Vols. 1 And 2.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x10] The Wife Aquatic.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x11] Revenge Is A Dish Best Served Three Tim.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x12] Little Big Girl.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x13] Springfield Up.avi
Archive #10:/media/cdrom/Simpsons/The Simpsons [18x14] Yokel Chords.avi
Archive #10:/media/cdrom/Simpsons/s18e07.srt
Archive #10:/media/cdrom/Simpsons/s18e08.srt
Archive #10:/media/cdrom/Simpsons/s18e09.srt
Archive #10:/media/cdrom/Simpsons/s18e10.srt
Archive #10:/media/cdrom/Simpsons/s18e11.srt
Archive #10:/media/cdrom/Simpsons/s18e12.srt
Archive #10:/media/cdrom/Simpsons/s18e13.srt
Archive #10:/media/cdrom/Simpsons/s18e14.srt
Archive #9:/media/cdrom/Simpsons/s18e06.srt
Archive #9:/media/cdrom/Simpsons/s18e05.srt
Archive #9:/media/cdrom/Simpsons/s18e04.srt
Archive #9:/media/cdrom/Simpsons/s18e03.srt
Archive #9:/media/cdrom/Simpsons/s18e02.srt
Archive #9:/media/cdrom/Simpsons/s18e01.srt
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x06] Moe 'N' A Lisa.avi
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x05] GI (Annoyed Grunt).avi
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x04] Treehouse Of Horror XVII.avi
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x03] Please Homer, Don't Hammer 'Em.avi
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x02] Jazzy And The Pussycats.avi
Archive #9:/media/cdrom/Simpsons/The Simpsons [18x01] The Mook, the Chef, the Wife, and Her H.avi


Ying good


If you have any ideas for improvement, then I think posting three or four lines of code in the comment will not be a very bad idea :)
I hope I helped you organize your collection of disks. Myself, I also helped a little to distract from the disease and that is good :)

PS Habrapamer ystein in the comments suggested a great idea and implementation (for Windows) of disk dumping, which transfers such functions as search to already existing file managers.

PPS Minus, unsubscribe in habraposhta or comments on the reasons :)

Source: https://habr.com/ru/post/74508/


All Articles