Operating System - Linux
1751873 Members
5107 Online
108782 Solutions
New Discussion юеВ

Re: Using find command to add common suffix to all files.

 
SOLVED
Go to solution
Danny Fang
Frequent Advisor

Using find command to add common suffix to all files.

Hi,

I'm attempting to use the find command to list all files within a directory, add a ".txt" extension to their existing filenames.

However, I got only a single file, and in another instance an error msg indicating that the files are similar. See output below.
bash-2.05$ ls -l
total 0
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 f1
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 f2
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 f3
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 f4
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 f5
bash-2.05$ find . -type f -name "f*" -exec mv {} "{}.txt" \;
bash-2.05$ ls -l
total 0
-rw-r--r-- 1 danny nwwls 0 Oct 7 19:15 {}.txt
bash-2.05$

bash-2.05$ find . -type f -name "f*" -exec mv {} "f*.txt" \;
mv: ./f*.txt and f*.txt are identical
bash-2.05$


I had to use these in the bash or perl scripts:
for i in `ls `
do
mv $i $i.txt
done

OR:
#!/usr/bin/perl
#
use strict;
use File::Copy;

while (<@ARGV>) {
my $ori = $_;
rename($ori, "$ori\.txt");
}

Could anyone show me how can I achieve the same as I did using the scripts, on a command line?

Thanks
Danny

5 REPLIES 5
Steven Schweda
Honored Contributor

Re: Using find command to add common suffix to all files.

> [...] -exec mv {} "{}.txt" \;

Don't quote the "{}.txt"? It makes sense to
quote a file name in a script, because the
name could include a space or other special
character, but if "find -exec" can't deal
with an unquoted
{}.txt
then there's probably no hope for a plain
{}
either, and you're using that already.

> mv $i $i.txt

And in this script you quote nothing, so why
annoy the "find" command with quotation?
Danny Fang
Frequent Advisor

Re: Using find command to add common suffix to all files.

HI Steven,

I've tried unquoting the {}.txt as you've suggested. However, that did not work as it produced the same result as before:

bash-2.05$ ls -l
total 0
-rw-r--r-- 1 danny nwwls 0 Oct 7 23:50 f1
-rw-r--r-- 1 danny nwwls 0 Oct 7 23:50 f2
-rw-r--r-- 1 danny nwwls 0 Oct 7 23:50 f3
bash-2.05$ find . -type f -name "f*" -exec mv {} {}.txt \;
bash-2.05$ ls -l
total 0
-rw-r--r-- 1 danny nwwls 0 Oct 7 23:50 {}.txt
bash-2.05$

Appreciate it if you could point to me where did I go wrong in this.

Thanks
Danny
Ivan Ferreira
Honored Contributor
Solution

Re: Using find command to add common suffix to all files.

Maybe you could do something like:

for FILE in $(find . -type f -name "f*" )
do
mv ${FILE} ${FILE}.txt
done
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Steven Schweda
Honored Contributor

Re: Using find command to add common suffix to all files.

Oops. "find" replaces "{}" only when it sees
it as a "command argument", that is, as an
isolated token. "find" has enough
limitations that I don't see a good way to
avoid using a script (in some form or other).

You could write a little script like this:

sol# cat ../append_name.sh
#!/bin/sh
mv "$1" "${2}${3}"

and then use it:

sol# ls -l
total 6
-rw-r--r-- 1 root root 4 Oct 7 15:51 a
-rw-r--r-- 1 root root 4 Oct 7 15:51 b
-rw-r--r-- 1 root root 4 Oct 7 16:11 c c

sol# find . -name '*' -exec ../append_name.sh {} {} '.txt' \;

sol# ls -l
total 6
-rw-r--r-- 1 root root 4 Oct 7 15:51 a.txt
-rw-r--r-- 1 root root 4 Oct 7 15:51 b.txt
-rw-r--r-- 1 root root 4 Oct 7 16:11 c c.txt

On the bright side, "find" does seem to pass
a funny file name (like "c c") as a unit, so
if the script quotes things properly, funny
file names won't (always) cause trouble.

That was done on a Solaris system, but if
you've seen one UNIX, ...
James R. Ferguson
Acclaimed Contributor

Re: Using find command to add common suffix to all files.

Hi Danny:

# find /path -type f -print|xargs -i mv {} {}.txt

Regards!

...JRF...