InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Handling Filenames Containing Odd Characters in bash
Bash

Handling Filenames Containing Odd Characters in bash

InfinityCoder February 20, 2017

First, understand that to Unix folks, odd means “anything not a lowercase letter, or maybe a number.”

So uppercase, spaces, punctuation, and character accents are all odd. But you’ll find all of those and more in the names of many songs and bands.
Depending on the oddness of the characters, your system, tools, and goal, it might be enough to simply quote the replacement string (i.e., put single quotes around the {}, as in ‘{}’) .

You did test your command first, right?
If that’s no good, try using the -print0 argument to find and the -0 argument to xargs. -print0 tells find to use the null character (\0) instead of whitespace as the output delimiter between pathnames found. -0 then tells xargs the input delimiter.
These will always work, but they are not supported on every system.
The xargs command takes whitespace delimited (except when using -0) pathnames from standard input and executes a specified command on as many of them as possible (up to a bit less than the system’s ARG_MAX value.

Since there is a lot of overhead associated with calling other commands, using xargs can drastically speed up operations because you are calling the other command as few times as possible, rather than each time a pathname is found.

So, to rewrite the solution from Recipe 9.1, “Finding All Your MP3 Files” to handle odd characters:

1
$ find . -name '*.mp3' -print0 | xargs -i -0 mv '{}' ~/songs

Here is a similar example demonstrating how to use xargs to work around spaces in a path or filename when locating and then coping files:

1
$ locate P1100087.JPG PC220010.JPG PA310075.JPG PA310076.JPG | xargs -i cp '{}' .

There are two problems with this approach.

One is that not all versions of xargs support the -i option, and the other is that the -i option eliminates argument grouping, thus negating the speed increase we were hoping for.

The problem is that the mv command needs the destination directory as the final argument, but traditional xargs will simply take its input and tack it onto the end of the given command until it runs
out of space or input.

The results of that behavior applied to an mv command would be very, very ugly.

So some versions of xargs provide a -i switch that defaults to using {} (like find), but using -i requires that the command be run one at a time.

So the only benefit over using find’s -exec is the odd character handling.
However, the xargs utility is most effective when used in conjunction with find and a command like chmod that just wants a list of arguments to process.

You can really see a vast speed improvement when handling large numbers of pathnames.

For example:

1
$ find some_directory -type f -print0 | xargs -0 chmod 0644

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Validating Input in bash
You’ve asked for input (e.g., from a user or a …

Validating Input in bash

Preventing Core Dumps in bash
You want to prevent your script from dumping core in …

Preventing Core Dumps in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap