You are not logged in.

#1 2020-01-17 21:39:01

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

[NULLIFIED] Renaming files recursively via terminal?

Hi there folks!

I need to do the following via terminal.  In /home/me/stuff, I need to change about 400 instances of __resource.lua to fxmanifest.lua.  There's one of these files in every child directory.  I'm googling but haven't found a scenario that fits mine exactly.

Could someone clue me in on how I would go about doing this?

Thanks for your time!

Last edited by schwim (2020-02-13 20:17:31)


Schw.im! A social site with an identity crisis.

Offline

#2 2020-01-17 22:18:24

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,605

Re: [NULLIFIED] Renaming files recursively via terminal?

sudo apt install rename # should already be installed
cd /home/me/stuff
rename s/__resource/fxmanifest/ */__resource.lua

The show must go on.

Offline

#3 2020-01-17 22:31:48

Sun For Miles
Member
Registered: 2017-04-12
Posts: 173

Re: [NULLIFIED] Renaming files recursively via terminal?

^or

find /home/me/stuff -type f -name "__resource.lua" -exec rename '__resource.lua' 'fxmanifest.lua' {} \;

Señor Chang, why do you teach Spanish?

Online

#4 2020-01-18 16:17:33

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Hi there guys and thanks for your help!

I tried both of the solutions and neither worked for me.  @twoion , When I ran your suggestion, it completed immediately and no files were changed.  @Sun For Miles, when I ran yours, I got a few hundred of these with no changed files:

Bareword "lua" not allowed while "strict subs" in use at (user-supplied code).
Bareword "__resource" not allowed while "strict subs" in use at (user-supplied code).


Schw.im! A social site with an identity crisis.

Offline

#5 2020-01-18 16:46:14

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Think I fell on it:

find ./resources/ -iname '__resource.lua' -execdir mv -i '{}' fxmanifest.lua \;


Schw.im! A social site with an identity crisis.

Offline

#6 2020-01-18 18:45:29

Sun For Miles
Member
Registered: 2017-04-12
Posts: 173

Re: [NULLIFIED] Renaming files recursively via terminal?

I'm glad you got the right idea! However, I would love to figure out why neither of our suggestions worked, for start we can compare systems we tried this on:

[root@pi ~]# cat /etc/*release
CentOS Linux release 7.7.1908 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

CentOS Linux release 7.7.1908 (Core)
CentOS Linux release 7.7.1908 (Core)

[root@pi ~]# rename -V
rename from util-linux 2.23.2

[root@pi ~]# find -version
find (GNU findutils) 4.5.11
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)

Señor Chang, why do you teach Spanish?

Online

#7 2020-01-19 08:55:38

ohnonot
...again
Registered: 2015-09-29
Posts: 4,291
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

fwiw: since the renaming is completely static, I'd just use mv:

find -name __resource.lua | while read file; do echo mv "$file" "${file/__resource.lua/fxmanifest.lua}"; done

Try that, and if you like what you see, remove the echo and run it again.

edge cases where the string __resource.lua might appear more than once in a path or where a directory might be called __resource.lua are not considered.

Last edited by ohnonot (2020-01-19 08:56:31)

Offline

#8 2020-02-10 13:29:44

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Hey guys, very sorry for the delay, it seems I'm back at this project.  I've done a bit more research and I need to do more than just rename the __resource.lua file.  I also need to add the following to each of those files:

fx_version 'adamant'

game 'gta5'

including the newlines.  Can you guys suggest the best way to add this to those files via terminal, either before or after renaming?


Schw.im! A social site with an identity crisis.

Offline

#9 2020-02-10 13:37:45

damo
....moderator....
Registered: 2015-08-20
Posts: 5,563

Re: [NULLIFIED] Renaming files recursively via terminal?

schwim wrote:

Hey guys, very sorry for the delay, it seems I'm back at this project.  I've done a bit more research and I need to do more than just rename the __resource.lua file.  I also need to add the following to each of those files:

fx_version 'adamant'

game 'gta5'

including the newlines.  Can you guys suggest the best way to add this to those files via terminal, either before or after renaming?

echo -e "fx_version 'adamant'\n\ngame 'gta5'\n" >> $filename

"Linux Append to File 101"  tongue


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#10 2020-02-10 14:37:47

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

echo -e "fx_version 'adamant'\n\ngame 'gta5'\n" >> $filename

"Linux Append to File 101"  tongue

I do understand prepending content into a file, I should have explained my confusion better.  I'm having an issue figuring out how to add that to the command I'm using to rename files.  I need to manipulate this to do both the rename and add the content:

    find ./resources/ -iname '__resource.lua' -execdir mv -i '{}' fxmanifest.lua \;

Schw.im! A social site with an identity crisis.

Offline

#11 2020-02-10 20:11:41

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,989
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Quick try with z-shell (handle with care).

p.s. According to wikipedia, one of z-shell features is also

Extended file globbing allows file specification without needing to run an external program such as find

edit: Expanding to linked zsh tutorial, echo line could be something along

echo "fx_version 'adamant'\n\ngame 'gta5'" # >> glob here

Last edited by brontosaurusrex (2020-02-10 21:03:44)

Offline

#12 2020-02-11 23:07:13

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

brontosaurusrex wrote:

Quick try with z-shell (handle with care).

Mr. Rex, thanks so much but I'm going to be honest, this is well above my level of understanding. I fear I'm going to have some questions ?


Schw.im! A social site with an identity crisis.

Offline

#13 2020-02-11 23:08:29

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

brontosaurusrex wrote:

Quick try with z-shell (handle with care).

Mr. Rex, thanks so much but I'm going to be honest, this is well above my level of understanding. I fear I'm going to have some questions ?


Schw.im! A social site with an identity crisis.

Offline

#14 2020-02-11 23:37:14

damo
....moderator....
Registered: 2015-08-20
Posts: 5,563

Re: [NULLIFIED] Renaming files recursively via terminal?

Have you thought of scripting it, as previously suggested?

Pseudocode:

grep for each file
echo "text" >> file
mv file

It would be my approach, since find syntax is complex, and chaining it makes it worse  monkey


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#15 2020-02-12 08:25:11

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 1,989
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

schwim wrote:

I fear I'm going to have some questions ?

The answer lies where the question is asked. Just ask.

Last edited by brontosaurusrex (2020-02-12 10:37:51)

Offline

#16 2020-02-13 20:17:10

schwim
Member
From: Western NC, US
Registered: 2015-09-29
Posts: 253
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Hi there guys and thanks a bunch for all the help.  Since this was a necessity and not a learning exercise for me and time was of the essence, I went ahead and just modified the 300'ish files.

Solved!


Schw.im! A social site with an identity crisis.

Offline

#17 2020-02-14 06:34:30

ohnonot
...again
Registered: 2015-09-29
Posts: 4,291
Website

Re: [NULLIFIED] Renaming files recursively via terminal?

Well you are going to have to learn it eventually.
You had so many partial solutions offered to you, you could have easily put in the effort and had it solved long before yesterday.
As a server admin you should have a minimum of command line knowledge, and this (find, rename, ad-hoc shell scripting a.k.a. oneliners) is really basic stuff. Also meaningful troubleshooting when something does not work as expected.
I'm not trying to bring you down, but this needs to be said IMO. Sorry.

Offline

Board footer

Powered by FluxBB