Linux Commands

How to Archive/Compress Files & Directories, Setting File Attributes and Finding Files in Linux

In this tutorial, we will look at the three important Linux file management tasks: archiving/compressing files and directories, setting the file characteristics, and discovering the files. These activities are critical for effective file organization, storage, and retrieval. Whether we are a novice or an experienced Linux user, understanding these skills can significantly increase our productivity and effectiveness in file management. So, let’s get started and learn how to archive/compress the files and directories in Linux, modify file attributes, and discover the files.

Archiving/Compressing the Files and Directories

Archiving and compressing the files and directories is critical to save the disc space, transmit the data, and create backups. To do these operations, Linux provides a variety of command-line utilities such as tar and zip.

1. Tar-based Archiving

The most often used “tar” commands:

Long option Abbreviation Description
  –create  c Makes a tar archive
–extract or –get x Files are extracted from an archive
  –list  t The contents of a tarball are listed
–file archive   f Makes use of an archive file or device: ARCHIVE
–diff or –compare d Discovers the distinctions between an archive and a file system
  –append r Items are added to the conclusion of a file
  –concatenate   A Tar files are appended to an archive
–update u Appends the files that are newer than the archive’s copy

Typically used operation modifiers:

Long option Abbreviation Description
–directory dir c Before executing the operations, we make changes to the “dir” directory.
–xz J Xz is used to process an archive.
–bzip2 j Bzip2 is used to process an archive.
–gzip or –gunzip z Gzip is used to compress an archive.
  –exclude=pattern X It excludes the pattern-based file.
  –verify W It is verified after writing the archive.
–verbose v It lists all files that have been read or extracted. When combined with -list, this flag displays the file sizes, ownership, and time stamps.
 –same-permissions p Original permissions are preserved.

Enter the following code to create a “tar” archive:

tar -cvf archive.tar file1 file2 directory1

2. Using Gzip to Compress

Once we built a “tar” archive, we can conserve the disc space by compressing it with the “gzip” command. In Linux, the “gzip” compression method is commonly used.

gzip archive.tar

This compresses the “archive.tar” file, producing a compressed file named “archive.tar.gz”. The “.gz” extension denotes a “gzip” compression.

Setting or Configuring the File Attributes

We can change the rights, ownership, timestamps, and other file properties in Linux. The file properties that are properly configured ensure security, access control, and efficient file management.

Modifying the File Permissions

File permissions manage the read, write, and executes an access for several user categories including the owner, group, and others. The “chmod” function is used to alter or update the privileges of a file. Here’s an example:

chmod 755 file.txt

This command grants the read, write, and executes an access to the file owner (7), reads and executes permissions to the group (5), and reads and executes permissions to others (5).

Finding Files or Folders in Linux

Searching for certain files or folders is a typical activity in Linux. Fortunately, Linux has sophisticated command-line tools to search for files based on a variety of parameters.

Locating the Files Using a Name

The “find” command allows us to look for files by their names. Here’s an illustration:

find /path/to/search -name filename.txt

Replace “/path/to/search” with the location of the search and the “filename.txt” with the name of the file that we’re looking for.

Finding the Files Based on Their Type

We may also find the files by type such as ordinary files, folders, symbolic links, and so on. The “locate” command’s “-type” argument lets us specify the file type. For example:

find /path/to/search -type f

This command searches for all regular files in the provided directory.

Conclusion

Learn the basic skills of effective file location, file archiving, compression, and modification to master the art of successful file management in Linux. Improve our efficiency and productivity as a Linux user by organizing, storing, and retrieving the data more effectively. We can use these techniques to fully utilize Linux’s file management capabilities.

Similar Posts