find & remove characters in filenames


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find & remove characters in filenames
# 8  
Old 12-19-2011
Awesome. I knew what IFS was, but I had never changed it before, and the whole leading/trailing spaces being stripped thing didn't occur to me before you mentioned it. Thanks for taking the time to explain that in such detail!

But wait.. So I understand the WHY now, but not specifically the HOW, i.e., how you used it in your original script.
Code:
while IFS= read -r

I don't get it... what is this actually setting IFS to? How are you setting a variable and then running a command without a semicolon or command substitution? Hmmm. I'm too tired to try to work this out right now.
# 9  
Old 12-19-2011
This type of variable assignment affects only the command it precedes (if you insert a semicolon between the assignment and the command the meaning changes):

with semicolon (IFS is affected inside the loop, a and b parsed correctly):

Code:
bash-4.1$ printf '%s:%s\n' a b | 
>   while IFS=:; read a b; do 
> printf 'IFS => '
> printf '%s' "$IFS" |
>   od -bc 
> printf '%s => %s\n'  a "$a"  b "$b"  
>   done
IFS => 0000000 072
          :
0000001
a => a
b => b

without semicolon (inside the loop IFS remains unchanged but a and b are still parsed correctly):

Code:
bash-4.1$ printf '%s:%s\n' a b | 
>   while IFS=: read a b; do 
> printf 'IFS => '
> printf '%s' "$IFS" |
>   od -bc 
> printf '%s => %s\n'  a "$a"  b "$b"  
>   done
IFS => 0000000 040 011 012
             \t  \n
0000003
a => a
b => b

From man bash (SIMPLE COMMAND EXPANSION):
Code:
       When a simple command is executed, the shell performs the following expansions, assignments, and redirections,
       from left to right.

       1.     The  words  that  the  parser has marked as variable assignments (those preceding the command name) and
              redirections are saved for later processing.

       2.     The words that are not variable assignments or redirections are expanded.  If any  words  remain  after
              expansion,  the first word is taken to be the name of the command and the remaining words are the argu-
              ments.

       3.     Redirections are performed as described above under REDIRECTION.

       4.     The text after the = in each variable assignment undergoes tilde expansion, parameter  expansion,  com-
              mand substitution, arithmetic expansion, and quote removal before being assigned to the variable.

       If  no  command  name  results, the variable assignments affect the current shell environment.  Otherwise, the
       variables are added to the environment of the executed command and do not affect the  current  shell  environ-
       ment.   If  any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the
       command exits with a non-zero status.

Another example for this type of assignment:

Code:
bash-4.1$ a=1; a=2 awk 'BEGIN { printf "%s => %s\n", ARGV[0], ENVIRON["a"] }'; printf '%s => %s\n' "$0" "$a"
awk => 2
bash => 1

This User Gave Thanks to radoulov For This Post:
# 10  
Old 12-20-2011
wow, this is getting very technical for a mere noob like me!

One other quick question; I have a few mp3 files with a dot half way through the file name (ie track1 feat. artistx.mp3), up until now I have manually removed them as I came across it, how do I modify this command to remove them on mass?

Thanks!

Last edited by barrydocks; 12-20-2011 at 07:49 AM.. Reason: typo
# 11  
Old 12-20-2011
Code:
find . -type f -name '*.*.mp3' -exec rm {} +

Attention: the command above will remove all the files that have two (2) dots in their names and end by .mp3!

Make sure you have a valid backup before executing that command!
Try it without rm first:

Code:
find . -type f -name '*.*.mp3'

This User Gave Thanks to radoulov For This Post:
# 12  
Old 12-20-2011
sorry radoulov, you miss understood me: I want to remove the additional dot from the filename not delete the file?
Thanks
# 13  
Old 12-20-2011
OK, sorry!
Try this:

Code:
find . -type f -name '*.mp3' |
  while IFS= read -r; do
    p=${REPLY%/*} n=${REPLY#$p/}
    n=${n//[:?.\"]} n=${n%mp3}.mp3
    [ -f "$p/$n" ] && {
      printf >&2 '%s already exists\n' "$p/$n"
      continue  
        } ||
      mv -- "$REPLY" "$p/$n" 
  done

The code above will not overwrite existing files.
# 14  
Old 12-20-2011
Quote:
Originally Posted by radoulov
If no command name results, the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment. If any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the command exits with a non-zero status.
HOLY CRAP! 3.5 years since I started learning nix (and bash)--how the heck have I not seen this before! Wow. Thanks for the taking the time radoulov.

@barrydocks: Why do you want to remove dots from filenames? Since when do dots cause trouble on any os? I'm just curious...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To remove any invisible and special characters from the file(exclude @!#$&*)

Hi Guys, My requirement is to remove any invisible and special characters from the file like control M(carriage return) and alt numerics and it should not replace @#!$% abc|xyz|acd¥£ó adc|123| 12áí Please help on this. Thanks Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

2. Shell Programming and Scripting

Delete characters & find unique IP addresses with port

Hi, I have a file having following content. <sip:9376507346@97.208.31.7:51088 <sip:9907472291@97.208.31.7:51208 <sip:8103742422@97.208.31.7:51024 <sip:9579892841@97.208.31.7:51080 <sip:9370904222@97.208.31.7:51104 <sip:9327665215@97.208.31.7:51104 <sip:9098364262@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies

3. Shell Programming and Scripting

How best to remove certain characters from filenames and folders recursively

hello, I'm trying to figure out which tool is best for recursively renaming and files or folders using the characters \/*?”<>| in their name. I've tried many examples that use Bash, Python and Perl, but I'm not much of a programmer I seem to have hit a roadblock. Does anyone have any... (15 Replies)
Discussion started by: prometheon123
15 Replies

4. Shell Programming and Scripting

Perl: windows filenames escape characters

I have a perl find program that will find all files of window application stored on unix disks. Ofcourse these files contain all the weird characters windows allows, but on *nix pukes out all kinds of unwanted effects when processing these. Is their a utility that will escape all these... (3 Replies)
Discussion started by: karelb
3 Replies

5. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

6. Shell Programming and Scripting

Find duplicate filenames and remove in different mount point

Hi Gurus, Do any kind souls encounter have the same script as mentioned here. Find and compare filenames in different mount point and remove duplicates. Thanks a million!!! wanna13e (7 Replies)
Discussion started by: wanna13e
7 Replies

7. Shell Programming and Scripting

Bash script - stripping away characters that can't be used in filenames

I want to create a temp file which is named based on a search string. The search string may contain spaces or characters that aren't supposed to be used in filenames so I want to strip those out. My thought was to use 'tr' with but the result is the opposite of what I want: $ echo "test... (5 Replies)
Discussion started by: mglenney
5 Replies

8. Shell Programming and Scripting

Filenames created with '\r' characters at the end

Hi all, Am creating files and doing copy,compare and deletion. As i do not want to mention the filepath everywhere, i store the filepaths in variables. FILENAME="/home/test/create/Myfile.txt" WR_PATH="/home/test/wrie/writefile.txt" RD_PATH="/home/test/myread/readfile.txt" echo "This is my... (2 Replies)
Discussion started by: amio
2 Replies

9. Shell Programming and Scripting

Script to find and remove characters

Hi. I have many files in a folder, and even more in the subfolders. I need a script that finds and removes certain characters (them being /n in this one) in the files in the folder and it's subfolders. So, could someone write me a script that works in Linux, does this: Searchs for "/n" in... (5 Replies)
Discussion started by: Zerby
5 Replies

10. Shell Programming and Scripting

Perl code to search for filenames that contain special characters

Hello, I have a requirement to search a directory, which contains any number of other directories for file names that contain special characters. directory structure DIR__ |__>DIR1 |__>DIR2__ |__>DIR2.1 |__>DIR2.2 |__>DIR3 .. ... (8 Replies)
Discussion started by: jerardfjay
8 Replies
Login or Register to Ask a Question