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-2011, 08:22 AM   #1
zappadragon
Member
 
Registered: Jul 2010
Posts: 36

Rep: Reputation: 0
Rename all files in dir to remove first nine characters


I have a directory on my desktop with several files in it. I would like to rename all of the files so that the first nine characters are removed or deleted. What is the command for doing this?

Thanks
 
Old 11-12-2011, 08:31 AM   #2
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
Being inside the directory (assuming it contains only the files you want to rename):
Code:
for file in *; do echo mv $file $(echo $file | sed -r 's/^.{9}//'); done
The sed command removes the first 9 characters from the filename. The echo statement (in red) serves only to test the command before actually executing it. Check the result and if it is what you want, remove the echo and run again.
 
1 members found this post helpful.
Old 11-12-2011, 08:31 AM   #3
jonkiribati
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Rep: Reputation: Disabled
You can use the find command with the exec option
 
Old 11-12-2011, 08:36 AM   #4
zappadragon
Member
 
Registered: Jul 2010
Posts: 36

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Being inside the directory (assuming it contains only the files you want to rename):
Code:
for file in *; do echo mv $file $(echo $file | sed -r 's/^.{9}//'); done
The sed command removes the first 9 characters from the filename. The echo statement (in red) serves only to test the command before actually executing it. Check the result and if it is what you want, remove the echo and run again.
Thank You SOOOOOO much! Worked perfect! I really need to learn sed stuff
 
Old 11-13-2011, 06:00 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please try searching the forum first in the future. The bulk renaming of files is one of the most commonly asked, and answered, scripting questions.

There are also a number of ready-made renaming applications you can use, and a search of your distribution's software repositories and/or the web should turn a good list of them.

It would help too if you gave some actual examples of the filenames you wanted to rename. It would make it easier to recommend solutions that match your exact situation.

As for the above, you can even eliminate sed and just use bash's built-in parameter substitution.

Code:
for file in *; do mv "$file" "${file:9}"; done
If there's some kind of regular pattern to the names, then it might be more appropriate to use a different pattern; one that targets exactly the string you want to eliminate. This goes for the input glob as well. We don't want it to rename the wrong files, after all.
 
  


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
Need to rename files and folders with invalid characters in the names laureynsr Linux - Newbie 5 04-01-2009 03:13 PM
Rename files with the first three characters towards the end of the name pwc101 Programming 12 06-15-2008 06:25 PM
Remove Certain Files/Dir from Large Dir thomas.beaver Linux - Newbie 2 01-27-2008 08:47 AM
How to rename file while copying - and remove special characters corporal79 Linux - General 3 10-11-2007 04:16 PM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM

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

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