Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 09-15-2006, 10:08 AM   #1
Registered: May 2006
Posts: 69

Rep: Reputation: 16
File name expansion with {} in gnu find and xargs

I was thinking about suggesting a second possible solution to a problem posted at when I discovered a problem of my own.

I believe that it is possible to use find to traverse a file system, select only certain types of files (such as *.txt files) and copy those files to one central location. I thought that gnu find would do the trick -- maybe piped to xargs if necessary (although I see no reason why I couldn't just use the exec flag...)

I ran into problems with I realized that I needed to place the name of the file find was currently proccessing in the middle of a command, as in:

find . -type f -name '*.txt' -exec cp current_file_name /tmp \;
My find man page says:

All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. Both of these constructions might need to be escaped (with a `\') or quoted to protect them from expansion by the shell. The command is executed in the starting directory.
So I tried:

find . -type f -name '*.txt' -exec cp {} /tmp \;

find . -type f -name '*.txt' -exec cp '{}' /tmp \;

find . -type f -name '*.txt' -exec cp \{\} /tmp \;
However, find refused to expand {} to the current file name. Instead it took the {} literally and simply placed the current file at the end of the command, so find would actually process a command that looks something like this:

cp {} /tmp path/to/current_file_name
What is going wrong?
Old 09-15-2006, 03:15 PM   #2
Senior Member
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
I'm not having any trouble. In the following example I execute an ls command in the current directory. In that directory the backup, experiment, and testdisk-6.5-WIP listings are directories. The listing is a regular file.

$ cd bin
$ mkdir /tmp/bin
$ ls
backup  experiment  testdisk-6.5-WIP
$ find . -exec cp {} /tmp/bin \;
cp: omitting directory `.'
cp: omitting directory `./experiment'
cp: omitting directory `./backup'
cp: omitting directory `./backup/old.good'
cp: omitting directory `./backup/keep'
cp: omitting directory `./testdisk-6.5-WIP'
cp: omitting directory `./testdisk-6.5-WIP/doc'
cp: omitting directory `./testdisk-6.5-WIP/linux'
cp: omitting directory `./testdisk-6.5-WIP/linux/l'
$ ls /tmp/bin
AUTHORS                       ie60fixes.css
COPYING                       iefixes.js
ChangeLog                     index.html
INFO                          intel_partition_table.html
NEWS                          linux
README                        linux_bootdisk.html
after_using_testdisk.html     lock_icon.gif
backup-command.txt            logo.png
backup-conf.txt               logo_sstic_transp.png
backup-exclude-files.txt      mail_icon.gif                main.css              media_image.html
It put all of the regular files from the current working directory downward into the /tmp/bin directory. If you want to replicate a directory tree then you are better off using cp -R or the tar command or the pax command.

When I'm using the find command with the -name option I never use single or double quotes. I have found that the behavior of the find command seems to vary from one specific situation to the next when using -name '*' type parameters. Instead of using the quotes I use the \ escape character. So where you entered
find . -type f -name '*.txt' -exec cp {} /tmp \;
I would enter
find . -type f -name \*.txt -exec cp {} /tmp \;

Last edited by stress_junkie; 09-15-2006 at 03:29 PM.
Old 09-16-2006, 03:31 PM   #3
Registered: May 2006
Posts: 69

Original Poster
Rep: Reputation: 16
OK, I think I know what I was doing wrong.

My find has an option similar to -exec: -ok. The man page says:

-ok command ;
Like -exec but ask the user first (on the standard input); if the response does not start with `y' or `Y', do not run the command, and return false.
I was originally using -ok to test things out before I ran the actual command, so that's where I was getting confused.

The output of the -ok command really threw me off.

The -exec flag works exactly as it should.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
clarification on find + xargs rm command dtra Linux - General 2 05-17-2006 05:56 AM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 08:21 AM
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 01:42 AM
error with find . | xargs cbonar Linux - Newbie 7 12-09-2004 11:22 AM
where can I find grammar file and lex file of GNU c ? thinks Huiming Linux - Software 1 06-11-2004 07:38 AM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:40 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration