Linux find with tar

Find and tar Files on Linux

One of the most powerful features of the Linux operating system is the ability to find and manipulate files quickly and easily from the command line. This can be especially useful when working with large numbers of files or when you need to automate certain tasks. In this article, we will take a look at two of the most commonly used command-line tools for finding and compressing files on Linux: the find command and the tar command.

Finding Files with the find Command

The find command is a powerful tool that allows you to search for files on your Linux system based on a variety of criteria. Here are a few examples of how you can use the find command −

To find all files in the current directory and its subdirectories that have the .txt file extension, you can use the following command 7minus;

To find all files in the /home/user directory that have been modified in the last 30 days, you can use the following command −

To find all files in the /home/user directory that are larger than 1GB, you can use the following command −

Compressing Files with the tar Command

The tar command is used to create, extract, and manipulate archive files on Linux. tar stands for «Tape Archive» and it was originally used to write data to tape drives. Today, it is commonly used to create archive files that can be easily shared or transferred over the internet. Here are a few examples of how you can use the tar command −

To create a new archive file called myfiles.tar that contains all files in the /home/user directory, you can use the following command −

tar -cf myfiles.tar /home/user

To extract all files from the myfiles.tar archive file, you can use the following command −

To add a new file called file.txt to an existing archive file called myfiles.tar, you can use the following command −

tar -uf myfiles.tar file.txt

It is important to note that the tar command does not compress the files by default. However, it can be used in conjunction with other command such as gzip to compress the files while creating the archive. For example, to create a compressed archive file called myfiles.tar.gz that contains all files in the /home/user directory, you can use the following command −

tar -czf myfiles.tar.gz /home/user

By using the find and tar commands together, you can easily find and manipulate large numbers of files on your Linux system. With a little practice and experimentation, you will soon become proficient at automating tasks and managing files with these powerful command-line tools.

Читайте также:  Подключение флешки в virtualbox linux

Finding and Compressing Specific Types of Files

In addition to searching for files based on specific criteria such as file size or modification date, the find and tar commands can also be used to search for and compress specific types of files. For example, you may only want to find and compress all image files in a certain directory or all files with the .log extension.

Here are a few examples of how you can use the find command to search for specific types of files −

To find all image files (with extensions such as .jpg, .png, and .gif) in the /home/user directory, you can use the following command −

find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif"

To find all files with the .log extension in the /var/log directory, you can use the following command −

You can then use the tar command to compress the files that were found by the find command. Here are a few examples of how you can use the tar command to compress specific types of files −

To create a compressed archive file called images.tar.gz that contains all image files in the /home/user directory, you can use the following command −

tar -czf images.tar.gz $(find /home/user -iname "*.jpg" -o -iname "*.png" -o -iname "*.gif")

To create a compressed archive file called logfiles.tar.gz that contains all files with the .log extension in the /var/log directory, you can use the following command −

tar -czf logfiles.tar.gz $(find /var/log -name "*.log")

Conclusion

The find and tar commands are powerful tools that can be used to find and compress large numbers of files on a Linux system. By using specific search criteria and command options, you can easily find and compress specific types of files such as image files or log files. With a little practice and experimentation, you will soon become proficient at automating tasks and managing files with these powerful command-line tools.

Читайте также:  Iptv player linux mint

Источник

How to tar multiple files with the find command

I am trying to tar multiple files using the find command. I want to find all the files in the directory that contain a specific string in the file name and then tar those files. The string that I am want to find in the file name is the date. For instance, I have a file name named ulog.20120914.log What I’m doing right now is:

DAYTWOPREV=`date +%Y%m%d --date='2 days ago'` function archive < cd $1; if [ ! -d archive ]; then mkdir archive; fi TMPFILE=`mktemp`; find . -maxdepth 1 -name "$*" -type f -print0 > $TMPFILE; TARFILE=archive/$$2.tar; if [ ! -e $TARFILE ]; then echo tar cfT $TARFILE /dev/null; tar cfT $TARFILE /dev/null; fi cat $TMPFILE | xargs -0r tar rf $TARFILE cat $TMPFILE | xargs -0r rm -rf rm -f $TMPFILE; > 

4 Answers 4

Assuming you have GNU tar (i.e. on Linux, not AIX, or HP-UX or . ):

find . -maxdepth 1 -type f -print0 | grep -z "$" | tar -cvf archive.tar --null -T /dev/stdin 

If the three tar files match the name you specified, then that’s the correct behaviour. If you wanted all the names that match except those that end with .tar , you have to tell find (or tar ) to exclude the names you want excluded. You might also need the -type f option (but tar files match that criterion anyway).

The «*$*» bit is problematic, because, in double quotes, the shell might expand that before handing it to find . However, putting it in single quotes means the variable won’t get expanded either. Editing to suggest an alternative.

ok i changed my edit. i was trying to respond inijus comment but responded to yours instead. but im just testing it with one file at the moment but there are two in the archive.

What you wrote originally was fine; the double quotes mean the shell won’t expand the metacharacters ( * ).

You can tell GNU tar to read the list of files to archive from its standard input:

find . -maxdepth 1 -name "$*" -type f | tar -czf archive.tar.gz -T - 

The -T — is the magic; -T means ‘read file list from given file’, and the (second) — indicates ‘the file is standard input’. (I added the z option to compress the file using gzip and named the tar file accordingly. You might prefer j and bzip2 with the .bz2 extension. You might find your tar supports xz compression natively; the option letter is -J on Mac OS X and BSD. And BSD supports —lzma for LZMA compression.)

Читайте также:  Linux install shared library

Also, with GNU tar , you can specify option —null (and then with GNU find use -print0 ) to handle even file names containing newlines. As written, this runs into issues if the file names contain newlines; otherwise, it works one file per line reliably enough, spaces and all.

Источник

How to combine the ‘tar’ command with ‘find’

This is semi+off topic, but going forward with the find command, you should quote the search term. It works without sometimes but not always.

7 Answers 7

Note: See @Iain’s answer for a somewhat more efficient solution.

Note that find will call the -exec action for every single file it finds.

If you run tar -cvf file.tar <> for every single file find outputs, this means you’ll overwrite file.tar every time, which explains why you end up with one archive left that only contains anaconda.storage.log — it’s the last file find outputs.

Now, you actually want to append the files to the archive instead of creating it each time (this is what the -c option does). So, use the following:

find var/log/ -iname "anaconda.*" -exec tar -rvf file.tar <> \; 

The -r option appends to the archive instead of recreating it every time.

Note: Replace -iname anaconda.* with -iname «anaconda.*» . The asterisk is a wildcard and can be expanded by your shell before find even sees it. To prevent this expansion, wrap the argument in double quotes.

As for tar removing leading / : The archive should only contain relative file names. If you added files with a leading / , they would be stored as absolute file names, literally meaning /var/… on your computer, for example.

IIRC this is simply a precaution for tar implementations other than GNU, and it’s safer this way because you won’t overwrite your actual data in /var/… when you extract the archive if it contains relative filenames.

Источник

Оцените статью
Adblock
detector