LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-12-2008, 09:40 AM   #1
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Rep: Reputation: 0
n00b needs help for renaming files


Hi,

Ive just started using linux, and Im trying both Ubuntu and CentOS. So far so good, but I am having a problem renaming files. I've tried the following:

find /home/*/ -iname ".new-account | mv ".new-account" ".new-user"

no luck. So I tried

find /home/*/ -iname ".new-account | rename 's/\.new-account/\.new-user/'

no luck. Then I tried

find /home/*/ -iname ".new-account | while read a; do mv "$a" .new-user ; done

Still no luck.

Its CentOS that I really need to get this to work on, so any input you guys may have would be really appreciated. TIA!
 
Old 11-12-2008, 09:49 AM   #2
masonm
Senior Member
 
Registered: Mar 2003
Location: Following the white rabbit
Distribution: Slackware64 -current
Posts: 2,300

Rep: Reputation: 90
Is this a homework/class assignment?
 
Old 11-12-2008, 09:56 AM   #3
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Original Poster
Rep: Reputation: 0
no! I work in an office where we all user Vista, but our servers run linux. The management saw fit to fire 3/4's of the staff on Monday and now they are looking to do some new things. I've sort of been stuck with figuring out how to make it all work, but I cant get these files renamed. Its driving me nutz!!
 
Old 11-12-2008, 10:58 AM   #4
Cichlid
Member
 
Registered: Jan 2002
Location: Montreal
Distribution: Ubuntu 8.10
Posts: 178

Rep: Reputation: 30
In the terminal type: man cp
You will find instruction on renaming files there.
 
Old 11-12-2008, 11:01 AM   #5
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Original Poster
Rep: Reputation: 0
I'm a bit new to all this stuff to fully understand how to use cp to rename, even after reading through the manual. Will cp work in

find -iname .new-account | cp .new-account .new-user

?
 
Old 11-12-2008, 11:13 AM   #6
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
Quote:
Originally Posted by dmgctrl View Post
no! I work in an office where we all user Vista, but our servers run linux. The management saw fit to fire 3/4's of the staff on Monday and now they are looking to do some new things. I've sort of been stuck with figuring out how to make it all work, but I cant get these files renamed. Its driving me nutz!!
I'm afraid you have to start reading some good linux book, for example Rute User's Tutorial and Exposition.

Regarding your issue, if I correctly understand you're trying to rename the file .new-account in every user's home to .new-user, right? Keep it simple and use a loop over the files, e.g.
Code:
while read name
do
  dir=$(dirname "$name")
  echo mv "$name" "$dir/.new-user"
done < <(ls /home/*/.new-account)
I've put an echo in front of the mv command for testing purposes: this will simply echo the command without actually execute it. When you're satisfied with the result, strip out the echo and the trick is done.
 
Old 11-12-2008, 11:21 AM   #7
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Original Poster
Rep: Reputation: 0
thanks colucix. I ran the following command

find -name .new-account | while read name; do dir=$(dirname "$name") echo mv "$name" "$dir/.new-user" done; < < (ls /home/*/.new-account)

but I get the following error:

-bash: syntax error near unexpected token `<'

I am going to start learning linux properly, and have bookmarked that link you provided. But for now I just need to get these files renamed! Have I written the expression incorrectly above?
 
Old 11-12-2008, 11:25 AM   #8
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
Don't use find, don't put a semi-colon after the done statement... just simply try the code I suggested as is. You will see it works!

The syntax
Code:
done < <(ls blablabla)
just passes the output of the ls command to the loop, one line (that is one file name) at a time. It is called process substitution. There are other ways to pass input to a loop, but this has some advantages over the others.
 
Old 11-12-2008, 11:35 AM   #9
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Original Poster
Rep: Reputation: 0
I am gift wrapping copious quantities of cold beer for you. That worked a treat. Thank you SO much, it was a headache for the last 24 hours for me!
 
Old 11-12-2008, 12:09 PM   #10
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
Quote:
Originally Posted by dmgctrl View Post
I am gift wrapping copious quantities of cold beer for you. That worked a treat. Thank you SO much, it was a headache for the last 24 hours for me!
You're welcome! I raise my mug of beer!
 
Old 11-12-2008, 06:10 PM   #11
masonm
Senior Member
 
Registered: Mar 2003
Location: Following the white rabbit
Distribution: Slackware64 -current
Posts: 2,300

Rep: Reputation: 90
Quote:
Originally Posted by dmgctrl View Post
no! I work in an office where we all user Vista, but our servers run linux. The management saw fit to fire 3/4's of the staff on Monday and now they are looking to do some new things. I've sort of been stuck with figuring out how to make it all work, but I cant get these files renamed. Its driving me nutz!!
Sorry, we see a lot of homework questions here and try to help students without actually doing their homework for them.

Just to help you out a little more, the command 'mv' is what you were looking for (and why the script given works), no such thing as 'rename'.

Personally, if I were you I would let the servers flounder until the management realizes they shouldn't have fired the people needed to keep them going.

But welcome to LQ and Linux. Any additional questions, we'll be glad to help.
 
Old 11-12-2008, 06:50 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,673
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
You might be thinking about the xargs command, which is designed to execute commands that are built-up: in part from whatever appears on the xargs command-line, and in part from whatever it reads from standard-input.

In this case, you might wish to use the for construct of shell-scripting:

Code:
 for fn in *.txt; do echo $fn; done; 
  • The block of statements will be repeated for each file in the current (or specified...) directory whose filename matches the pattern *.txt.
  • The arbitrary symbol ("fn" in this example), which is listed without the "$" the first time, becomes a substitution parameter (with the "$") in the command(s) which lie between do and done.
  • The placement of semicolons (and the word "do") in the foregoing example is "niggling, arbitrary, but very important." Otherwise it just won't work.
As is so often the case in Linux, "there's more than one way to do it." Not only do you have "shell scripts," but you also have several full-featured programming languages (Perl, PHP, Ruby...) all of which can easily be used to build a command. (That's what the "shebang" at the start of a command-file ... #! ... is all about.)

In this respect, Unix/Linux is so far ahead of Microsoft Win-doze that it takes quite a bit of getting used to. (And it makes Win-doze almost impossible for a Linux maven to "put up with anymore ..." )
 
Old 11-13-2008, 02:12 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You might also find this useful for explaining a few differences in the mindset required when using MS vs Linux: http://linux.oneandoneis2.org/LNW.htm
 
Old 11-13-2008, 02:34 AM   #14
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
Quote:
Originally Posted by masonm View Post
Sorry, we see a lot of homework questions here and try to help students without actually doing their homework for them.
Well, you already asked the OP about homework and he replied this is not homework at all. Just a linux newbie question. Giving suggestion about a good reading and a working piece of code, serves to encourage someone to learn. Hopefully.
 
Old 11-13-2008, 10:18 AM   #15
dmgctrl
LQ Newbie
 
Registered: Nov 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks so much for all your help guys. This has been a surprisingly welcoming forum for a newbie like myself. I have been sort of timid in approaching linux, but I am seeing its benefits already.

As far as the renaming thing goes, I have a much better idea about it now. Problem is, after I got it done, I then realised that the ".new-account" file isnt in all the directories that I need it to be in! Is there a way I could create a file named .new-account in each directory in /home/*/public_html? I could then run the previous command to change them all to .new-user, and my problems would soon be over.

Any input you have will be very much appreciated.

Thanks for your welcome to this board everyone!
 
  


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
Renaming files Viablade Linux - Newbie 11 10-10-2008 01:54 PM
help renaming files please balistic Linux - Newbie 2 07-29-2007 03:35 PM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 02:06 PM
renaming files TomalakBORG Linux - Newbie 4 12-24-2005 10:14 AM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 01:16 PM

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

All times are GMT -5. The time now is 02:28 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