4

I have been trying to write a script that will take the current working directory, scan every file and check if it is a .txt file. Then take every file (that's a text file), and check to see if it contains an underscore anywhere in its name and if it does to change the underscore to a hyphen.

I know that this is a tall order, but here is the rough code I have so far:

#!/bin/bash
count=1
while((count <= $#))
       do
          case $count in
               "*.txt") sed 's/_/-' $count
          esac
          ((count++))
done

What I was thinking is that this would take the files in the current working directory as the arguments and check every file(represented by $count or the file at "count"). Then for every file, it would check if it ended in .txt and if it did it would change every underscore to a hyphen using sed. I think one of the main problems I am having is that the script is not reading the files from the current working directory. I tried included the directory after the command to run the script, but I think it took each line instead of each file (since there are 4 or so files on every line).

Anyway, any help would be greatly appreciated! Also, I'm sorry that my code is so bad, I am very new to UNIX.

2
  • One problem is that $count is the value of the variable count, which is some integer. If you want to iterate over the positional parameters, you'd write: for file in "$@"; do ... or the shorthand for file; do ... Commented Oct 7, 2011 at 19:03
  • check here:theunixshell.blogspot.com/2013/01/… Commented Jan 10, 2013 at 6:29

4 Answers 4

2
for fname in ./*_*.txt; do
  new_fname=$(printf '%s' "$fname" | sed 's,_,-,')
  mv "$fname" "$new_fname"
done
Sign up to request clarification or add additional context in comments.

1 Comment

If the shell is bash, one can write: new_fname=${fname//_/-}
1

why not:

rename 's/_/-/' *.txt

2 Comments

rename: command not found. The question is about Unix, not a particular Linux distribution. But given that Perl is installed almost everywhere, it's a nice and flexible solution.
@RolandIllig: I know rename is not everywhere, but from shebang I assumed it's executed on some modern unix-like system.
0
$ ls *.txt | while read -r file; do echo $file | 
  grep > /dev/null _ && mv $file $(echo $file | tr _ -); done

(untested)

3 Comments

This will fail on files containing spaces in filename. You may even loose some data if yo do wrong mv.
Yes, it will fail on files containing spaces. People who put spaces in their file names deserve to lose all their data. :)
I kind of agree... :-) However we are here to learn of each other that is why I added this comment. Regards.
0

Thanks for all your input guys! All in all, I think the solution I found was the most appropriate for my skill level was:

ls *.txt | while read -r file; do echo file |
   mv $file $(echo $file | sed 's,_,-,');
done

This got what I needed done, and for my purposes I am not too worried about the spaces. But thanks for all your wonderful suggestions, you are all very intelligent!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.