Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: find -exec and filenames

Tags:

find

bash

exec

w3m

I want to strip the HTML out of few hundred files.

Here's the command I've started with:

find -name *.html -exec w3m {} > w3m {}.html.out \; 

The problem I've run into is that it created one single large .htm.out file (named {}.html.out) -- I want the file I'm using to be named whatever it's original is .out.

For instance, I have

2002/filename.html

I want to run it through w3m, and get 2002/filename.html.out

Any suggestions? I'm open to other solutions that don't use bash

I'm using cygwin.

like image 246
andyhky Avatar asked Apr 27 '11 20:04

andyhky


People also ask

How do I get filename in bash?

Using Bash, there's also ${file%. *} to get the filename without the extension and ${file##*.} to get the extension alone. That is, file="thisfile.

How do I search for a specific file in bash?

You need to utilize the “-L” option and the path and “-name” option in your command. The “*” in the name specification is used for searching “all” the bash files with “. sh” extensions. It returns a total of 4 records on our screen.

What is basename in bash?

In Linux, the basename command prints the last element of a file path. This is especially useful in bash scripts where the file name needs to be extracted from a long file line. The “basename” takes a filename and prints the filename's last portion. It can also delete any following suffix if needed.

How do I search for a file in shell?

You can use the find command to search for a file or directory on your file system. By using the -exec flag ( find -exec ), matches, which can be files, directories, symbolic links, system devices, etc., can be found and immediately processed within the same command.


1 Answers

The redirection happens outside of find. Invoke a subshell.

find -name *.html -exec bash -c 'w3m "$1" > w3m-"$1".html.out' w3mout {} \; 
like image 92
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 12:09

Ignacio Vazquez-Abrams