How to strip the spaces in file names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to strip the spaces in file names?
# 1  
Old 10-01-2010
Debian 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/


Code:
#!/bin/bash
# MINT 9 - FFMPEG - QT-FASTSTART - X264 - MP4

 
DIR=/var/www/tmp
 
for i in `find $DIR -type f`; do
ffmpeg -i $i -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 -threads 0 $i.mp4 

&& qt-faststart $i.mp4 $i.qt.mp4 

&& chmod 777 $i.qt.mp4 && ffmpeg  -itsoffset -4  -i $i -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x200 $i.jpg 

&& chmod 777 $i.jpg && mv $i.jpg /var/www/thumbs/ && rm $i.mp4 

&& rm $i && mv $i.qt.mp4 /var/www/uploads/

done



---------- Post updated at 01:15 AM ---------- Previous update was at 01:12 AM ----------



i wold also like to know how to strip the spaces in file names

example blaa blaa blaa.avi how would i replace the space with _ so it looks like this blaa_blaa_blaa.avi

beacuse sometimes ffmpeg does not understand videos with spaces in them, and files like these
blaaa[blaaaa]filename.avi

Moderator's Comments:
Mod Comment Note to mysoogal: Your original thread title was a rule violation. First and last warning on following forum rules.
# 2  
Old 10-01-2010
if the file names having spaces, the command would fail in the find itself. and it would split the value of $i to as many arguments with spaces.

try quoting your find command and $i both.

Code:
for i in "`find . -name -type f`"
do
 echo "$i"
done


Or use the while read method.
if it still fails, It would be better to split and command and add an OR ( ||) thing to debug more and increase readability.
# 3  
Old 10-01-2010
Quote:
Originally Posted by anchal_khare
if the file names having spaces, the command would fail in the find itself. and it would split the value of $i to as many arguments with spaces.

try quoting your find command and $i both.

Code:
for i in "`find . -name -type f`"
do
 echo "$i"
done

Or use the while read method.
if it still fails, It would be better to split and command and add an OR ( ||) thing to debug more and increase readability.
i try but it still can not understand, the file is Dream%20Job.mpg

the %20 is white space, how would i remove that with sed or ruby

forgive me, im new with this Smilie

this is what get

Code:
ffmpeg -i /var/www/tmp/Dream%20Job.mpg -acodec libfaac -ab 128k -ac 2  -vcodec libx264 -vpre slow -crf 22 -threads 0  /var/www/tmp/Dream%20Job.mpg.mp4

that %20 Smilie
# 4  
Old 10-01-2010
actual whitespace can be replaced like this:

Code:
i_new=$(echo "$i" | tr ' ' '_')

proceed with i_new
# 5  
Old 10-01-2010
Quote:
Originally Posted by anchal_khare
if the file names having spaces, the command would fail in the find itself. and it would split the value of $i to as many arguments with spaces.

try quoting your find command and $i both.

Code:
for i in "`find . -name -type f`"
do
 echo "$i"
done

Or use the while read method.
if it still fails, It would be better to split and command and add an OR ( ||) thing to debug more and increase readability.
That's absolutely useless. All of the filenames output by find will result in one long string. That string will be assigned to i and the for loop will only execute once. You may as well do: echo "`find . -name -type f`"

A better approach:
Code:
find . -type f -exec sh -c '
    for i; do
        # Do stuff with "$i" here
    done
' sh {} +

Regards,
Alister

Last edited by alister; 10-01-2010 at 04:00 AM..
This User Gave Thanks to alister For This Post:
# 6  
Old 10-01-2010
I'm getting confused again,

i read about sed, and ruby, they not have simple command to replace white space with _ in file%20name.avi ? so it looks like this file_name.avi ?
# 7  
Old 10-01-2010
The for loop will not work properly in this case because file names may contain whitespace and special characters. Also, you need to put double quotes around your variable references.
Try this:
Code:
find "$DIR" -type f |
while read i
do
  ffmpeg -i "$i" -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre slow -crf 22 -threads 0 "$i".mp4 &&
  qt-faststart "$i".mp4 "$i".qt.mp4 &&
  chmod 777 "$i".qt.mp4 &&
  ffmpeg  -itsoffset -4  -i "$i" -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x200 "$i".jpg &&
  chmod 777 "$i".jpg &&
  mv "$i".jpg /var/www/thumbs/ &&
  rm "$i".mp4 &&
  rm "$i" &&
  mv "$i".qt.mp4 /var/www/uploads/
done

BTW chmod 777 is considered bad practise

Last edited by Scrutinizer; 10-01-2010 at 04:09 AM..
This User Gave Thanks to Scrutinizer 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

Shell command to Strip white spaces at specific location

Hello, I have a Comma separated input file with one of the sample records as below: -9223372036854477528,"834","834003325515BXNI00101012013C","5","PLAN_VALUE","PPO, General Cable","C",20130101,99991231,"A","2012-12-25-13.58.14.434000","ZPL2 ","2012-12-25-13.58.14.434000","ZPL2 ... (4 Replies)
Discussion started by: Praveenkulkarni
4 Replies

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

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

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

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

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question