- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Using find command to add common suffix to all fil...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 03:30 AM
тАО10-07-2008 03:30 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 06:24 AM
тАО10-07-2008 06:24 AM
Re: Using find command to add common suffix to all files.
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 08:00 AM
тАО10-07-2008 08:00 AM
Re: Using find command to add common suffix to all files.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 11:57 AM
тАО10-07-2008 11:57 AM
Solutionfor FILE in $(find . -type f -name "f*" )
do
mv ${FILE} ${FILE}.txt
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 01:15 PM
тАО10-07-2008 01:15 PM
Re: Using find command to add common suffix to all files.
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, ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-07-2008 02:42 PM
тАО10-07-2008 02:42 PM
Re: Using find command to add common suffix to all files.
# find /path -type f -print|xargs -i mv {} {}.txt
Regards!
...JRF...