Remove spaces from start of file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces from start of file names
# 1  
Old 07-29-2012
Remove spaces from start of file names

Hi,

I have a directory with the following file names


Code:
01 -  abc hyn
02-def
03-ghi[www.Unix3333.com].dir
04 - jhu[www.Unix.com].dir
 abc1 kil
  def bil

The last two file names abc1 starts with one space and def starts with double space. I want these files in my directory to be renamed as

Code:
ABC HYN
DEF
GHI.dir
JHU.dir
ABC1 KIL
DEF BIL

Thanks

Last edited by jacobs.smith; 07-29-2012 at 03:37 PM..
# 2  
Old 07-29-2012
If you have a list of the files you want renamed in a file named "list", the following script should work:
Code:
#!/bin/ksh
IFS=""
while read old
do
        new=${old##*([-0-9 ])}
        new=${new/\[*\]/}
        nds=${new%.dir}
        if [ "${nds}.dir" == "$new" ]
        then
                suf=.dir
        else
                suf=
        fi
        new=$(printf "%s\n" "$nds"|tr "[:lower:]" "[:upper:]")
        mv "$old" "$new$suf"
done < list

Note that this assumes that there is no more than one occurrence of [...] in a line.

This seems like a pretty strange set of conversions???
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-29-2012
Hi Don,

How can I run this script inside a folder?

Because all those are files inside a directory with those names.
# 4  
Old 07-29-2012
Do you want to apply this naming convention to ALL files in the directory; or only to a selected list of files?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-29-2012
All files in the directory
# 6  
Old 07-29-2012
Install the following in your $HOME/bin directory as a filed named "namechange":
Code:
#!/bin/ksh
IFS=""
while read old
do
        new=${old##*([-0-9 ])}
        new=${new/\[*\]/}
        nds=${new%.dir}
        if [ "${nds}.dir" == "$new" ]
        then
                suf=.dir
        else
                suf=
        fi
        new=$(printf "%s\n" "$nds"|tr "[:lower:]" "[:upper:]")
        mv "$old" "$new$suf"
done

and make it executable. Then issue the commands:
Code:
cd directory
ls | $HOME/bin/namechange

where "directory" is the name of the directory containing the files you want to rename.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-29-2012
Thanks for a great help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding size of files with spaces in their file names

I am running a UNIX script to get unused files and their sizes from the server. The issue is arising due to the spaces present in the filename/folder names.Due to this the du -k command doesn't work properly.But I need to calculate the size of all files including the ones which have spaces in them.... (4 Replies)
Discussion started by: INNSAV1
4 Replies

2. OS X (Apple)

Remove leading spaces from file names and folders

Hi All, I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems... (1 Reply)
Discussion started by: prometheon123
1 Replies

3. Shell Programming and Scripting

remove spaces and lines that start with --

Is it possible to remove empty lines between >humid-sets (bold) and also humidset that start with -- (for ex: > humid3 | () : | (+) ) Thanx in advance Note: The humid sets will be in thousands and lines will be more than 100 thousand. input > humid1 | () : | (+)... (7 Replies)
Discussion started by: quincyjones
7 Replies

4. Shell Programming and Scripting

How to strip the spaces in file names?

please somebody tell me what is wrong with this, while the thumbnail grabbing works and encoding works, but what is not working is, mv $i.jpg /var/www/thumbs/ and mv $i.mp4 /var/www/uploads/ #!/bin/bash # MINT 9 - FFMPEG - QT-FASTSTART - X264 - MP4 DIR=/var/www/tmp for i in... (9 Replies)
Discussion started by: mysoogal
9 Replies

5. Shell Programming and Scripting

Opening File names with spaces inside it !- PERL

I developed a perl code..And the excerpt from it is given below... open(HANDLE,$cmp_path) ; #reading the xml file from the file path while($file_path = <HANDLE>) I have list of XML files to read from a folder. It has some spaces inside the name of the file...I used "\"... (2 Replies)
Discussion started by: gameboy87
2 Replies

6. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies

7. UNIX for Dummies Questions & Answers

Unable to use mimesender to send attachments with spaces in the file names / paths

Hello, I found the mimesender multiple attachment emailing shell script in the FAQ of these forums, and I have been able to use it to send multiple files, but only if they don't have spaces in their file name or path. When I attempt to send a file with spaces in it's name, enclosed... (0 Replies)
Discussion started by: rsmorra
0 Replies

8. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

9. Shell Programming and Scripting

File names with spaces? Is it possible?

Gurus - I got one simple TXT file with long file name with blank spaces in between the words. I am trying to display that full file name, but it breaks while displaying. Could somebody shed some light here? Script ------ for i in `cat ~\temp\employee.txt` do echo $i done (5 Replies)
Discussion started by: Eric_2005
5 Replies

10. Shell Programming and Scripting

how to find capital letter names in a file without finding words at start of sentence

Hi, I want to be able to list all the names in a file which begin with a capital letter, but I don't want it to list words that begin a new sentence. Is there any way round this? Thanks for your help. (1 Reply)
Discussion started by: kev269
1 Replies
Login or Register to Ask a Question