Renaming the file names in a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Renaming the file names in a directory
# 1  
Old 04-20-2017
Renaming the file names in a directory

Hi, I have about 60 files in a directory and need to rename those files. For example the file names are

Code:
i_can_phone_yymmdd.txt  (where yymmdd is the date. i.e 170420 etc)
i_usa_phone_1_yymmdd.txt
i_eng_phone_4_yymmdd.txt

The new file names should be
Code:
phone.txt
phone_1.txt
phone_4.txt

I am using the following command but it works only for i_can_phone-170420.txt. The command is

Code:
for file in `ls *.txt`
do
  echo 'The file name is ......' $file

  new_file=`echo ${file}|cut -f3 -d_`.txt

  echo 'The new file name is ......' $new_file
  mv ${file} $new_file
done;


Last edited by naveed; 04-20-2017 at 12:38 PM..
# 2  
Old 04-20-2017
With 15 posts under your belt, you should have figured out code tags by now. Select text and hit the code button, the Image button.

`ls *.txt` is redundant, for file in *.txt does the same thing more reliably since it will not be confused by spaces in filenames.

If I understand you I think this may work:

Code:
#!/bin/bash

# IFS is a special variable which controls splitting.
# Set it to _ and we can split on that string.
# But we will need to restore its original value later!
OLDIFS="$IFS"
IFS="_"

for file in *.txt
do
  echo 'The file name is ......' $file

  # Split on IFS and store in $1, $2, $3 variables.
  # $1="i", $2="can", $3="phone", $4="170420.txt"
  set -- $file 
  shift 3 # Remove "i", "can", "phone", leaving $1="170420.txt"

  new_file=$(printf "_%s" "$@") # new_file=_$1_$2_$3_...
  new_file="${new_file:1}" # Strip off leading _

  echo "The new file name is ...... $new_file"
  echo mv ${file} $new_file
done;

IFS="$OLDIFS"

Remove the echo from mv once you've tested and are sure this does what you want.
# 3  
Old 04-20-2017
Hi.

There are utilities that can do much of the work for you:
Code:
Rename multiple files, groups of files

        1) rename -- Debian version and RedHat version differ, q.v.
           (try package util-linux:
           http://en.wikipedia.org/wiki/Util-linux)

        2) ren -- RedHat relatives

        3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname

        4) mved -- (circa 2006; good as of 2015.05), perl
           http://raf.org/mved/
           (An earlier shell version may be available.)

        5) rename -- perl builtin library routine (DIY)

        6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns

        7) gprename - batch rename using a GUI

        8) krename - batch rename using a GUI

Bet wishes ... cheers, drl
# 4  
Old 04-20-2017
Try also
Code:
for FN in *.txt
  do TFN=${FN#?_*_}
     echo mv ${FN} ${TFN%_*}.txt
  done
mv i_can_phone_yymmdd.txt phone.txt
mv i_eng_phone_4_yymmdd.txt phone_4.txt
mv i_usa_phone_1_yymmdd.txt phone_1.txt

Remove the echo if happy with the resulting proposals.
# 5  
Old 04-21-2017
Thanks a lot RudiC, your logic works. It has save a hell lot of time for me. Have a nice day.

Naveed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ab.name.11.first ab.name.12.second ab.name.13.third ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

3. 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

4. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

5. UNIX for Dummies Questions & Answers

Renaming files in one file from names in other

Hi Guys, I have a small problem of renaming multiple files. For example I have names of a set of files in one directory like K2_34625-34675 K7_988963-988983 K12_773882-7734102 and the other set corresponding to the same is U_P_321_9_3_11.ab1 U_P_322_9_3_11.ab1 U_P_323_9_3_11.ab1 Now... (23 Replies)
Discussion started by: pawannoel
23 Replies

6. Shell Programming and Scripting

Renaming file names

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

8. UNIX for Advanced & Expert Users

Permissions for Renaming a File vs Directory

Hello, I'm trying to fully understand UNIX permissions as applied to directories. While testing my understanding today, I came across something I found peculiar. Please consider the following directory structure: mydir (bob/bob: 0700) +myfile (root/root: 0700) +mysubdir... (2 Replies)
Discussion started by: daviejones
2 Replies

9. 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

10. Shell Programming and Scripting

Renaming file names in a shell script

I want to write a shell script that will rename all the file names to today's date attached to it.. so for example i have a file names like file1.sales.20081201.txt.c zbrs.salestxtn.20091101.txt.inn then it will rename both the files with todays date to it so the file names get changed... (1 Reply)
Discussion started by: rudoraj
1 Replies
Login or Register to Ask a Question