LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2016, 09:24 AM   #1
acacius
LQ Newbie
 
Registered: Jun 2015
Posts: 11

Rep: Reputation: Disabled
Bash script to rename files


I have bash 4.3.30. I am trying to make a script that will do the following:

If a filename in current directory contains a special character other than a dot or a dash, replace character with underscore.

Here is what I've tried:

Code:
#!/bin/bash

for FILE in *
do
 case [[ $FILE2 =~ (^\.a-zA-Z0-9\-) ]] in 

# trying to substitute a space here: but I get a
# syntax error at line 5.

  *\ *) $FILE2 = "${FILE2// /_}" ;;
 esac
  mv "$FILE" "$FILE2"
done
Can someone guide me?
 
Old 08-23-2016, 09:50 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
What is your strategy, replace a special specific character with a specific character in each case?
Wouldn't be more efficient to replace all special characters with a specific character in one pass?
 
Old 08-23-2016, 10:04 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
https://centos.tips/fixing-troublesome-filenames/
 
Old 08-23-2016, 10:55 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
substitution example
Code:
str=',;!!&@`(|^=+}'
echo ${str//[^\.a-zA-Z0-9\-]/_}

It's possible to limit the _'s, eg replace all consecutive special chars with one _
Code:
shopt -s extglob
str=',;!!&@`(|^=+}'
echo ${str//+([^\.a-zA-Z0-9\-])/_}
 
1 members found this post helpful.
Old 08-23-2016, 08:52 PM   #5
acacius
LQ Newbie
 
Registered: Jun 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Great, that worked.
 
Old 08-24-2016, 02:13 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
  *\ *) $FILE2 = "${FILE2// /_}" ;;
is syntactically incorrect:
Code:
  *\ *) FILE2="${FILE2// /_}" ;;
#      ^    ^ ^  no $, no space before/after =
this is just one line, probably there are other errors
 
Old 08-24-2016, 07:56 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Thanks keefaz! That is a keeper.
Code:
echo ${str//+([^\.a-zA-Z0-9\-])/_}
can also be written as
Code:
echo "${str//+([^[:alnum:].-])/_}"
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script to rename files elalexluna83 Programming 5 10-08-2013 12:31 AM
Help with bash script to rename multiple files zookman Programming 1 05-09-2011 03:23 AM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration