LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   rename folders /files/and thier contents recursively (https://www.linuxquestions.org/questions/linux-server-73/rename-folders-files-and-thier-contents-recursively-4175639449/)

dr.x 09-30-2018 06:17 AM

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

ondoho 09-30-2018 06:34 AM

yes, this is possible.
show us what you tried and where you got stuck.
use code tags for posting code examples.

Turbocapitalist 09-30-2018 06:43 AM

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.

dr.x 09-30-2018 07:12 AM

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

Turbocapitalist 09-30-2018 07:20 AM

Quote:

Originally Posted by dr.x (Post 5909390)
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' {} \;


dr.x 09-30-2018 07:31 AM

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

Turbocapitalist 09-30-2018 07:48 AM

Quote:

Originally Posted by dr.x (Post 5909407)
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. :)

dr.x 09-30-2018 08:20 AM

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 ~]#

Turbocapitalist 09-30-2018 08:33 AM

I meant this:

Code:

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


All times are GMT -5. The time now is 06:23 PM.