2

I've got a couple dozen sets of Audacity project folders and .aup project files whose names are very close, i.e. 10-13-14-data and 10-13-14.aup. I'd like to compress all of these files automatically via the command line to individual zips (named for example 10.13.14.zip), instead of going through the whole set and compressing each one (which should have been done initially).

I'm doing this because I'd like to learn more about how to use the CLI and hopefully continue to learn from AskUbuntu. I could simply go through the number of files/folders I have to zip together, but I'm taking the opportunity to be patient and learn something new instead. Thanks!

4
  • I would say make a script that creates a list of lists, the last one(s) containing all files of the same date in your directory, after that running a for loop to combine it with a tar command per sublist. Oct 13, 2014 at 20:35
  • And how would I do that? I don't have much knowledge of command lines to go off of. Oct 13, 2014 at 21:26
  • AHA, I can explain how it works in a python (script)? (after I get some sleep) would that interest you? Also: is the date the identifying string in the names? Are all files in a "flat" single directory or also sub folders? Oct 13, 2014 at 21:31
  • Python sounds good. A further challenge would be to maintain the system; we save a new project in that folder every week (so yeah, they are and will always be in that folder), so if we could have a script automatically zip the files every time a new project was found in that folder, and move it to a separate folder for zips, that would be great. The date is the string which is common between the project files and the project folders, exactly like I stated in the original question. The script should find two matching strings, zip those items into a compressed .zip, and repeat on the next set. Oct 13, 2014 at 22:54

2 Answers 2

2

You could use a for loop in the parent directory of the directories that contain the files, for example

for f in */*; do 
    zip "${f%.*}".zip "${f%.*}.data" "${f%.*}.aup"
    echo mv -v -- "${f%.*}".zip /media/user/folder/$(basename "${f/.*/.zip}")      
done

remove echo after testing and run again to actually move the files (you would need to replace it with sudo in this case).

0

I managed (with a lot of help from my dad), to come up with this.

dates=`ls *.aup | sed 's/\(.*\).aup/\1/'`
for i in $dates; do zip -r $i $i*
mv $i /media/user/folder
done

I don't fully understand this myself, but it's currently working; if someone wants to explain how I just solved my own problem, I'd appreciate it on behalf of those who find this question/answer in future; thanks for trying to help, those who did!

3
  • Turns out there is an error in the code, but only in the move part of it. I'll update when I figure out why it's not working. Oct 14, 2014 at 3:48
  • So the problem in the script was that I failed to put the extension on the end of $i in the mv line. It should read... mv $i.zip /media/user/folder This way the script finds the zips instead of looking for a file with simply the date and no extension. Oct 14, 2014 at 4:18
  • 1
    parsing ls is a really bad idea
    – Zanna
    Jan 29, 2017 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .