View Full Version : Script help....again :)
Been searching for an answer on this but it's all a bit over my head at the moment.
I have one directory with multiple directories inside it. I need to run the command "dpkg -b <directory name>" to create a deb package of that directory. Thing is, I want to be able to run it automatically for whatever directories are present. The command can also only handle one directory at a time.
So, I need a script that will collect the directory names and then pass them on to that command one at a time until they are all done.
:)
Ta-da:
find . -type d -exec dpkg -b {} \;
'Find everything, in the current directory (.), of type directory (-type d) and execute (-exec) the command dpkg -b'. The bits at the end are find's way of substituting parameters, so the directory will go where the {} is, and the \; signifies the end of the command.
Cheers Mark, that seems to have done the trick although it's not quite perfect :)
Can you make it so it only does the directories in the main directory? What it seems to be doing is running the command on as many sub-directories as it can find. Whilst this doesn't actually cause a problem as it can't find the control file to create the deb, it just gives a messy output.
But thanks again as it does give the final files I need :)
find . -maxdepth 1 -type d -exec dpkg -b {} \;
You might need -maxdepth 2. I rarely get that one right.
depth 1 worked a treat :D
To cover a geek angle it's worth emphasising the importance of -exec there.
Linux allows you to have filenames with spaces and other special characters in them, e.g.
test file here
to view or access the file those spaces need to be 'escaped' (preceeded by a \ symbol), e.g.
$ cat test\ file\ here
otherwise Linux would be looking for the three files "test", "file" and "here".
It's quite a common trick to pipe responses into xargs, e.g.
find . -name "*here"| xargs cat
With normal file names that have no special characters that would work fine. But in this case we get:
# find . -name "*here" | xargs cat
cat: ./test: No such file or directory
cat: file: No such file or directory
cat: here: No such file or directory
That's fine for "cat" processes, if it fails no big deal but if you using "rm -rf" or similar it can be disastrous as you might end up deleting the wrong directory (seen it done, sadly).
-exec gets away with all that fuss, it makes sure whatever commands are carried out actually take place against the correct file / directory, leaving no room for mistakes (unless your entire concept is bad, but there is no helping that!)
# find . -name "*here" -exec cat {} \;
Boat Drinks
And the other Geek angle is why *here was in quotes. Try it without (as all too often happens), and one of three things will happen:
If there are no files that match the pattern (i.e. end in 'here') in the current directory, it'll 'just work'. You then assume it'll always work, but...
If there is exactly one file that matches, e.g. 'test file here', it'll only match files with that exact name. Probably not what you intended.
If there is more than one file that matches, the command will fail. Definitely not what you wanted. :)
Cheers guys :)
It's always nice to get a bit more info about these things just so I know why something is happening rather than just blindly copying it.
Just so you know what it's for, my iPhone repo is now totally automatic :D
I drop apps or themes in to folders and then run a script based on what repo it should go in to. It now automatically creates the deb based on all folders, creates a packages file, creates a zip of the packages file and uploads all of that to the webspace all with one click of a button :)
vBulletin® v3.7.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.