Remove spaces between file names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove spaces between file names
# 8  
Old 03-29-2009
Quote:
Originally Posted by srimitta
Any help in modifying your script to remove Apostrophe within file names.
No problem, but I am going to move this thread to "Shell Programming and Scripting" as it is not really AIX-related.

At first you have to change the filter to find also the files with apostrophes in their names. In the following, the first line is the original, the second line into what you should change it:

Code:
ls -1 | grep "[^ ] [^ ]" | while read oldfile ; do
ls -1 | grep "[^ '] [^ ']" | while read oldfile ; do

Second you have to change the replacement mechanism itself: the line where from the old filename the new one is derived. As this starts to get more complex we change from the simple "tr" to "sed:

Code:
newfile="$( print - $oldfile | tr -d ' ')"
newfile="$( print - $oldfile | sed 's/\'//g;s/ //g')"

Ready - that should do it.

I hope this helps.

bakunin
# 9  
Old 03-29-2009
Another approach with awk:

Code:
ls | awk '
{f=$0; gsub("[^A-Za-z0-9.]", "")}
f != $0 { system("mv " f " " $0) }'

First we store the original filename in the variable f and remove the characters, not defined in the class, from the current line (filename). Then we compare the the current line with the original filename and move the file if the name is changed.

Regards
# 10  
Old 03-29-2009
Thanks bakunin,

It's working with a small change, replaced single qoutes to double qoutes.
Quote:
newfile="$( print - $oldfile | sed "s/\'//g;s/ //g")"
# 11  
Old 03-30-2009
Hello Srimitta,

You can use the same script given by bacunin and replace the line

" newfile="$( print - $oldfile | tr -d ' ')" as

" newfile="$( print - $oldfile | tr -d ' '| tr -d ''')" "
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove dates from file names

Hi, I'm using a shell script. I have extracted current date files to a directory1 and the date should be removed on both sides of a CSV file. FYI... I'm looking to remove the date from the file name and not inside the CSV file. Directory1 2017-07-12_gmr_tag_log_20170711.csv... (0 Replies)
Discussion started by: shivamayam
0 Replies

2. Shell Programming and Scripting

Remove spaces from the file

Hi All, The output file contains data as below. "20141023","CUSTOMER" ,"COMPANY" ,"IN0515461" ,"" ,"JOSHUA" There are spaces in between the ending " and ,. The number of spaces is random. How can I remove that from the file so that the final output is:... (4 Replies)
Discussion started by: aarsh.dave
4 Replies

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

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

5. Shell Programming and Scripting

Remove spaces from start of file names

Hi, I have a directory with the following file names 01 - abc hyn 02-def 03-ghi.dir 04 - jhu.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 ABC HYN DEF GHI.dir... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

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

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

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

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

10. 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
Login or Register to Ask a Question