removing first 7 characters of directory names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers removing first 7 characters of directory names
# 1  
Old 10-17-2009
Data removing first 7 characters of directory names

Hi, I'm relatively new to unix, and would like to change the following files in a particular directory. The files have names like:

M10_90_Phcn402_3F.ab1
M10_94_Sput402_3F.ab1
M11_92_Abrg402_3R.ab1
M10_91_Cdel402_3F.ab1
M11_90_Phcn402_3R.ab1
M12_84_Sput402_3R.ab1
M10_93_Ehdr402_3F.ab1
M11_71_Cdel402_3R.ab1
M12_95_Epic402_3R.ab1

I would like to remove the first 7 characters of each file. For instance, in the first one, I would like M10_90_ to be removed, so the file name becomes Phcn402_3F.ab1

I have been playing with:

for x in M1*;do mv $x ${x/nnn/};done

I'm not sure what the correct expression is to remove the first 7 characters (not sure what to put in place of "nnn"). Is there a fast way to do this, or is there a different way to do this, maybe with "rename"?

Thanks

Last edited by euspilapteryx; 10-17-2009 at 07:31 PM..
# 2  
Old 10-17-2009
Code:
#!/bin/ksh
for i in M1*
do
   echo mv "${i}" "${i##???????}"
done

Once satisfied with the echo-ed results, remove the 'echo'
# 3  
Old 10-17-2009
To remove up to and including the second underscore:
Code:
mv "$x" "${x#*_*_}"

# 4  
Old 10-21-2009
Thanks, this is really useful!
# 5  
Old 10-22-2009
How about rename ?

Code:
rename 's/(.){7}//' *.ab1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename directory removing last characters

Hello - I was looking to write a simple script trying to rename sub-directories chopping off the last n characters. For example: In /home/myname/dir there are three sub-directories: directory1_1, directory2_2, and directory3_3. Is there a simple script to chop off the last... (4 Replies)
Discussion started by: twckfa16
4 Replies

2. UNIX for Dummies Questions & Answers

[solved]removing characters from a mass of file names

I found a closed thread that helped quite a bit. I tried adding the URL, but I can't because I don't have enough points... ? Modifying the syntax to remove ! ~ find . -type f -name '*~\!]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//~\!]}"; done These messages are... (2 Replies)
Discussion started by: rabidphilbrick
2 Replies

3. Shell Programming and Scripting

Removing unknow chars from file names.

I'm trying to move a large folder to an external drive but some files have these weird chars that the external drive won't accept. Does anyone know any command of any bash script that will look through a given folder and remove any weird chars? (4 Replies)
Discussion started by: Joktaa
4 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. UNIX for Dummies Questions & Answers

Removing path name from list of file names

I have this piece of code printf '%s\n' $pth*.msf | tr ' ' '\n' | sort -t '-' -k7 -k6r \ | awk -F- '{c=($6$7!=p&&FNR!=1)?ORS:"";p=$6$7}{printf("%c%s\n",c,$0)}' When I run it I get /home/chrisd/tatsh/branches/terr0.50/darwin/n02-z30-dsr65-terr0.50-dc0.002-8x6drw-csq.msf... (8 Replies)
Discussion started by: kristinu
8 Replies

6. UNIX for Dummies Questions & Answers

Loop through directory and extract sub directory names

I am trying to loop through folders and extract the name of the lowest level subfolder I was running the script below, it returns /bb/bin/prd/newyork /bb/bin/prd/london /bb/bin/prd/tokyo I really want newyork london tokyo I couldn't find a standard variable for the lowest level... (1 Reply)
Discussion started by: personalt
1 Replies

7. Shell Programming and Scripting

Removing files with same text but different file names

Hi All, I have some 50,000 HTML files in a directory. The problem is; some HTML files are duplicate versions that is wget crawled them two times and gave them file names by appending 1, 2, 3 etc after each crawl. For example, if the file index.html has been crawled several times, it has been... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. Red Hat

tar: Removing leading `/' from member names

Hello, when i start to take backup following error generate please share solution for this problem i am very thankful to you. $ tar -czvf orahome.tar.gz /testhome/TEST/PROD/orahome/ tar: Removing leading `/' from member names O.S 4.5 Red Hat Thanks, Umair (1 Reply)
Discussion started by: umair
1 Replies

9. UNIX for Dummies Questions & Answers

How to display only Owner and directory/sub directory names under particular root

hai, I am new to Unix, I have a requirement to display owner name , directory or sub directory name, who's owner name is not equal to "oasitqtc". (here "oasitqtc" is the owner of the directory or sub directory.) i have a command (below) which will display all folders and sub folders, but i... (6 Replies)
Discussion started by: gagan4599
6 Replies

10. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies
Login or Register to Ask a Question