LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-07-2013, 09:52 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 73

Rep: Reputation: 0
find and rename files / folders.. need some assistance.


Hi all,

managed to get a command working for my needs but i can't get one part to work.

Code:
find /path/to/folder/ -name '*_W*' -exec bash -c 'mv $0 ${0/_W}' {} \;
so this is removing a character from the files found. but i need it to move the file to a new folder is possible.

can any one assist where i need to put the new folder path as where i would normally put it for other find / move commands it doesn't work..
 
Old 04-07-2013, 12:04 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You may try the -execdir action: the command will be executed inside the directory where the file is located, therefore you can simply add the directory name to the destination argument, e.g.
Code:
find /path/to/folder/ -name '*_W*' -execdir bash -c 'echo mv $0 /path/to/destination/${0/_W}' {} \;
Maybe you already have a method for testing, but I always suggest to put an echo in front of the command under -exec or -execdir, especially when executing mv or rm. Hope this helps.
 
Old 04-07-2013, 02:13 PM   #3
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
Hi,

Thanks for that. The execdir seems to solve the initial issue of it moving it to the new directory but i know get the following where when the command is ran it is adding an additions "." in the path.

Code:
find /path/to/folder/ -name '*_W*' -execdir bash -c 'echo mv $0 /path/to/destination/${0/_W}' {} \;
Code:
mv ./path/to/folder/folder name_W /path/to/folder/./folder name
 
Old 04-07-2013, 06:13 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes, I'm aware of that. Using -execdir the matching path name is relative to the current directory and the find command always shows a path (unless you customize the output using -printf, but this would be an alternative or an addition to -execdir). The good new is that the shortcuts . and .. are allowed in paths and you can safely use the single dot with no effect: in other words the path
Code:
/path/to/destination/./file
is equivalent to
Code:
/path/to/destination/file
A little addition (sorry, I didn't think about that before): if by chance there are files with blank spaces in their name, you have to put double quotes around the arguments of the mv command:
Code:
find /path/to/folder/ -name '*_W*' -execdir bash -c 'echo mv "$0" "/path/to/destination/${0/_W}"' {} \;

Last edited by colucix; 04-07-2013 at 06:14 PM.
 
Old 04-08-2013, 08:18 AM   #5
Springs
Member
 
Registered: Apr 2008
Posts: 73

Original Poster
Rep: Reputation: 0
Thanks,

It does actually work when i take the echo out.

Thanks again.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do I rename files/folders recursively using a simple script? ixcel87 Programming 5 07-11-2012 11:02 AM
[SOLVED] rename files in folders using perl script pablgonz Programming 15 12-18-2011 12:22 PM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
Need to rename files and folders with invalid characters in the names laureynsr Linux - Newbie 5 04-01-2009 03:13 PM
Rename multiple files in folders??? adds2one Linux - Software 19 10-05-2006 12:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:01 AM.

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