Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

enter image description hereI have files in UNIX directory which have a unique number starting with, i have to remove the unique id followed by '-' from the file name and need to have regular file. Any help is appreciated.

Example: 32456113-report.pdf

Required File name in the database: report.pdf

I have file names of all kind of extensions .pdf, .doc, .xls, .txt but have the same number in the front.

I am using winscp to test or view the data and i am running a shell script by registering it as a host concurrent program in oracle apps.

share|improve this question
    
Not sure I understand where the database fits in. Are you using another language to interact with a database? –  Josh Berry Jul 2 '13 at 19:57
add comment

3 Answers

for f in *; do 
    regular="${f#*-}"
    echo "$f => $regular"
done

This parameter expansion (${f#*-}) removes, from the start of the variable's value, the shortest string that ends with a hyphen (see manual). So:

$ f=32456113-summary-report.doc
$ echo "$f => ${f#*-}"
32456113-summary-report.doc => summary-report.doc
share|improve this answer
    
mv: cannot rename /tmp/34178248 to /tmp/34178248//34178248: Invalid argument /u01/R2ID10/R2ID10appl/xxps/11.5.0/bin/XXPS_MULTIPLE_EMAIL3: bad substitution /u01/R2ID10/R2ID10appl/xxps/11.5.0/bin/XXPS_MULTIPLE_EMAIL3 Program exited with status 1 –  sam Jul 2 '13 at 20:41
    
Thanks ... this is error i got when i run this code –  sam Jul 2 '13 at 20:42
    
This code might not work for ksh, which is what @sam is using. –  Evan Teitelman Jul 2 '13 at 20:42
    
This code does work in ksh, that's where I tested it. @sam, please show what you're actually running, edit your question with more details please. –  glenn jackman Jul 2 '13 at 20:45
    
mv -- "34178248-2640006.pdf" "${34178248-2640006.pdf}" when i use this i got the desired result, can i automate the process for all the files in the directory –  sam Jul 2 '13 at 23:39
add comment

Not entirely sure I understand what you are trying to do, but I believe a simple bash script as follows would work

for f in * ; do mv "$f" "$(echo "$f" | cut -d- -f2)"; done

If you don't like the echo to cut nonsense, I can look up the string rules in bash for you.

share|improve this answer
    
I fixed up your code a bit to include more quotation marks. Also, don't parse ls output. –  Evan Teitelman Jul 2 '13 at 20:09
    
:) Thanks! I should have known better than parsing ls, but was too blinded by not immediately knowing the string rules of bash. –  Josh Berry Jul 2 '13 at 20:15
    
Apparently the original poster is using ksh. –  Evan Teitelman Jul 2 '13 at 20:17
    
Thanks for your reply..... my file name is changed but it was like this $(echo 34178248-BPI_TAX_EX--.pdf | cut -d- -f2). Any suggestion, i really appreciate for your time –  sam Jul 2 '13 at 20:31
    
You can use cut -d- -f2- instead. –  Evan Teitelman Jul 2 '13 at 20:35
show 6 more comments

You can use perl-rename.

perl-rename 's/[0-9]*-//' <files>

Depending your OS/distro, this utility may be called rename. To be safe, use the -n flag with perl-rename to do a dry run before actually renaming any files.

share|improve this answer
    
ksh[6]: syntax error at line 6 : ';' unexpected.... i got this when i run the above command in winscp –  sam Jul 2 '13 at 19:29
    
and Thanks for the quick reply –  sam Jul 2 '13 at 19:29
    
If i use rename it says not a valid command –  sam Jul 2 '13 at 19:32
    
How are you running the command? Is it part of a script? –  Evan Teitelman Jul 2 '13 at 20:07
1  
I've never used ksh or winscp so I may be missing something, but I find it strange that you're receiving an error on line 6 when you're only running one line. –  Evan Teitelman Jul 2 '13 at 20:36
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.