Monday, February 11, 2008
Renaming dos file names to unix format - BASH newbie
Thought of sharing this with all of you, a simple way of renaming dos format file names, replacing all spaces in file name with "_".
$ ls -lrt
total 1572
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 12:33 dos file1.txt
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 13:38 dosfile 2.txt
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 13:38 dos file 3.txt
$ ls | while read file; do mv "$file" `echo "$file" | tr ' ' '_'`; done
$ ls -lrt
total 1572
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 12:33 dos_file1.txt
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 13:38 dosfile_2.txt
-rw-r--r-- 1 jsaikia sta 529402 Feb 11 13:38 dos_file_3.txt
Why the "" with $file is important ?
See this small example:
$ ls -l
total 8
-rw-r--r-- 1 jsaikia sta 23 Feb 11 13:49 dos file 2.txt
-rw-r--r-- 1 jsaikia sta 23 Feb 11 13:49 dos file1.txt
$ cat dos\ file1.txt
this is a dos file (1)
$ cat dos\ file\ 2.txt
this is a dos file (2)
Now if I write a small one liner to cat the contents of all the files.
$ ls | while read file; do
> cat $file
> done
cat: dos: No such file or directory
cat: file: No such file or directory
cat: 2.txt: No such file or directory
cat: dos: No such file or directory
cat: file1.txt: No such file or directory
Now adding "" to $file variable.
$ ls | while read file; do cat "$file"; done
this is a dos file (2)
this is a dos file (1)
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
No comments:
Post a Comment