The implementation of the linux command to compress and archive the files in the current directory into subdirectories

  • 2020-05-15 03:27:01
  • OfStack

In our daily work, we often need to archive the history files with the tar command and delete the source files to save space. If the archive is compressed into 1 file, it is relatively simple to use 1 command, such as the command: tar-czf bak2013.tgz t2013/* will package all the files in the directory t2013 into 1 compressed file. However, such files are often too large and inconvenient to use. At this time, if you can package the files under the directory t2013 into subdirectories, 1 subdirectory and 1 file, it will be convenient to use.

Of course, it is not possible to implement such a function with a single command, just use the following script:


# get directory name
subdir=`ls -l |grep ^d |awk '{printf $9" "}'`

for dt in $subdir
do
echo $dt
tar -czf $dt.tgz $dt/*
#rm -r $dt
done

If you package some files in a partial directory, you only need to make corresponding modifications, as shown below:


# get directory name
subdir=`ls -ld 201212* |grep ^d |awk '{printf $9" "}'`

for dt in $subdir
do
echo $dt
tar -czf $dt.tgz $dt/03005*.dat
done

The attached:

tar command details

-c: create compressed files

- x: decompression

-t: view the content

-r: appends a file to the end of a compressed archive

-u: updates files in the original zip

These are separate commands, and you need to use one of them to compress and uncompress, and you can use it with other commands, but you can only use one of them. The following parameters are optional when compressing or decompressing the file as needed.

-c: create compressed files

- x: decompression

-t: view the content

-r: appends a file to the end of a compressed archive

-u: update the files in the original zip

The following parameter -f is required

-f: use the file name, remember, this parameter is the last parameter, only the file name can be followed.

# tar -cf all.tar *.jpg

This command is to type all.jpg files into a package called all.tar. -c is for generating a new package, and -f specifies the file name of the package.

# tar -rf all.tar *.gif

This command is to add all.gif files to all.tar packages. -r means to add files.

# tar -uf all.tar logo.gif

This command is to update the logo.gif file in the original tar package all.tar, -u means to update the file.

# tar -tf all.tar

This command lists all the files in the all.tar package. -t lists the files

# tar -xf all.tar

This command is to unpack all the files in the all.tar package, -t means unpack

The compression

tar, cvf, tar *.jpg // package all the jpg files in the directory into tar.jpg

jpg // package all the jpg files in the directory into jpg.tar and compress them with gzip to produce a package with gzip compressed, named jpg.tar.gz

tar, cjf, tar. bz2 *.jpg // package all the jpg files in the directory into jpg. tar, and then compress them with bzip2 to produce a package with bzip2 compressed, named jpg. tar. bz2

tar.Z *.jpg // package all the jpg files in the directory into jpg.tar and then compress them with compress to produce a package that is umcompress compressed and named jpg.tar.Z

rar a jpg. rar *.jpg //rar, you need to download rar for linux

zip jpg. zip *.jpg //zip compression, you need to download zip for linux

Unpack the

tar, xvf, file. tar // extract the tar package

tar-xzvf file. tar. gz // extract tar. gz

tar-xjvf file. tar. bz2 // extract tar. bz2

tar, xZvf, file. tar. Z // extract tar. Z

unrar e file.rar // unzip rar

unzip file.zip // unzip zip

conclusion

1. *. Extract tar with tar and xvf

2. *.gz is extracted by gzip-d or gunzip

3. *.tar.gz and *.tgz are extracted with tar and xzf

4. *.bz2 can be extracted by bzip2-d or bunzip2

5. *.tar.bz2 extract with tar and xjf

6. *.Z decompress with uncompress

7. *.tar.Z can be extracted with tar and xZf

8, *.rar extract with unrar e

9. *.zip is extracted by unzip


Related articles: