Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Locate and process files with find and xargs

#1
Command line quick tips: Locate and process files with find and xargs

find is one of the more powerful and flexible command-line programs in the daily toolbox. It does what the name suggests: it finds files and directories that match the conditions you specify. And with arguments like -exec or -delete, you can have find take action on what it… finds.

In this installment of the Command Line Quick Tips series, you’ll get an introduction to the find command and learn how to use it to process files with built-in commands or the xargs command.

Finding files


At a minimum, find takes a path to find things in. For example, this command will find (and print) every file on the system:

find /

And since everything is a file, you will get a lot of output to sort through. This probably doesn’t help you locate what you’re looking for. You can change the path argument to narrow things down a bit, but it’s still not really any more helpful than using the ls command. So you need to think about what you’re trying to locate.

Perhaps you want to find all the JPEG files in your home directory. The -name argument allows you to restrict your results to files that match the given pattern.

find ~ -name '*jpg'

But wait! What if some of them have an uppercase extension? -iname is like -name, but it is case-insensitive:

find ~ -iname '*jpg'

Great! But the 8.3 name scheme is so 1985. Some of the pictures might have a .jpeg extension. Fortunately, we can combine patterns with an “or,” represented by -o. The parentheses are escaped so that the shell doesn’t try to interpret them instead of the find command.

find ~ \( -iname 'jpeg' -o -iname 'jpg' \)

We’re getting closer. But what if you have some directories that end in jpg? (Why you named a directory bucketofjpg instead of pictures is beyond me.) We can modify our command with the -type argument to look only for files:

find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f

Or maybe you’d like to find those oddly named directories so you can rename them later:

find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d

It turns out you’ve been taking a lot of pictures lately, so narrow this down to files that have changed in the last week with -mtime (modification time). The -7 means all files modified in 7 days or fewer.

find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7

Taking action with xargs


The xargs command takes arguments from the standard input stream and executes a command based on them. Sticking with the example in the previous section, let’s say you want to copy all of the JPEG files in your home directory that have been modified in the last week to a thumb drive that you’ll attach to a digital photo display. Assume you already have the thumb drive mounted as /media/photo_display.

find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7 -print0 | xargs -0 cp -t /media/photo_display

The find command is slightly modified from the previous version. The -print0 command makes a subtle change on how the output is written: instead of using a newline, it adds a null character. The -0 (zero) option to xargs adjusts the parsing to expect this. This is important because otherwise actions on file names that contain spaces, quotes, or other special characters may not work as expected. You should use these options whenever you’re taking action on files.

The -t argument to cp is important because cp normally expects the destination to come last. You can do this without xargs using find‘s -exec command, but the xargs method will be faster, especially with a large number of files, because it will run as a single invocation of cp.

Find out more


This post only scratches the surface of what find can do. find supports testing based on permissions, ownership, access time, and much more. It can even compare the files in the search path to other files. Combining tests with Boolean logic can give you incredible flexibility to find exactly the files you’re looking for. With build in commands or piping to xargs, you can quickly process a large set of files.

Portions of this article were previously published on Opensource.com. Photo by Warren Wong on Unsplash.



https://www.sickgaming.net/blog/2019/10/...and-xargs/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Contribute at Fedora Linux 34 Upgrade, Audio, and Virtualization test days xSicKxBot 0 12 04-06-2021, 10:27 AM
Last Post: xSicKxBot
  Fedora - Fedora Council statement on Richard Stallman rejoining FSF Board xSicKxBot 0 13 04-03-2021, 11:11 AM
Last Post: xSicKxBot
  Fedora - Announcing the release of Fedora Linux 34 Beta xSicKxBot 0 26 03-24-2021, 02:28 PM
Last Post: xSicKxBot
  Fedora - Fedora Workstation 34 Feature Focus: Updated Activities Overview xSicKxBot 0 31 03-17-2021, 03:08 PM
Last Post: xSicKxBot
  Fedora - How to use Poetry to manage your Python projects on Fedora xSicKxBot 0 30 03-09-2021, 10:57 AM
Last Post: xSicKxBot
  Fedora - Getting started with COBOL development on Fedora Linux 33 xSicKxBot 0 38 02-28-2021, 01:55 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora Audio, Kernel 5.11 and i18n test days xSicKxBot 0 50 02-26-2021, 01:09 PM
Last Post: xSicKxBot
  Fedora - Installing Nextcloud 20 on Fedora Linux with Podman xSicKxBot 0 73 02-16-2021, 07:51 AM
Last Post: xSicKxBot
  Fedora - Fedora Aarch64 on the SolidRun HoneyComb LX2K xSicKxBot 0 74 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  Fedora - Astrophotography with Fedora Astronomy Lab: setting up xSicKxBot 0 67 02-06-2021, 04:07 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Unigine 2.14 Released
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 2
News - Lawson Will Offer New Pokémon Sna...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 1
News - Nacon Announces WRC 10, Speeds On...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 1
News - Blizzard Arcade Collection Gets T...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 2
(Indie Deal) FREE Stranded In Time | XCO...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
Microsoft - 5 reasons to attend Azure St...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 7
News - Deals: Ring Fit Adventure Is Down...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
News - Cuphead Will Have An Awesome Deca...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
News - Nintendo Actually Responded To A ...
Last Post: xSicKxBot
Today 01:50 AM
» Replies: 0
» Views: 9
News - Rumour: Dragon Ball Z: Kakarot Co...
Last Post: xSicKxBot
Today 01:50 AM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020