This article is launched on the wechat public account “AndroidTraveler”.

background

Recently, I have to reinstall macOS, so I need to copy the data for backup.

The backup process was driving me crazy. It was super slow and the waiting time was excruciating.

Analysis of the

The main reason is that too many files have a great impact on the speed of data migration.

As you know, when we create a HelloWorld project using AndroidStudio, the system automatically generates a lot of files for us.

I built one here, and I looked at the number of files (including directories), and there were 85 files, which was the simplest project.

Assuming that a typical project averages 1000 files, with 100 projects the number of files reaches the 100,000 level. (PS: I came across a project with over 50,000 files)

If you copy it directly, it takes a long time, especially on a mechanical hard drive.

So can we compress it?

If you compress such a large number of files into one file at a time, it will take a lot of time.

Therefore, we want to compress each project separately, so that the time is relatively less?

But right clicking directly on a MAC doesn’t have the bulk compression option.

Choosing more than one system will give you the impression that you are trying to compress multiple directories into a single file.

As we know, compressed files can be used on the command line.

So we can use scripts to achieve batch compression files.

In actual combat

Now let’s complete the batch compression script step by step.

Step 1: Create and run the script

Let’s assume the script name is batch_zip.sh.

Run the following command on the terminal:

touch batch_zip.sh; chmod u+x batch_zip.shCopy the code

The first command is to create the batch_zip.sh file, and the second command is to add executable permissions to the file, since our script will eventually need to be run.

If you copy the script directly from the link at the end of this article, you can download it locally only by executing the second command.

Step 2: Be familiar with compression commands

First we need to understand the compression command. The basic compression commands are as follows:

Zip compressed file. Zip compressed fileCopy the code

Since we have recursive compression requirements, we need to add the -r option.

In addition, the compression process shows the compression details by default, such as which files are compressed.

You can add the -q option if you don’t want to know the exact compression process.

We combine these two options and the final compression command is:

Zip -rq Indicates the compressed file. Zip indicates the compressed fileCopy the code

You think this is the end of it?

If the file name before compression is named with Spaces, you can use the command above, which will cause unexpected results, so to deal with this situation, we need to modify the file name by enclosing it in quotation marks (single or double quotation marks can be used).

zip -rq 'Compressed file.zip' 'File before compression'
Copy the code

Step 3: List all files in the current directory

We know that listing all the files in the current directory is just a matter of using the ls command.

So we save the result set returned by this command in a variable, and then we print it to see what it looks like.

We modify our script file to look like this:

FILES=`ls`
echo $FILES
Copy the code

Note that this is not a single quotation mark; the symbol position is in the upper left corner of the keyboard.

Then we run the script:

./batch_zip.sh
Copy the code

You can see the files listed by Luo.

Step 4: Loop through all files

Because our files are in the collection, we need to go through them one by one and then compress them one by one.

Here we use the loop statement, the basic structure is as follows:

for element in array
do
// TODO
done
Copy the code

// Inside TODO is the specific thing you need to deal with.

So we can modify the script file as follows:

FILES=`ls`
for file in $FILES
do
echo $file
done
Copy the code

Running this will print the listed file names one by one.

If you have files in your current directory whose names contain Spaces, you will notice that the printed file names with Spaces are separated.

For example, if your file name is test 2. TXT, two lines will be printed, respectively test and 2.txt.

The ls command lists different files separated by Spaces.

Therefore, it is recommended not to have Spaces in the name. If you need to separate names, use underscores (_).

But what if there were?

Don’t panic, it’s not a problem.

The direct show code is as follows:

#! /bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for file in *
do
  echo $file
done

IFS=$SAVEIFS
Copy the code

We won’t go into script analysis. You can see the reference link at the end of the article if you are interested.

Step 5: Compress only directories

In fact, we compress the directory that contains a lot of trivial files, so we only need to compress the directory.

In addition, our script files are ordinary files, including compressed files, so there is no need to exclude them.

The code after transformation is as follows:

#! /bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for file in *
do
  if [ -d $file ]
  then
    echo $file
  fi
done

IFS=$SAVEIFS
Copy the code

The modification point is inside the do-done block.

The main thing is to add a judgment that only prints if the file is a directory.

Step 6: Replace printing with compression commands

Combined with Step2, our final script command is:

#! /bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

for file in *
do
  if [ -d $file ]
  then
    zip -rq $file.zip $file
    echo $file was successfully compressed.
  fi
done

IFS=$SAVEIFS
Copy the code

Compression success A message is printed for each file.

use

Simply copy the script command to the directory you want to compress, and then compress all directories at the same level as this file.

Remember that script files need executable permissions to run.

If no permission is displayed, run the following command to add the permission.

chmod u+x batch_zip.sh
Copy the code

download

You can download the resulting script file directly from the GitHub repository.

Github.com/nesger/Effe…

The readme.md section also has specific instructions.

Optimal point

There are a lot of things you can do to improve the script command.

Here are the examples;

  1. Supports compression of the specified directory rather than fixed to the current directory, the specified directory is carried in by the script execution parameter
  2. Tell the total number of successful compression after the compression is complete
  3. Add a logical decision to delete the original file after successful compression? Can be entered by executing the script

I’m not going to describe it here, but it’s pretty easy for the reader to implement these optimizations.

Warm prompt

In fact, if you want to change the machine, and the current system has no bugs.

A Time Machine is recommended for system migration.

It’s a migration of the entire system (including files) on your machine, saving you time to reinstall applications and setup your environment.

Refer to the link: www.cyberciti.biz/tips/handli… www.cnblogs.com/cocowool/ar…