LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-30-2018, 06:17 AM   #1
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Rep: Reputation: Disabled
rename folders /files/and thier contents recursively


hey Guys
looking for :

rename folders /files/and thier contents recursively


i have a parent folder with sub files/ directories over 1000.

i want :
1- rename all folders & subfolders with name "fire" to "rock"
2-rename all (files names) in all folders/sub folders with name "lion" to "dust"

the path is /root/mic

i tried many ways and googled much and none worked !

Thanks
 
Old 09-30-2018, 06:34 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
yes, this is possible.
show us what you tried and where you got stuck.
use code tags for posting code examples.
 
Old 09-30-2018, 06:43 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
As a hint for where to start, you might look at the find and / or rename utilities. There are two variants of the rename utility. The perl-based one is the useful one.
 
Old 09-30-2018, 07:12 AM   #4
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
here what i tried and none worked :

##############3

find . -name "*.*" -exec rename -v 's/\lion/\dust/i' {} \;

find /root/mic -type f | egrep '/fire' | rename -n 's/fire/rock\1/'
################


any ideas im listening for ....

Thanks
 
Old 09-30-2018, 07:20 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by dr.x View Post
1- rename all folders & subfolders with name "fire" to "rock"
2-rename all (files names) in all folders/sub folders with name "lion" to "dust"
The rename utility has a -n option to show you what it would do, but without actually doing it. Look it up in the rename manual.

Then in the find manual, look up -type, -name, and maybe -exec.

Then skim xargs, optionally.

Code:
man xargs
man find
man rename
You can use either find with -exec or else xargs for this but not both.

Code:
find /root/mic/ -type d -name '*foo* -exec rename -n 's/foo/bar' {} \;
find /root/mic/ -type f -name '*foo* -exec rename -n 's/foo/bar' {} \;
 
Old 09-30-2018, 07:31 AM   #6
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
ok im trying it on squid folder .

im looking for replacing squid with stinger.

here is result :

[root@li1802-227 ~]# find /root/squid-3.5.22 -type f -name *squid* -exec rename -n 's/squid/stinger' {} \;
[root@li1802-227 ~]# find /root/squid-3.5.22 -type d -name *squid* -exec rename -n 's/squid/stinger' {} \;
[root@li1802-227 ~]#
[root@li1802-227 ~]#
[root@li1802-227 ~]#
[root@li1802-227 ~]# cd squid-3.5.22/
[root@li1802-227 squid-3.5.22]# ls
acinclude config.logstingerstinger COPYING include Makefile.amstinger scripts tools
aclocal.m4stingerstinger config.statusstinger CREDITS INSTALL Makefile.instingerstinger SPONSORS
bootstrap.shstinger configure doc lib po4a.confstinger squid
cfgaux configure.acstinger errors libltdl QUICKSTART src
ChangeLog contrib helpers libtool README stingerclient
compat CONTRIBUTORS icons Makefile RELEASENOTES.htmlstingerstinger test-suite
[root@li1802-227 squid-3.5.22]#






i still go to folder not changed !!

thanks
 
Old 09-30-2018, 07:48 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by dr.x View Post
i still go to folder not changed !!
1) The quotes are missing for -name, without them it will be the shell which interperest the asterisks, not find

2) The manual page for rename went unread.
 
Old 09-30-2018, 08:20 AM   #8
dr.x
Member
 
Registered: Jan 2013
Posts: 231

Original Poster
Rep: Reputation: Disabled
Did you mean this :

find /root/squid-3.5.22 -type d -name "*squid* -exec rename -n 's/squid/stinger''' {} \;




[root@li1802-227 ~]# find /root/squid-3.5.22 -type d -name '*squid* -exec rename -n 's/squid/stinger'' {} \;
find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name `*squid* -exec rename -n s/squid/stinger'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `*squid* -exec rename -n s/squid/stinger''.
find: paths must precede expression: {}
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@li1802-227 ~]#
 
Old 09-30-2018, 08:33 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
I meant this:

Code:
man rename
You're very close, but two small changes remain in regards to rename
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rename HIDDEN files recursively mrmnemo Linux - Software 26 09-17-2012 05:20 AM
[SOLVED] How do I rename files/folders recursively using a simple script? ixcel87 Programming 5 07-11-2012 11:02 AM
Recursively rename some files mail4mz Programming 21 07-15-2011 10:18 AM
script to rename files recursively rs232 Linux - General 3 05-08-2011 02:05 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:57 PM.

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