Help deleting prefix from file names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help deleting prefix from file names
# 1  
Old 09-10-2011
Help deleting prefix from file names

I have a directory full of personal files named similar to the following:
Code:
043 fflshju
923- osfjpojfsa
1 - oasfns.txt

Smilie

What I would like to do is strip any leading numbers, spaces, dashes (etc.)..
and leave only the main section of the name.

For example, the preceeding list would be renamed to:
Code:
fflshiu
osfipojfsa
oasfns.txt

Is there an easy way to do this from the command-line (or BASH script), or do I have to change them individually, manually?
Thanks in advance.
# 2  
Old 09-10-2011
Perl:
Code:
perl -e'
  for (glob "[0-9-]*") {
    ($fn = $_) =~ s/^[\s\d-]+//;
    rename $_, $fn unless -f $fn
    }'

KornShell:

Code:
for f in +([ 0-9-])*; do
  [ -f "${f##+([ 0-9-])}" ] || 
    mv -- "$f" "${f##+([ 0-9-])}"
done

bash:

Code:
shopt -s extglob
for f in +([ 0-9-])*; do
  [ -f "${f##+([ 0-9-])}" ] || 
    mv -- "$f" "${f##+([ 0-9-])}"
done

zsh:

Code:
setopt extendedglob
for f in ([ 0-9-])##*; do
  [ -f "${f##([ 0-9-])##}" ] ||
    mv -- "$f" "${f##([ 0-9-])##}"
done


Last edited by radoulov; 09-10-2011 at 04:28 PM..
This User Gave Thanks to radoulov For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

2. Shell Programming and Scripting

IF condition to compare file prefix

I have files with naming as below, testS123, testS223, testB1, testC1, testD1 I need to write a if condition to print 'Hello' when the file prefix is not testS* else 'Good bye'. if then echo "Hello" else echo "Good bye" fi; (1 Reply)
Discussion started by: r@v!7*7@
1 Replies

3. UNIX for Advanced & Expert Users

Prefix/Suffix on same file

Hi, I want to add prefix and suffix on line# 205 using SED or AWK and want to change on the same file without creating new file. This command will be used in the bash script Am using Bash shell Regards Nayaj (3 Replies)
Discussion started by: Nayaj
3 Replies

4. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

5. UNIX for Dummies Questions & Answers

Deleting part of file names

My server got messed up and the names of my files were not completely processed. As results I ended up getting a bunch of files with the following names: I need to remove the underscore and everything before it. Thus, the files will be renamed to something like this: Any help will be greatly... (3 Replies)
Discussion started by: Xterra
3 Replies

6. Shell Programming and Scripting

ftp file and change prefix

Hi, i want to ftp to another server and change the prefix of the file and change direcotry also. but i am unable to get it. any ideas will be helpful? ftp -n ${HOST} <<END quote USER $USER quote PASS $PASS cd /dir/dir put file mv file fnl.fileEND END (2 Replies)
Discussion started by: dazdseg
2 Replies

7. Shell Programming and Scripting

Remove prefix per line in file

Hi, I'm using a .ksh script to split one file into multible files by checking for the prefix per line. It works perfekt (thanks again for anyone involved in helping me with that ;)), but I want to remove the prefix per line too. Means only the line information itself should remain in the... (7 Replies)
Discussion started by: spidermike
7 Replies

8. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

9. Shell Programming and Scripting

Prefix a string to the contents of a file

Hi all, I have a requirement where in i have to create a temporary file by prefixing a string of special characters to each row of a input file. the input file is '|' delimited. here is the content of input file aaa|1234|axad|123 bbb|qwqw|qw|1334 the output should be ... (5 Replies)
Discussion started by: nvuradi
5 Replies

10. Shell Programming and Scripting

Check if a file exists with certain prefix

Hi guys, How would i check a file exists with certainprefix? i have a directory with some files: ABC1 ABC2 ABC3 etc.. and want to do: please note i am using the korn shell environment.As when i gone through some stuff on then net i came to know some of the options will work... (11 Replies)
Discussion started by: raoscb
11 Replies
Login or Register to Ask a Question