Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove two first characters of filenames 3

Status
Not open for further replies.

licarse

IS-IT--Management
Sep 22, 2005
65
US
Hi,

I'm new in Unix. I'd like to know if there's any command/script to remove the first two characters of filenames in several files in a directory. For instance,

test1.any
other.some

rename them to

st1.any
her.some

Thanks in advance.
 
In a Korn-like shell:
Code:
for f in *; do mv $f ${f#??}; done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Another option is to use the cut command (man cut)

Code:
ls | cut -c 3-

Code what you mean,
and mean what you code!
But by all means post your code!

Razalas
 
Calling this script with parameter my_directory works too:
Code:
# rename file cutting 2 leading chars 
# usage
# [sh] rf2 dir

my_dir=$1

for file in $my_dir/*; do
  # new file name
  new_file=`cut -b 3- $file`
  echo "renaming file=$file -> new_file=$new_file"
  mv $file $my_dir/$new_file
done
 
mikrom, your script will not work as intended unless you play with cd or basename ...
 
stefanwagner, your suggestion don't play well with blanks in filenames due to the for:
ls | while read f; do mv "$f" "${f#??}"; done
 
Nice solution PHV! It worked great!

An additional complexity: What if I want to erase the first two characters ONLY IF they're [0-9]? (no letters).
 
ls [0-9][0-9]* | while read f; do mv "$f" "${f#??}"; done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks!!

I think I'm starting to understand the logic. Where can I read more about this recursive logic in order to start testing more expressions?
 
Hi

PHV said:
stefanwagner, your suggestion don't play well with blanks in filenames due to the for:
That works for me in Bash and (M)Ksh with all kind of blanks ( including newlines ( \n ) ). And I do not see where could be a problem as [tt]*[/tt] is expanded by the same process which executes the [tt]for[/tt]. Problems used to occur only when data is pass from one process to another like [tt]for f in `ls`; do[/tt].

Given that, I would go forward with the [tt]for[/tt] based code :
Code:
for f in [0-9][0-9]*; do mv "$f" "${f#??}"; done
Tested in [tt]bash[/tt] and [tt]mksh[/tt].

licarse said:
Where can I read more about this recursive logic in order to start testing more expressions?
There is absolutely no recursion. Search in your shell's man page for "expansion". There are many types of expansions, like parameter expansion ( ${f#??} ) and pathname expansion ( [0-9][0-9]* ). ( Some shells may use abit different terminology. )


Feherke.
 
Thanks feherke. Now what if I want to remove blank spaces in the first character of filenames?

Let's say

abc.txt

would be renamed to

abc.txt

what would be the command?

for f in ' \*'; do mv "$f" "${f#?}"; done

Am I right?
 
Hi

licarse said:
for f in ' \*'; do mv "$f" "${f#?}"; done

Am I right?
Well, almost :
[ul]
[li]if you escape the asterisk ( * ) it will not be expanded[/li]
[li]if you put the asterisk inside quotes it will not be expanded[/li]
[/ul]
Code:
for f in ' '*; do mv "$f" "${f#?}"; done

[gray]# or[/gray]

for f in \ *; do mv "$f" "${f#?}"; done


Feherke.
 
Almost got it :)

Thanks to help me start understanding expansion and recursion!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top