Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello, I'm pretty new to the Linux world. I'm having a hard time understanding how to go about changing file names. I have searched through this forum board for possible solutions to what I am trying to do but the ones that were on the board did not seem to make sense to me, or work.
I am using the Terminal Linux - bash shell, I hope that makes sense?
I am trying to figure out a way to change files in multiple different ways. The only one that I can see that fits is the following command:
mv file1.txt file.txt
Which renames file1.txt to file.txt.
Now I tried to use the rename command, but I did not understand that one at all. I even tried doing the solutions that were found on the board, but I couldn't figure it out (understand it).
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/' *
OPTIONS
-v, --verbose
Verbose: print names of files successfully renamed.
-n, --no-act
No Action: show what files would have been renamed.
-f, --force
Force: overwrite existing files.
ENVIRONMENT
No environment variables are used.
AUTHOR
Larry Wall
SEE ALSO
mv(1), perl(1)
DIAGNOSTICS
If you give an invalid Perl expression you’ll get a syntax error.
BUGS
The original "rename" did not check for the existence of target
filenames, so had to be used with care. I hope I’ve fixed that (Robin
Barker).
When I bring up the man rename page this is all I see:
Code:
RENAME(1) Linux Programmerâs Manual RENAME(1)
NAME
rename - Rename files
SYNOPSIS
rename from to file...
DESCRIPTION
rename will rename the specified files by replacing the
first occurrence of from in their name by to.
RENAME(1) Linux Programmerâs Manual RENAME(1)
NAME
rename - Rename files
SYNOPSIS
rename from to file...
DESCRIPTION
rename will rename the specified files by replacing the
first occurrence of from in their name by to.
For example, given the files foo1, ..., foo9, foo10,
..., foo278, the commands
rename foo foo0 foo?
rename foo foo0 foo??
will turn them into foo001, ..., foo009, foo010, ...,
foo278.
And
rename .htm .html *.htm
will fix the extension of your html files.
SEE ALSO
mv(1)
So how is it different between the two of us? I copied and pasted this from Terminal window.
It would be helpful if you could give more information on exactly what you are trying to accomplish. What type of files you are trying to change and what about them you want to change.
In a terminal, cd to the directory these files are in for it to work. This will change any file in the directory beginning with "text" to "chap" so if you have files you don't want to change you will need to move them. I expect that "text" is not the actual name so...
Thank you so much! I was trying to do it that way but kept getting it wrong, that rename command is kind of confusing if you ask me. Thank you so much though! I appreciate it!!
You're most welcome ... the command is not that hard if you manage to
understand the man-page ... it's just as it says in the brief summary:
Quote:
rename from to file...
In anything file (or other names following [which the ellipsis represents]
replace the first occurrence of the string from with the string to.
In your example all files have text which you want to replace with chap,
and you want to do that to all files that have a beginning of text, followed
by a number and the extension .txt.
Let us say there are 3 files: text1.txt, text2.txt and text3.txt. Give two sets of commands to rename these files to chap1.txt, chap2.txt and chap3.txt, rename command.
Here is the rename command:
for INPUT in chap*.txt
do
OUTPUT=`echo $INPUT | sed 's/chap/new/'`
mv $INPUT $OUTPUT
done
or as a one liner
Code:
for INPUT in chap*.txt; do OUTPUT=`echo $INPUT | sed 's/chap/new/'`; mv $INPUT $OUTPUT; done
This route you can change mv to echo to see if what it's going to attempt is what you intended to be attempted. And probably a dozen other ways. If you have funky filenames you might need to do fancy stuff with mv. mv -- "old_odd_name" "new_odd_name". Or would it be single quotes? Or I could be wrong.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.