LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2019, 10:01 PM   #1
Lin Wai Htet
LQ Newbie
 
Registered: Mar 2018
Location: Yangon
Distribution: Centos
Posts: 7

Rep: Reputation: Disabled
How to rename all the files in centos


I want to rename all the files ( I mean all the same files in different directories)in centos , please help me to fix.
 
Old 09-09-2019, 10:06 PM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Quote:
Originally Posted by Lin Wai Htet View Post
I want to rename all the files ( I mean all the same files in different directories)in centos , please help me to fix.
That question is so vague as to be unanswerable...
man rename
man mv
 
1 members found this post helpful.
Old 09-09-2019, 10:14 PM   #3
Lin Wai Htet
LQ Newbie
 
Registered: Mar 2018
Location: Yangon
Distribution: Centos
Posts: 7

Original Poster
Rep: Reputation: Disabled
No I try with one file name "a" with this command ( find / -type f -name a -exec mv {} b \; )in (test) directory and the file "a" also in (test1) directory, but the file "a" in (test) directory change to "b" but the file "a" in (test1) directory is removed.

Last edited by Lin Wai Htet; 09-09-2019 at 10:16 PM.
 
1 members found this post helpful.
Old 09-09-2019, 10:18 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,635

Rep: Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205Reputation: 1205
Quote:
Originally Posted by Lin Wai Htet View Post
No I try with one file name "a" with this command ( find / -type f -name a -exec mv {} b \; )in (test) directory and the file "a" also in (test1) directory, but the file "a" in (test) directory change to "b" but the file "a" in (test1) directory is removed.
Yup...that’s what you told it to do...change the name of file a to b.
File a wasn’t “removed” it just had its name changed to b

Maybe you need to tell us more about what you are wanting to accomplish.
 
1 members found this post helpful.
Old 09-09-2019, 11:13 PM   #5
Lin Wai Htet
LQ Newbie
 
Registered: Mar 2018
Location: Yangon
Distribution: Centos
Posts: 7

Original Poster
Rep: Reputation: Disabled
Lightbulb

I mean the file "a" in (test) directory change to "b" but the file "a" in (test1) directory does not change to b and is removed. I want to also change "a" in (test1) directory changed to "b". I mean I want to also change "a" in (test2) directory to "b".I dont want to get "a" removed in (test1) and (test2) and other directories. Please help me.
 
Old 09-09-2019, 11:30 PM   #6
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,285

Rep: Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858
This is what I understand:

You have two directories, test1 and test2.
If two files, that reside in test1 and test2 respectively, have the same name, rename them.
For example, if there are files test1/a and test2/a, they have to be renamed test1/b and test2/b.

Is this correct?

If so, I have a question:

Do you only want to process files that are in test1 and test2 directly, or also files that are in subdirectories, such as test1/sub/a and test2/sub/a? If so, you need to be clear what you mean by "same name". For example, would test1/sub1/a and test2/sub2/a have the same name?
 
Old 09-09-2019, 11:45 PM   #7
Lin Wai Htet
LQ Newbie
 
Registered: Mar 2018
Location: Yangon
Distribution: Centos
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes, I mean all the same name files "a" in all the whole system directories change name to "b". Please help me.
 
Old 09-10-2019, 01:35 AM   #8
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: Redhat/Centos, Ubuntu, Raspbian, Fedora, Alpine, Cirros, OpenSuse/SLES
Posts: 3,285

Rep: Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858Reputation: 858
To rename the files, your command should work almost unchanged:
Code:
find test1 -type f -name a -exec mv {} b \; 
find test2 -type f -name a -exec mv {} b \;
Now the first question is, how do you determine which files have to be renamed? My problem is that I don't quite understand what you mean by "all the same files in different directories".
The second question is, what is the new name?
 
1 members found this post helpful.
Old 09-10-2019, 01:59 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,307
Blog Entries: 9

Rep: Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310Reputation: 3310
Quote:
Originally Posted by Lin Wai Htet View Post
Yes, I mean all the same name files "a" in all the whole system directories change name to "b". Please help me.
And that is what you told 'find' to do.
Have you considered what the command does?
Have you considered what {} gets expanded to?
Have you considered that 'b' is only one file in your current directory, whereas {} can be all over the place? and all these files are getting moved to b in your current directory.
Read 'man find', maybe it has a rename option.
Read 'man mv', maybe it has a no-clobber option.
 
Old 09-10-2019, 02:01 AM   #10
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,636

Rep: Reputation: 277Reputation: 277Reputation: 277
Exclamation

Quote:
Originally Posted by Lin Wai Htet View Post
Yes, I mean all the same name files "a" in all the whole system directories change name to "b". Please help me.
Are you creating a malware? Why would you rename files in system directories?
 
Old 09-10-2019, 04:51 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,999

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
unless you can provide a clear pattern for what you want the files renamed to it will end in tears.

as has already been pointed out by @ondoho your current command moves *everything* named a to the single file, "at your feet" aka $PWD (print working directory) named b

in effect, you have deleted all the matching files apart from the last one that find found.
That was moved to $PWD/b

find's -exec , is handy, I use it quite a bit for basic things

however
Code:
while read file
do
   echo \"mv \"${file}\" \"${file%/*}/b\"
done < <(find / -type f -name a)
that will move all files named a to b in the same dir as a was found
it is dumb, It will overwrite existing files

Code:
while read file
do
   if [[ ! -e "${file%/*}/b" ]]
   then
       echo "\"mv \"${file}\" \"${file%/*}/b\""
   else
       echo "# \"${file%/*}/b\" already exists.. not moving \"${file}\""
   fi
done < <(find / -type f -name a)
safer
if b already exists in the same dir as a was found, skip it.


Notice I'm using echo, and I'm escaping some " with \
this is so I can check I will be doing what I want *before* I actually do it
If I were happy with the result I would

Code:
<up arrow> | sh
sh would then perform the moves


It is easy for me to write those, you may need more experience to write those without error. I have also not explained what the %/* did

and the little script I have written is redundant

what you probably want is rename

but you are going to need to learn perlexpr

first part of man rename
Code:
RENAME(1p)                                  User Contributed Perl Documentation                                  RENAME(1p)

NAME
       rename - renames multiple files

SYNOPSIS
       rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -e|-E perlexpr]*|perlexpr [ files ]

DESCRIPTION
       "rename" renames the filenames supplied according to the rule specified as the first argument.  The perlexpr
       argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames
       specified.  If a given filename is not modified by the expression, it will not be renamed.  If no filenames are
       given on the command line, filenames will be read via standard input.

       For example, to rename all files matching "*.bak" to strip the extension, you might say

               rename 's/\.bak$//' *.bak

       To translate uppercase names to lower, you'd use

               rename 'y/A-Z/a-z/' *
tl;dr use rename
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: GUI To Batch Rename Files On Linux With Exif And Music Tags Support: Inviska Rename LXer Syndicated Linux News 0 05-25-2019 12:07 PM
LXer: How to rename user in Linux (also rename group & home directory) LXer Syndicated Linux News 0 06-26-2018 12:54 PM
LXer: How to rename user in Linux (also rename group & home directory) LXer Syndicated Linux News 0 11-28-2017 07:53 PM
Rename network interface (Rename ppp0 ...) Nicolas1390 Linux - Newbie 2 08-08-2011 02:14 AM
Rename all ".JPG" files to ".jpg" under all subfolders... jiapei100 Programming 4 04-25-2010 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration