Linux, programming, computers and life

October 14, 2006

bash annoyance

Filed under: CLI, programming, linux

bash is a great shell. However, while i’m using it i sometimes simple don’t know what to do.
I want to rename some files with spaces in names. For example i have 2 files i need to remove first several chars from their names.

/tmp/tst>ls -l
total 0
-rw-r–r– 1 noone users 0 2006-10-14 10:54 this is file 1.txt
-rw-r–r– 1 noone users 0 2006-10-14 10:54 this is file 2.txt

I’d like to have the following result:
/tmp/tst>ls -l
total 0
-rw-r–r– 1 noone users 0 2006-10-14 10:54 is file 1.txt
-rw-r–r– 1 noone users 0 2006-10-14 10:54 is file 2.txt
Well, the obvious way would be:
for f in `ls`
do
    mv $f `echo $f | cut -b 5-`
done
But it doesn’t work, here’s the problem, file names have spaces:
for f in `ls`; do echo mv $f `echo $f | cut -b 5-`; done
mv this this
mv is
mv file
mv 1.txt t
mv this this
mv is
mv file
mv 2.txt t
mv
After some googling, i got the following way of treating file names with spaces:
find . -type f | while read file; do echo mv \’$file\’ \’`echo $file | cut -b 8-`\’; done;
mv ‘./this is file 1.txt’ ‘is file 1.txt’
mv ‘./this is file 2.txt’ ‘is file 2.txt’
Well, it looks to be working, let’s remove the echo and see what happens:
find . -type f | while read file; do mv \’$file\’ \’`echo $file | cut -b 8-`\’; done;
mv: target `1.txt\'’ is not a directory
mv: target `2.txt\'’ is not a directory

Here i was broken and i just did copy-paste of the ‘echo’ results before into the shell.
What should i do to make it work? Bash experts, HELP!
Update:
The correct code would be:
find . -type f | while read file; do mv $file `echo $file | cut -b 8-`; done;thanks to archlinux forums :)

Update 2:
Here’s python code to replace spaces with ‘_’ in file names:
#! /usr/bin/python
# -*- coding: UTF-8 -*-
# rename_file_with_spaces.py

import sys
import os

if len(sys.argv) != 2:
print ‘Usage ‘, sys.argv[0], ‘ directory’
sys.exit(1)

for fileName in os.listdir(sys.argv[1]):
if ‘ ‘ not in fileName:
continue
newFileName = fileName.replace(’ ‘, ‘_’)
os.rename(fileName, newFileName)

Technorati Tags: , , , ,

2 Comments »

The URI to TrackBack this entry is: http://linux4all.blogsome.com/2006/10/14/bash-annoyance/trackback/

  1. Uhmm, how about ‘rename’? ;)

    to convert spaces to underscores for instance:

    rename y/ /_/ *

    to convert to lowercase:

    rename y/A-Z/a-z/ *

    (Of course care should be taken with the *, that renames everything in the current directory…)

    Comment by eric casteleijn — December 21, 2006 @ 16:44

  2. As far as i know rename does not accept regexps. I tried what you suggested, but it did nothing. Did you check the command?

    Comment by linux4all — December 21, 2006 @ 20:03

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers